Completed
Push — master ( 6e52f0...d9a404 )
by Alexandre
02:29
created

AuthorizationRequest::getScope()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Alexandre
5
 * Date: 31/12/2017
6
 * Time: 00:29
7
 */
8
9
namespace OAuth2OLD\Endpoint\Server\Messages\Authorization;
10
use Psr\Http\Message\ServerRequestInterface;
11
12
13
/**
14
 * Class Request
15
 * @package OAuth2\Endpoints\Server\Messages\Authorization
16
 *
17
 * @see https://tools.ietf.org/html/rfc6749#section-4.1.1
18
 *
19
 * Authorization Request
20
 *
21
 *     The client constructs the request URI by adding the following
22
 * parameters to the query component of the authorization endpoint URI
23
 * using the "application/x-www-form-urlencoded" format, per Appendix B:
24
 *
25
 * response_type
26
 * REQUIRED.  Value MUST be set to "code".
27
 *
28
 * client_id
29
 * REQUIRED.  The client identifier as described in Section 2.2.
30
 *
31
 * redirect_uri
32
 * OPTIONAL.  As described in Section 3.1.2.
33
 *
34
 * scope
35
 * OPTIONAL.  The scope of the access request as described by
36
 * Section 3.3.
37
 *
38
 * state
39
 * RECOMMENDED.  An opaque value used by the client to maintain
40
 * state between the request and callback.  The authorization
41
 * server includes this value when redirecting the user-agent back
42
 * to the client.  The parameter SHOULD be used for preventing
43
 * cross-site request forgery as described in Section 10.12.
44
 *
45
 * The client directs the resource owner to the constructed URI using an
46
 * HTTP redirection response, or by other means available to it via the
47
 * user-agent.
48
 *
49
 * For example, the client directs the user-agent to make the following
50
 * HTTP request using TLS (with extra line breaks for display purposes
51
 * only):
52
 *
53
 * GET /authorize?response_type=code&client_id=s6BhdRkqt3&state=xyz
54
 * &redirect_uri=https%3A%2F%2Fclient%2Eexample%2Ecom%2Fcb HTTP/1.1
55
 * Host: server.example.com
56
 *
57
 * The authorization server validates the request to ensure that all
58
 * required parameters are present and valid.  If the request is valid,
59
 * the authorization server authenticates the resource owner and obtains
60
 * an authorization decision (by asking the resource owner or by
61
 * establishing approval via other means).
62
 *
63
 * When a decision is established, the authorization server directs the
64
 * user-agent to the provided client redirection URI using an HTTP
65
 * redirection response, or by other means available to it via the
66
 * user-agent.
67
 */
68
class AuthorizationRequest
69
{
70
    protected $responseType;
71
    protected $clientId;
72
    protected $redirectUri;
73
    protected $scope;
74
    protected $state;
75
76
    public function __construct($responseType, $clientId, $redirectUri = null, $scope = null, $state = null)
77
    {
78
        $this->responseType = $responseType;
79
        $this->clientId = $clientId;
80
        $this->redirectUri = $redirectUri;
81
        $this->scope = $scope;
82
        $this->state = $state;
83
    }
84
85
    static public function createFromServerRequest(ServerRequestInterface $request) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
86
        $responseType = $request->getQueryParams()['response_type'] ?? null;
87
        $clientId = $request->getQueryParams()['client_id'] ?? null;
88
        $redirectUri = $request->getQueryParams()['redirect_uri'] ?? null;
89
        $scope = $request->getQueryParams()['scope'] ?? null;
90
        $state = $request->getQueryParams()['state'] ?? null;
91
92
        return new self($responseType, $clientId, $redirectUri, $scope, $state);
93
    }
94
95
    /**
96
     * @throws \Exception
97
     */
98
    public function validate() {
99
        if(!$this->responseType) {
100
            throw new \Exception('Missing response_type parameter');
101
        }
102
103
        if(!$this->clientId) {
104
            throw new \Exception('Missing client_id parameter');
105
        }
106
        return true;
107
    }
108
109
    public function getResponseType()
110
    {
111
        return $this->responseType;
112
    }
113
114
    public function getClientId()
115
    {
116
        return $this->clientId;
117
    }
118
119
    public function getRedirectUri()
120
    {
121
        return $this->redirectUri;
122
    }
123
124
    public function getScope()
125
    {
126
        return $this->scope;
127
    }
128
129
    public function getState()
130
    {
131
        return $this->state;
132
    }
133
134
135
}