This repository has been archived by the owner on Nov 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
OAuthController.php
202 lines (177 loc) · 4.98 KB
/
OAuthController.php
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
200
201
202
<?php
/**
* CakePHP OAuth Server Plugin
*
* This is an example controller providing the necessary endpoints
*
* @author Thom Seddon <thom@seddonmedia.co.uk>
* @see https://github.com/thomseddon/cakephp-oauth-server
*
*/
App::uses('OAuthAppController', 'OAuth.Controller');
/**
* OAuthController
*
*/
class OAuthController extends OAuthAppController {
public $components = array('OAuth.OAuth', 'Auth', 'Session', 'Security');
public $uses = array('Users');
public $helpers = array('Form');
private $blackHoled = false;
/**
* beforeFilter
*
*/
public function beforeFilter() {
parent::beforeFilter();
$this->OAuth->authenticate = array('fields' => array('username' => 'email'));
$this->Auth->allow($this->OAuth->allowedActions);
$this->Security->blackHoleCallback = 'blackHole';
}
/**
* Example Authorize Endpoint
*
* Send users here first for authorization_code grant mechanism
*
* Required params (GET or POST):
* - response_type = code
* - client_id
* - redirect_url
*
*/
public function authorize() {
if (!$this->Auth->loggedIn()) {
$this->redirect(array('action' => 'login', '?' => $this->request->query));
}
if ($this->request->is('post')) {
$this->validateRequest();
$userId = $this->Auth->user('id');
if ($this->Session->check('OAuth.logout')) {
$this->Auth->logout();
$this->Session->delete('OAuth.logout');
}
//Did they accept the form? Adjust accordingly
$accepted = $this->request->data['accept'] == 'Yep';
try {
$this->OAuth->finishClientAuthorization($accepted, $userId, $this->request->data['Authorize']);
} catch (OAuth2RedirectException $e) {
$e->sendHttpResponse();
}
}
// Clickjacking prevention (supported by IE8+, FF3.6.9+, Opera10.5+, Safari4+, Chrome 4.1.249.1042+)
$this->response->header('X-Frame-Options: DENY');
if ($this->Session->check('OAuth.params')) {
$OAuthParams = $this->Session->read('OAuth.params');
$this->Session->delete('OAuth.params');
} else {
try {
$OAuthParams = $this->OAuth->getAuthorizeParams();
} catch (Exception $e){
$e->sendHttpResponse();
}
}
$this->set(compact('OAuthParams'));
}
/**
* Example Login Action
*
* Users must authorize themselves before granting the app authorization
* Allows login state to be maintained after authorization
*
*/
public function login () {
$OAuthParams = $this->OAuth->getAuthorizeParams();
if ($this->request->is('post')) {
$this->validateRequest();
//Attempted login
if ($this->Auth->login()) {
//Write this to session so we can log them out after authenticating
$this->Session->write('OAuth.logout', true);
//Write the auth params to the session for later
$this->Session->write('OAuth.params', $OAuthParams);
//Off we go
$this->redirect(array('action' => 'authorize'));
} else {
$this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
}
}
$this->set(compact('OAuthParams'));
}
/**
* Example Token Endpoint - this is where clients can retrieve an access token
*
* Grant types and parameters:
* 1) authorization_code - exchange code for token
* - code
* - client_id
* - client_secret
*
* 2) refresh_token - exchange refresh_token for token
* - refresh_token
* - client_id
* - client_secret
*
* 3) password - exchange raw details for token
* - username
* - password
* - client_id
* - client_secret
*
*/
public function token() {
$this->autoRender = false;
try {
$this->OAuth->grantAccessToken();
} catch (OAuth2ServerException $e) {
$e->sendHttpResponse();
}
}
/**
* Quick and dirty example implementation for protecetd resource
*
* User accesible via $this->OAuth->user();
* Single fields avaliable via $this->OAuth->user("id");
*
*/
public function userinfo() {
$this->layout = null;
$user = $this->OAuth->user();
$this->set(compact('user'));
}
/**
* Blackhold callback
*
* OAuth requests will fail postValidation, so rather than disabling it completely
* if the request does fail this check we store it in $this->blackHoled and then
* when handling our forms we can use $this->validateRequest() to check if there
* were any errors and handle them with an exception.
* Requests that fail for reasons other than postValidation are handled here immediately
* using the best guess for if it was a form or OAuth
*
* @param string $type
*/
public function blackHole($type) {
$this->blackHoled = $type;
if ($type != 'auth') {
if (isset($this->request->data['_Token'])) {
//Probably our form
$this->validateRequest();
} else {
//Probably OAuth
$e = new OAuth2ServerException(OAuth2::HTTP_BAD_REQUEST, OAuth2::ERROR_INVALID_REQUEST, 'Request Invalid.');
$e->sendHttpResponse();
}
}
}
/**
* Check for any Security blackhole errors
*
* @throws BadRequestException
*/
private function validateRequest() {
if ($this->blackHoled) {
//Has been blackholed before - naughty
throw new BadRequestException(__d('OAuth', 'The request has been black-holed'));
}
}
}