Completed
Push — master ( 88b001...24ab37 )
by Derek Stephen
10:51
created

AuthCodeController::authorizeAction()   B

Complexity

Conditions 3
Paths 9

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 30
ccs 0
cts 19
cp 0
rs 8.8571
c 1
b 0
f 0
cc 3
eloc 16
nc 9
nop 0
crap 12
1
<?php
2
3
namespace App\Controller;
4
5
use DateInterval;
6
use Del\Common\ContainerService;
7
use Exception;
8
use League\OAuth2\Server\AuthorizationServer;
9
use League\OAuth2\Server\Exception\OAuthServerException;
10
use League\OAuth2\Server\Grant\AuthCodeGrant;
11
use OAuth\User;
12
use Zend\Diactoros\Response;
13
use Zend\Diactoros\Stream;
14
15
class AuthCodeController extends OAuthController
16
{
17
    public function init()
18
    {
19
        parent::init();
20
        $container = ContainerService::getInstance()->getContainer();
21
        $authCodeRepository = $container['repository.AuthCode'];
22
        $refreshTokenRepository = $container['repository.RefreshToken'];
23
        $this->oauth2Server->enableGrantType(
24
            new AuthCodeGrant(
25
                $authCodeRepository,
26
                $refreshTokenRepository,
27
                new DateInterval('PT10M')
28
            ),
29
            new DateInterval('PT1H')
30
        );
31
    }
32
33
    /**
34
     *
35
     * @SWG\Get(
36
     *     path="/oauth2/authorize",
37
     *     @SWG\Response(response="200", description="An access token"),
38
     *     tags={"auth"},
39
     *     @SWG\Parameter(
40
     *         name="response_type",
41
     *         in="query",
42
     *         type="string",
43
     *         description="the type of response",
44
     *         required=true,
45
     *         default="code"
46
     *     ),
47
     *     @SWG\Parameter(
48
     *         name="client_id",
49
     *         in="query",
50
     *         type="string",
51
     *         description="the client identifier",
52
     *         required=true
53
     *     ),
54
     *     @SWG\Parameter(
55
     *         name="redirect_uri",
56
     *         in="query",
57
     *         type="string",
58
     *         description="where to send the response",
59
     *         required=false
60
     *     ),
61
     *     @SWG\Parameter(
62
     *         name="state",
63
     *         in="query",
64
     *         type="string",
65
     *         description="with a CSRF token. This parameter is optional but highly recommended.",
66
     *         required=false,
67
     *     )
68
     * )
69
     */
70
    public function authorizeAction()
71
    {
72
        /* @var AuthorizationServer $server */
73
        $server = $this->oauth2Server;
74
75
        $request = $this->getRequest();
76
        $response = new Response();
77
78
        try {
79
            // Validate the HTTP request and return an AuthorizationRequest object.
80
            // The auth request object can be serialized into a user's session
81
            $authRequest = $server->validateAuthorizationRequest($request);
82
            // Once the user has logged in set the user on the AuthorizationRequest
83
            $authRequest->setUser(new User());
84
            // Once the user has approved or denied the client update the status
85
            // (true = approved, false = denied)
86
            $authRequest->setAuthorizationApproved(true);
87
            // Return the HTTP redirect response
88
            $response = $server->completeAuthorizationRequest($authRequest, $response);
89
90
        } catch (OAuthServerException $exception) {
91
            $response = $exception->generateHttpResponse($response);
92
93
        } catch (Exception $exception) {
94
            $body = new Stream('php://temp', 'r+');
95
            $body->write($exception->getMessage());
96
            $response = $response->withStatus(500)->withBody($body);
97
        }
98
        $this->sendResponse($response);
99
    }
100
101
102
103
    /**
104
     * @SWG\Post(
105
     *     path="/oauth2/access-token",
106
     *     operationId="accessToken",
107
     *     @SWG\Response(response="200", description="An access token"),
108
     *     tags={"auth"},
109
     *     @SWG\Parameter(
110
     *         name="grant_type",
111
     *         in="body",
112
     *         type="string",
113
     *         description="the type of grant",
114
     *         required=true,
115
     *         default="authorization_code",
116
     *         @SWG\Schema(type="string")
117
     *     ),
118
     *     @SWG\Parameter(
119
     *         name="client_id",
120
     *         in="body",
121
     *         type="string",
122
     *         description="the client id",
123
     *         required=true,
124
     *         @SWG\Schema(type="string")
125
     *     ),
126
     *     @SWG\Parameter(
127
     *         name="client_secret",
128
     *         in="body",
129
     *         type="string",
130
     *         description="the client secret",
131
     *         required=true,
132
     *         @SWG\Schema(type="string")
133
     *     ),
134
     *     @SWG\Parameter(
135
     *         name="redirect_uri",
136
     *         in="body",
137
     *         type="string",
138
     *         description="with the same redirect URI the user was redirect back to",
139
     *         required=true,
140
     *         default="authorization_code",
141
     *         @SWG\Schema(type="string")
142
     *     ),
143
     *     @SWG\Parameter(
144
     *         name="code",
145
     *         in="body",
146
     *         type="string",
147
     *         description="with the authorization code from the query string",
148
     *         required=true,
149
     *         default="authorization_code",
150
     *         @SWG\Schema(type="string")
151
     *     ),
152
     * )
153
     */
154
    public function accessTokenAction()
155
    {
156
        /* @var AuthorizationServer $server */
157
        $server = $this->oauth2Server;
158
159
        $request = $this->getRequest();
160
        $response = new Response();
161
162
        try {
163
            // Try to respond to the access token request
164
            $response = $server->respondToAccessTokenRequest($request, $response);
165
        } catch (OAuthServerException $exception) {
166
            $response = $exception->generateHttpResponse($response);
167
        } catch (Exception $exception) {
168
            $body = $response->getBody();
169
            $body->write($exception->getMessage());
170
            $response = $response->withStatus(500)->withBody($body);
171
        }
172
        $this->sendResponse($response);
173
    }
174
}
175