cidaas SDK for PHP

The PHP SDK enables the developer to use methods and objects to communicate with the commerce tools platform rather than using plain HTTP calls and untyped data.

Similarly, cidaas SDK helps users to add the functionalities of cidaas to their PHP applications such as laravel, Symfony etc.

cidaas (customer identity as a service) - is a cloud based service that abstracts how users authenticate to applications. Built using OAuth 2.0 and Open ID connect standards, our customer identity service provides an easy and simple integration with our pre-built social login, registration, and single sign on components.

Installation of cidaas PHP SDK

In order to use this sdk, you need to perform the following steps:

1. Add the cidaas repository to your composer.json configuration

"repositories": [
  {
    "type": "vcs",
    "url": "https://github.com/Cidaas/cidaas-sdk-php.git"
  }
]

2. Install sdk dependency using composer

composer require "cidaas/oauth2-cidaas:dev-master"

Integration of cidaas PHP SDK

Before you can start integrating this sdk, you need the following information:

Base communication provider

All communication is done using the php class Cidaas\OAuth2\Client\Provider\Cidaas (called provider in the following chapters).

<?php
$cidaas = new Cidaas('https://yourcidaasinstance.cidaas.de', 'client id', 'client secret', 'https://yourwebsite/redirectAfterLogin');
?>

It is a cidaas constructor class which initiates a cidaas client with necessary prerequisite parameters which are:

Prerequisites

  • Base URL - URL of cidaas server
  • Client ID - issued by cidaas to identify your application
  • Client secret - issued by cidaas to identify your application
  • Redirect URI - URI to redirect to after successful login

In addition to these required parameters, there are two optional constructor parameters:

1. handler: to interfere with the http connections being performed

2. debug: to enable debug mode

During construction, an http call is being performed to read base configuration data from the server.

cidaas PHP SDK can be integrated in two ways as per the users requirement.

1. Hosted login page

2. Custom login page

Integration via Hosted Login page:

In this process, cidaas provides all the UI for the login page. The user needs to make a call to the URL in the browser to redirect to it.

In order to integrate a hosted login page, you might just implement a simple button, which itself calls the login method when clicked.

<?php
    $provider->loginWithBrowser();
?>

After a successful login, the browser is redirected to your selected redirectUri (see Prerequisites). Using method loginCallback enables you to retrieve the authorization code for retrieving access and refresh tokens.

<?php
$parameters = $provider->loginCallback();
if (array_key_exists('code', $parameters)) {
    $loginResultCode = $parameters['code'];     
}
?>

Retrieve tokens after login

Using the login result code, you can then get your access token and refresh token using the method getAccessToken with GrantType AuthorizationCode.

<?php
    $accessTokenResponse = $provider->getAccessToken(GrantType::AuthorizationCode, $loginResultCode)->wait();
    $accessToken = $accessTokenResponse['access_token'];
    $refreshToken = $accessTokenResponse['refresh_token'];
?>

Retrieve an access token by refresh token

In order to get a new access token by a given refresh token, you can also use the method getAccessToken with GrantType RefreshToken.

<?php
    $accessToken = $provider->getAccessToken(GrantType::RefreshToken, '', $refreshToken)->then(function ($response) {
        return $response['access_token'];
    })->wait();
?>

Logout

In order to perform a logout, you need an access token. Using this token, you can perform a logout.

<?php
    $provider->logout($accessToken);
?>

Integration via Custome Login page:

In this approach, the users can create their login and registration pages and include cidaas in the authentication.

Building your own login page

You can build your own login page and use a direct login mechanism. In order to access the login method, you need to retrieve a requestId first.

<?php
    try {
        $loginResult = $provider->getRequestId('openid identities profile offline_access')->then(function ($requestId) {
            return $provider->loginWithCredentials($username, 'email', $password, $requestId);
        })->wait();
        $loginResultCode = $loginResult['data']['code'];
    } catch (ClientException $exception) {
        $errorBody = json_decode($exception->getResponse()->getBody(), true);
        $passwordErrorMessage = $errorBody['error']['error_description'];
    }
?>

Building your own registration page

In order to build your own registration page, you can retrieve required fields using getRegistrationSetup with requestId and locale.

<?php
    $registrationResponse = $provider->getRequestId()->then(function ($requestId) {
        return $provider->getRegistrationSetup($requestId, $locale);
    })->wait();
    $registrationFields = $registrationResponse['data'];
?>

Using the fields and another requestId, you can then register a new customer.

<?php
try {
    $registrationResponse = $provider->getRequestId()->then(function ($requestId) {
        return $provider->register($fields, $requestId);
    })->wait();
} catch (ClientException $exception) {
    $errorMessage = json_decode($exception->getResponse()->getBody())['error']['error'];
}
?>

Retrieve tokens after login

Using the login result code, you can then get your access token and refresh token using the method getAccessToken with GrantType AuthorizationCode.

<?php
    $accessTokenResponse = $provider->getAccessToken(GrantType::AuthorizationCode, $loginResultCode)->wait();
    $accessToken = $accessTokenResponse['access_token'];
    $refreshToken = $accessTokenResponse['refresh_token'];
?>

Retrieve an access token by refresh token

Retrieve an access token by refresh token In order to get a new access token by a given refresh token, you can also use the method getAccessToken with GrantType RefreshToken.

<?php
    $accessToken = $provider->getAccessToken(GrantType::RefreshToken, '', $refreshToken)->then(function ($response) {
        return $response['access_token'];
    })->wait();
?>

Logout

In order to perform a logout, you need an access token. Using this token, you can perform a logout.

<?php
    $provider->logout($accessToken);
?>

Please refer to https://docs.guzzlephp.org/en/stable/testing.html for further information.



results matching ""

    No results matching ""