1<?php
2/**
3 * @author      Alex Bilbie <hello@alexbilbie.com>
4 * @copyright   Copyright (c) Alex Bilbie
5 * @license     http://mit-license.org/
6 *
7 * @link        https://github.com/thephpleague/oauth2-server
8 */
9
10use League\OAuth2\Server\AuthorizationServer;
11use League\OAuth2\Server\Exception\OAuthServerException;
12use League\OAuth2\Server\Grant\ImplicitGrant;
13use OAuth2ServerExamples\Entities\UserEntity;
14use OAuth2ServerExamples\Repositories\AccessTokenRepository;
15use OAuth2ServerExamples\Repositories\ClientRepository;
16use OAuth2ServerExamples\Repositories\ScopeRepository;
17use Psr\Http\Message\ResponseInterface;
18use Psr\Http\Message\ServerRequestInterface;
19use Slim\App;
20use Zend\Diactoros\Stream;
21
22include __DIR__ . '/../vendor/autoload.php';
23
24$app = new App([
25    'settings'    => [
26        'displayErrorDetails' => true,
27    ],
28    AuthorizationServer::class => function () {
29        // Init our repositories
30        $clientRepository = new ClientRepository();
31        $scopeRepository = new ScopeRepository();
32        $accessTokenRepository = new AccessTokenRepository();
33
34        $privateKeyPath = 'file://' . __DIR__ . '/../private.key';
35
36        // Setup the authorization server
37        $server = new AuthorizationServer(
38            $clientRepository,
39            $accessTokenRepository,
40            $scopeRepository,
41            $privateKeyPath,
42            'lxZFUEsBCJ2Yb14IF2ygAHI5N4+ZAUXXaSeeJm6+twsUmIen'
43        );
44
45        // Enable the implicit grant on the server with a token TTL of 1 hour
46        $server->enableGrantType(new ImplicitGrant(new \DateInterval('PT1H')));
47
48        return $server;
49    },
50]);
51
52$app->get('/authorize', function (ServerRequestInterface $request, ResponseInterface $response) use ($app) {
53    /* @var \League\OAuth2\Server\AuthorizationServer $server */
54    $server = $app->getContainer()->get(AuthorizationServer::class);
55
56    try {
57        // Validate the HTTP request and return an AuthorizationRequest object.
58        // The auth request object can be serialized into a user's session
59        $authRequest = $server->validateAuthorizationRequest($request);
60
61        // Once the user has logged in set the user on the AuthorizationRequest
62        $authRequest->setUser(new UserEntity());
63
64        // Once the user has approved or denied the client update the status
65        // (true = approved, false = denied)
66        $authRequest->setAuthorizationApproved(true);
67
68        // Return the HTTP redirect response
69        return $server->completeAuthorizationRequest($authRequest, $response);
70    } catch (OAuthServerException $exception) {
71        return $exception->generateHttpResponse($response);
72    } catch (\Exception $exception) {
73        $body = new Stream('php://temp', 'r+');
74        $body->write($exception->getMessage());
75
76        return $response->withStatus(500)->withBody($body);
77    }
78});
79
80$app->run();
81