oauth2-server
![Software License][ico-license]
![Coverage Status][ico-scrutinizer]
![Total Downloads][ico-downloads], (*1)
This is where your description should go. Try and limit it to a paragraph or two, and maybe throw in a mention of what
PSRs you support to avoid any confusion with users and contributors., (*2)
Install
Via Composer, (*3)
``` bash
$ composer require borisguery/oauth2-server, (*4)
## Usage
``` php
class OAuht2Controller {
public function tokenAction(Request $request)
{
$sfPasswordGrantType = new SymfonySecurityPasswordGrantType(
$this->container->get('security.user_provider'),
$this->container->get('security.encoder_factory')->getEncoder(UserAccount::class)
);
$clientStorage = new InMemoryClientStorage();
$defaultClient = new Client(
'test',
null,
[],
['password']
);
$clientStorage->save($defaultClient);
$configuration = (new ResourceServerConfigurationBuilder())
->setAccessTokenStorage(new InMemoryAccessTokenStorage())
->setClientStorage($clientStorage)
->setRefreshStorage(new InMemoryRefreshTokenStorage())
->setAccessTokenGenerator(new Php7CSPRNGStringGenerator())
->addGrantType($sfPasswordGrantType)
->alwaysRequireAClient(true)
->alwaysGenerateARefreshToken(true)
->build()
->getResourceConfiguration()
;
$resourceServer = new ResourceServer($configuration);
$inputDataBag = SymfonyHttpFoundationRequestInputDataBagFactory::fromRequest($request);
$attemptResult = $resourceServer->requestAccessToken(
new TokenRequestAttempt($inputDataBag->getGrantType(), $inputDataBag)
);
if ($attemptResult instanceof SuccessfulTokenRequestAttemptResult) {
$statusCode = 200;
$response = [
'access_token' => $attemptResult->getAccessToken()->getToken(),
'expires_in' => $attemptResult->getAccessToken()->getExpiresIn(),
'token_type' => $attemptResult->getAccessToken()->getTokenType(),
'refresh_token' => $attemptResult->getRefreshToken()
? $attemptResult->getRefreshToken()->getToken()
: null,
];
} elseif ($attemptResult instanceof FailedTokenRequestAttemptResult) {
$statusCode = 400;
$response = [
'error' => (string) $attemptResult->getGrantDecision()->getError(),
'error_description' => $attemptResult->getGrantDecision()->getError()->getErrorDescription(),
'error_uri' => $attemptResult->getGrantDecision()->getError()->getErrorUri(),
];
}
return new Response(json_encode($response), $statusCode, ['Content-Type' => 'application/json']);
}
}
Testing
bash
$ composer test, (*5)
Contributing
Please see CONTRIBUTING for details., (*6)
Security
If you discover any security related issues, please email guery.b@gmail.com instead of using the issue tracker., (*7)
Credits
License
The MIT License (MIT). Please see License File for more information., (*8)