Repository navigation
Expand file tree
/
Copy pathtoken.php
More file actions
199 lines (169 loc) · 6.09 KB
/
Copy pathtoken.php
File metadata and controls
199 lines (169 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
* Controller oAuth2 Token
*
* @package Gleez\oAuth2
* @author Gleez Team
* @version 1.0.0
* @copyright (c) 2011-2014 Gleez Technologies
* @license http://gleezcms.org/license Gleez CMS License
*/
class Controller_Token extends Controller
{
protected $accessToken;
protected $grantTypes;
protected $clientAssertionType;
protected $scopeUtil;
protected $config;
private $client;
private $client_id;
private $clientData;
/**
* The before() method is called before controller action
*/
public function before()
{
parent::before();
$this->auto_render = FALSE;
// Load the oauth2 config
$this->config = Config::load('oauth2')->as_array();
// create array of supported grant types
$this->grantTypes = array(
'authorization_code' => new Oauth2_GrantType_AuthorizationCode($this->config),
'client_credentials' => new Oauth2_GrantType_ClientCredentials($this->config, TRUE),
'refresh_token' => new Oauth2_GrantType_RefreshToken($this->config),
'password' => new Oauth2_GrantType_UserCredentials($this->config),
);
}
public function action_index()
{
try
{
if ($token = $this->grantAccessToken()) {
// @see http://tools.ietf.org/html/rfc6749#section-5.1
// server MUST disable caching in headers when tokens are involved
$this->response->status(200);
$this->response->headers(array('Cache-Control' => 'no-store', 'Pragma' => 'no-cache'));
$this->response->headers('content-type', 'application/json; charset='.Kohana::$charset);
$this->response->body( JSON::encode($token));
return;
}
}
catch(Oauth2_Exception $e)
{
// Throw an exception because there was a problem with the client's request
$response = array(
'error' => $e->getError(),
'error_description' => $e->getMessage()
);
$this->response->status($e->getCode());
$this->response->headers(array('Cache-Control' => 'no-store', 'Pragma' => 'no-cache'));
$this->response->headers('content-type', 'application/json; charset='.Kohana::$charset);
$this->response->body(json_encode($response));
return;
}
catch (Exception $e)
{
/**
* Something went wrong!
*
* Throw an error when a non-library specific exception has been thrown
*
* You should probably show a nice error page :)
*
* Do NOT redirect the user back to the client.
*/
throw HTTP_Exception::factory(500, $e->getMessage());
}
}
/**
* Grant or deny a requested access token.
* This would be called from the "/token" endpoint as defined in the spec.
* You can call your endpoint whatever you want.
*
* @see http://tools.ietf.org/html/rfc6749#section-4
* @see http://tools.ietf.org/html/rfc6749#section-10.6
* @see http://tools.ietf.org/html/rfc6749#section-4.1.3
*
* @ingroup oauth2_section_4
*/
protected function grantAccessToken()
{
if (strtolower($this->request->method()) != 'post')
{
$this->response->headers(array('Allow' => 'POST'));
throw Oauth2_Exception::factory(405, 'invalid_request', 'The request method must be POST when requesting an access token', NULL, '#section-3.2');
return FALSE;
}
/* Determine grant type from request
* and validate the request for that grant type
*/
if (!$grantTypeIdentifier = $this->request->post('grant_type')) {
throw Oauth2_Exception::factory(400, 'invalid_request', 'The grant type was not specified in the request');
return FALSE;
}
if (!isset($this->grantTypes[$grantTypeIdentifier])) {
/* TODO: If this is an OAuth2 supported grant type that we have chosen not to implement, throw a 501 Not Implemented instead */
throw Oauth2_Exception::factory(400, 'unsupported_grant_type', sprintf('Grant type "%s" not supported', $grantTypeIdentifier));
return null;
}
$grantType = $this->grantTypes[$grantTypeIdentifier];
/* Retrieve the client information from the request
* ClientCredentials allow for grant types which also assert the client data
* in which case ClientCredentials is handled in the validateRequest method
*
* @see OAuth2\GrantType\ClientCredentials
*/
if (!$grantType instanceof Oauth2_GrantType_ClientCredentials)
{
$check = new Oauth2_GrantType_ClientCredentials($this->config, FALSE);
if ( ! $check->validateRequest($this->request, $this->response) ) {
return null;
}
$clientId = $check->getClientId();
}
/* Retrieve the grant type information from the request
* The GrantTypeInterface object handles all validation
* If the object is an instance of Oauth2_GrantType_ClientCredentials,
* That logic is handled here as well
*/
if ( ! $grantType->validateRequest($this->request, $this->response) ) {
return null;
}
if ($grantType instanceof Oauth2_GrantType_ClientCredentials)
{
$clientId = $grantType->getClientId();
}
else
{
// validate the Client ID (if applicable)
if (!is_null($storedClientId = $grantType->getClientId()) && $storedClientId != $clientId) {
throw Oauth2_Exception::factory(400, 'invalid_grant', sprintf('%s doesn\'t exist or is invalid for the client', $grantTypeIdentifier));
return null;
}
}
$requestedScope = NULL;
/*
* Validate the scope of the token
* If the grant type returns a value for the scope,
* as is the case with the "Authorization Code" grant type,
* this value must be verified with the scope being requested
*/
/* $availableScope = $grantType->getScope();
if (!$requestedScope = $this->getScopeFromRequest($request)) {
if (!$availableScope) {
if (false === $defaultScope = $this->getDefaultScope($clientId)) {
throw Oauth2_Exception::factory(400, 'invalid_scope', 'This application requires you specify a scope parameter');
return null;
}
}
$requestedScope = $availableScope ? $availableScope : $defaultScope;
}
if (($requestedScope && !$this->scopeExists($requestedScope, $clientId))
|| ($availableScope && !$this->checkScope($requestedScope, $availableScope))) {
throw Oauth2_Exception::factory(400, 'invalid_scope', 'An unsupported scope was requested');
return null;
}*/
return $grantType->createAccessToken($clientId, $grantType->getUserId(), $requestedScope);
}
}