Authorize::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 20
rs 9.2
cc 4
eloc 13
nc 3
nop 4
1
<?php
2
3
namespace Chadicus\Slim\OAuth2\Routes;
4
5
use Chadicus\Slim\OAuth2\Http;
6
use Psr\Http\Message\ServerRequestInterface;
7
use Psr\Http\Message\ResponseInterface;
8
use OAuth2;
9
10
/**
11
 * Slim route for /authorization endpoint.
12
 */
13
final class Authorize implements RouteCallbackInterface
14
{
15
    const ROUTE = '/authorize';
16
17
    /**
18
     * The slim framework view helper.
19
     *
20
     * @var object
21
     */
22
    private $view;
23
24
    /**
25
     * The oauth2 server imstance.
26
     *
27
     * @var OAuth2\Server
28
     */
29
    private $server;
30
31
    /**
32
     * The template for /authorize
33
     *
34
     * @var string
35
     */
36
    private $template;
37
38
    /**
39
     * Extracts user_id from the incoming request.
40
     *
41
     * @var UserIdProviderInterface
42
     */
43
    private $userIdProvider;
44
45
    /**
46
     * Construct a new instance of Authorize.
47
     *
48
     * @param OAuth2\Server           $server         The oauth2 server imstance.
49
     * @param object                  $view           The slim framework view helper.
50
     * @param string                  $template       The template for /authorize.
51
     * @param UserIdProviderInterface $userIdProvider Object to extract a user_id based on the incoming request.
52
     *
53
     * @throws \InvalidArgumentException Thrown if $view is not an object implementing a render method.
54
     */
55
    public function __construct(
56
        OAuth2\Server $server,
57
        $view,
58
        $template = '/authorize.phtml',
59
        UserIdProviderInterface $userIdProvider = null
60
    ) {
61
        if (!is_object($view) || !method_exists($view, 'render')) {
62
            throw new \InvalidArgumentException('$view must implement a render() method');
63
        }
64
65
        $this->server = $server;
66
        $this->view = $view;
67
        $this->template = $template;
68
69
        if ($userIdProvider == null) {
70
            $userIdProvider = new UserIdProvider();
71
        }
72
73
        $this->userIdProvider = $userIdProvider;
74
    }
75
76
    /**
77
     * Invoke this route callback.
78
     *
79
     * @param ServerRequestInterface $request   Represents the current HTTP request.
80
     * @param ResponseInterface      $response  Represents the current HTTP response.
81
     * @param array                  $arguments Values for the current route’s named placeholders.
82
     *
83
     * @return ResponseInterface
84
     */
85
    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $arguments = [])
86
    {
87
        $oauth2Request = Http\RequestBridge::toOAuth2($request);
88
        $oauth2Response = new OAuth2\Response();
89
        if (!$this->server->validateAuthorizeRequest($oauth2Request, $oauth2Response)) {
90
            return Http\ResponseBridge::fromOAuth2($oauth2Response);
91
        }
92
93
        $authorized = $oauth2Request->request('authorized');
94
        if (empty($authorized)) {
95
            $response = Http\ResponseBridge::fromOAuth2($oauth2Response);
96
            $this->view->render($response, $this->template, ['client_id' => $oauth2Request->query('client_id')]);
97
            return $response->withHeader('Content-Type', 'text/html');
98
        }
99
100
        $this->server->handleAuthorizeRequest(
101
            $oauth2Request,
102
            $oauth2Response,
103
            $authorized === 'yes',
104
            $this->userIdProvider->getUserId($request, $arguments)
105
        );
106
107
        return Http\ResponseBridge::fromOAuth2($oauth2Response);
108
    }
109
}
110