alpa / oauth_manager
Authorization/authentication via oauth services
v0.0.1
2022-05-26 16:10 UTC
Requires
- league/oauth2-client: ^2.6
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-10-26 22:06:48 UTC
README
The manager manages the authorization of Oauth2 clients, which are implemented through the league/oauth2-client component.
<?php namespace Alpa\Icms\Oauth; // Authorization $providerData=new PoviderData([ // data from data base. 'alias'=>'MyOauthServer', 'class'=>'MyVendor\\MyOauthProvider',// Authorization provider class 'clientId'=>'your client id ', 'clientSecret'=>'your secret token', 'redirectUri'=>'redirect uri' ]); $options=[ 'code'=>'code from your client' //authorization code. Should be sent by the client. ]; $pm=new ProviderManager($providerData,$options); $provider=$pm->getProvider();// result object ProviderAbstract see https://oauth2-client.thephpleague.com/usage/ $token=$pm->getAccessToken();// // result object AccessTokenInterface see https://oauth2-client.thephpleague.com/usage/ $flip_fields=[ 'name'=>'flip_name' // From the provider, the flip_name field data will come, and fit into the 'name' field. ]; $data=$pm->getResourceOwner($flip_fields)->toArray();//additional data that is passed in the access token during authorization will also be reflected. $token_data=$token->jsonSerialize(); // optional $additional_data=$token->getValues(); $token_data=array_merge($token_data,$additional_data); // saving $token_data
<?php namespace Alpa\Icms\Oauth; // Authentication $providerData=new PoviderData([ // settings data from data base. 'alias'=>'MyOauthServer', 'class'=>'MyVendor\\MyOauthProvider',// Authorization provider class 'clientId'=>'your client id ', 'clientSecret'=>'your secret token', 'redirectUri'=>'redirect uri' ]); $pm=new ProviderManager($providerData); $token=$pm->newAccessToken([ // data from data base. 'access_token'=>' access token for your client', 'resource_owner_id'=>'providers client id', 'refresh_token'=>'token to refresh the access token', 'expires'=>'(integer) the time at which the access token will expire',// ]); $pm->setAccessToken($token); $flip_fields=[ 'name'=>'flip_name' // From the provider, the flip_name field data will come, and fit into the 'name' field. ]; $data=$pm->getResourceOwner($flip_fields)->toArray(); //saving data
<?php $check=$pm->refreshAccessToken(); //replace token if time is up //$check=$pm->refreshAccessToken(true); //force token refresh if($check){ $newToken=$pm->getAccessToken(); $token_data=$newToken->jsonSerialize(); // optional $additional_data=$newToken->getValues(); $token_data=array_merge($token_data,$additional_data); // saving $token_data }
// For other code, see https://oauth2-client.thephpleague.com/usage/ //Example $authorizationUrl = $provider->getAuthorizationUrl();