borisguery/oauth2-server

0.1 2016-04-04 10:04 UTC

This package is not auto-updated.

Last update: 2024-11-01 19:47:48 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

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.

Install

Via Composer

$ composer require borisguery/oauth2-server

Usage

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

$ composer test

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email guery.b@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.