An OAuth authentication client for integrating 3rd party application with Eko App.
Client application must be registered with Eko first. These values, redirect_uri
, client_id
, client_secret
and eko_uri
, will be defined during registration process.
- Edit the
client_id
,client_secret
,redirect_uri
andeko_uri
in/examples/app.php
. - Go to
http://localhost/path/to/project/examples/app.php
via the browser
composer require ekoapp/eko-oauth-sdk
See examples/app.php
for the complete flow of usage.
use EkoApp\OAuth\EkoOAuthClient;
$client = new EkoOAuthClient();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);
$client->setEkoUri(EKO_URI);
To authenticate a user, the client application must redirect the user to the url below.
- Create a redirect url to Eko authentication endpoint
$_SESSION['state'] = $client->createState();
// DO NOT forget to store this state into the session to validate it when Eko redirect back to your endpoint
$authEndpointUrl = $client->createAuthenticateUrl($_SESSION['state']);
The client application must setup an endpoint which must match the predefined redirect_uri
. After authentication success or fail, Eko will redirect the user back to this redirect_uri
endpoint along with state
and code
(authentication code) as query parameters. The client application must validate the incoming state with the one previously store on the session. Then, the client application use the code
to retrieve access token and use the access token to retrieve user info. DO NOT use the code
if state validation fail.
- Validate state (if fail, exceptions will be thrown)
$client->validateState($_SESSION['state'], $state);
- Get token
$token = $client->requestToken($code);
// Retreiving values from access token
$accessToken = $token->getAccessToken();
$refreshToken = $token->getRefreshToken();
$tokenType = $token->getTokenType();
$expiresIn = $token->getExpiresIn();
$scopes = $token->getScopes();
$rawIdToken = $token->getRawIdToken();
$idToken = $token->getIdToken();
- Get ID token
$idToken = $token->getIdToken();
// Retreiving values from id token
$firstName = $idToken->firstname;
$lastName = $idToken->lastname;
$email = $idToken->email;
- Get user info
$userInfo = $client->requestUserInfo($token->getAccessToken());
// Retreiving values from user info
$userId = $userInfo->_id;
$firstName = $userInfo->firstname;
$lastName = $userInfo->lastname;
$email = $userInfo->email;
$networkId = $userInfo->networkId;
or a shortcut to retreive user info ...
- Get user info by
code
$userInfo = $client->requestUserInfoByCode($code);
$token = $client->requestTokenByRefreshToken($token->getRefreshToken());
Version | Changes |
---|---|
1.0.0 |
|
- Jura Boonnom - Initial work - jura-b