Completed
Push — master ( d14457...da0e1e )
by Derek Stephen
25:25 queued 19:55
created

AuthCodeController::authorizeAction()   A

Complexity

Conditions 5
Paths 27

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 38
ccs 0
cts 26
cp 0
rs 9.2568
c 0
b 0
f 0
cc 5
nc 27
nop 0
crap 30
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\OAuthUser;
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
     *         default="testclient"
54
     *     ),
55
     *     @SWG\Parameter(
56
     *         name="redirect_uri",
57
     *         in="query",
58
     *         type="string",
59
     *         description="where to send the response",
60
     *         required=false
61
     *     ),
62
     *     @SWG\Parameter(
63
     *         name="state",
64
     *         in="query",
65
     *         type="string",
66
     *         description="with a CSRF token. This parameter is optional but highly recommended.",
67
     *         required=false,
68
     *     ),
69
     *     @SWG\Parameter(
70
     *         name="scope",
71
     *         in="query",
72
     *         type="string",
73
     *         description="allowed scopes, space separated",
74
     *         required=false,
75
     *     )
76
     * )
77
     */
78
    public function authorizeAction()
79
    {
80
        /* @var AuthorizationServer $server */
81
        $server = $this->oauth2Server;
82
83
        $request = $this->getRequest();
84
        $response = new Response();
85
86
        try {
87
            // Validate the HTTP request and return an AuthorizationRequest object.
88
            // The auth request object can be serialized into a user's session
89
            $authRequest = $server->validateAuthorizationRequest($request);
90
            // Once the user has logged in set the user on the AuthorizationRequest
91
            $authRequest->setUser(new OAuthUser());
92
            // Once the user has approved or denied the client update the status
93
            // (true = approved, false = denied)
94
            $authRequest->setAuthorizationApproved(true);
95
            // Return the HTTP redirect response
96
            $response = $server->completeAuthorizationRequest($authRequest, $response);
97
98
        } catch (OAuthServerException $e) {
99
            $response = $e->generateHttpResponse($response);
100
101
        } catch (Exception $e) {
102
            $body = new Stream('php://temp', 'r+');
103
            $body->write($e->getMessage());
104
            $response = $response->withStatus(500)->withBody($body);
105
        }
106
107
        $redirectUri = $response->getHeader('Location');
108
        if (!empty($redirectUri)) {
109
            if (substr($redirectUri[0], 0, 1) == '?') {
110
                $uri = str_replace('?', '', $redirectUri[0]);
111
                parse_str($uri, $vars);
112
                $this->sendJsonResponse($vars);
113
            }
114
        } else {
115
            $this->sendResponse($response);
116
        }
117
    }
118
119
120
121
    /**
122
     * @SWG\Post(
123
     *     path="/oauth2/access-token",
124
     *     operationId="accessToken",
125
     *     @SWG\Response(response="200", description="An access token"),
126
     *     tags={"auth"},
127
     *     @SWG\Parameter(
128
     *         name="grant_type",
129
     *         in="formData",
130
     *         type="string",
131
     *         description="the type of grant",
132
     *         required=true,
133
     *         default="authorization_code",
134
     *     ),
135
     *     @SWG\Parameter(
136
     *         name="client_id",
137
     *         in="formData",
138
     *         type="string",
139
     *         description="the client id",
140
     *         required=true,
141
     *         default="testclient"
142
     *     ),
143
     *     @SWG\Parameter(
144
     *         name="client_secret",
145
     *         in="formData",
146
     *         type="string",
147
     *         description="the client secret",
148
     *         required=false
149
     *     ),
150
     *     @SWG\Parameter(
151
     *         name="redirect_uri",
152
     *         in="formData",
153
     *         type="string",
154
     *         description="with the same redirect URI the user was redirect back to",
155
     *         required=false,
156
     *         default="authorization_code"
157
     *     ),
158
     *     @SWG\Parameter(
159
     *         name="code",
160
     *         in="formData",
161
     *         type="string",
162
     *         description="with the authorization code from the query string",
163
     *         required=true,
164
     *         default="pastehere"
165
     *     ),
166
     * )
167
     */
168
    public function accessTokenAction()
169
    {
170
        /* @var AuthorizationServer $server */
171
        $server = $this->oauth2Server;
172
173
        $request = $this->getRequest();
174
        $response = new Response();
175
176
        try {
177
            // Try to respond to the access token request
178
            $response = $server->respondToAccessTokenRequest($request, $response);
179
        } catch (OAuthServerException $exception) {
180
            $response = $exception->generateHttpResponse($response);
181
        } catch (Exception $exception) {
182
            $body = $response->getBody();
183
            $body->write($exception->getMessage());
184
            $response = $response->withStatus(500)->withBody($body);
185
        }
186
        $this->sendResponse($response);
187
    }
188
}
189