Skip to content

Commit a8cd83f

Browse files
author
Luca Degasperi
committed
Moar tests and updated documentation
1 parent 62cd87c commit a8cd83f

File tree

3 files changed

+60
-32
lines changed

3 files changed

+60
-32
lines changed

README.md

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,7 @@ state=1234567890
8585
Route::get('/oauth/authorize', array('before' => 'check-authorization-params|auth', function()
8686
{
8787
// get the data from the check-authorization-params filter
88-
$params['client_id'] = Session::get('client_id');
89-
$params['client_details'] = Session::get('client_details');
90-
$params['redirect_uri'] = Session::get('redirect_uri');
91-
$params['response_type'] = Session::get('response_type');
92-
$params['scopes'] = Session::get('scopes');
93-
$params['state'] = Session::get('state');
88+
$params = Session::get('authorize-params');
9489

9590
// get the user id
9691
$params['user_id'] = Auth::user()->id;
@@ -104,46 +99,26 @@ Route::get('/oauth/authorize', array('before' => 'check-authorization-params|aut
10499
Route::post('/oauth/authorize', array('before' => 'check-authorization-params|auth|csrf', function()
105100
{
106101
// get the data from the check-authorization-params filter
107-
$params['client_id'] = Session::get('client_id');
108-
$params['client_details'] = Session::get('client_details');
109-
$params['redirect_uri'] = Session::get('redirect_uri');
110-
$params['response_type'] = Session::get('response_type');
111-
$params['scopes'] = Session::get('scopes');
112-
$params['state'] = Session::get('state');
102+
$params = Session::get('authorize-params')
113103

114104
// get the user id
115105
$params['user_id'] = Auth::user()->id;
116106

117107
// check if the user approved or denied the authorization request
118108
if (Input::get('approve') !== null) {
119109

120-
$code = AuthorizationServer::getGrantType('authorization_code')->newAuthoriseRequest('user', $params['user_id'], $params);
110+
$code = AuthorizationServer::newAuthorizeRequest('user', $params['user_id'], $params);
121111

122-
// not appropriate
123-
Session::flush();
112+
Session::forget('authorize-params');
124113

125-
return Redirect::to(
126-
AuthorizationServer::makeRedirect($params['redirect_uri'],
127-
array(
128-
'code' => $code,
129-
'state' => isset($params['state']) ? $params['state'] : ''
130-
)
131-
));
114+
return Redirect::to(AuthorizationServer::makeRedirectWithCode($code, $params));
132115
}
133116

134117
if (Input::get('deny') !== null) {
135118

136-
// just for demonstration purposes (you should flush the vars individually)
137-
Session::flush();
119+
Session::forget('authorize-params');
138120

139-
return Redirect::to(
140-
AuthorizationServer::makeRedirect($params['redirect_uri'],
141-
array(
142-
'error' => 'access_denied',
143-
'error_message' => AuthorizationServer::getExceptionMessage('access_denied'),
144-
'state' => isset($params['state']) ? $params['state'] : ''
145-
)
146-
));
121+
return Redirect::to(AuthorizationServer::makeRedirectWithError($params));
147122
}
148123
});
149124
```

src/LucaDegasperi/OAuth2Server/Proxies/AuthorizationServerProxy.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,33 @@ public function makeRedirect($uri, $params = array(), $queryDelimeter = '?')
3838
return RedirectUri::make($uri, $params, $queryDelimeter);
3939
}
4040

41+
public function makeRedirectWithCode($code, $params = array())
42+
{
43+
return $this->makeRedirect($params['redirect_uri'], array(
44+
'code' => $code,
45+
'state' => isset($params['state']) ? $params['state'] : '',
46+
));
47+
}
48+
49+
public function makeRedirectWithError($params = array())
50+
{
51+
return $this->makeRedirect($params['redirect_uri'], array(
52+
'error' => 'access_denied',
53+
'error_message' => $this->authServer->getExceptionMessage('access_denied'),
54+
'state' => isset($params['state']) ? $params['state'] : ''
55+
));
56+
}
57+
4158
public function checkAuthorizeParams()
4259
{
4360
return $this->authServer->getGrantType('authorization_code')->checkAuthoriseParams();
4461
}
4562

63+
public function newAuthorizeRequest($owner, $owner_id, $options)
64+
{
65+
return $this->authServer->getGrantType('authorization_code')->newAuthoriseRequest($owner, $owner_id, $options);
66+
}
67+
4668
public function performAccessTokenFlow()
4769
{
4870
try {
@@ -61,7 +83,9 @@ public function performAccessTokenFlow()
6183
// make this better in order to return the correct headers via the response object
6284
$headers = $this->authServer->getExceptionHttpHeaders($this->authServer->getExceptionType($e->getCode()));
6385
foreach ($headers as $header) {
86+
// @codeCoverageIgnoreStart
6487
header($header);
88+
// @codeCoverageIgnoreEnd
6589
}
6690

6791
} catch (Exception $e) {

tests/AuthorizationServerProxyTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,35 @@ public function test_make_redirect()
3636
$this->assertEquals('example?', $result);
3737
}
3838

39+
public function test_make_redirect_with_code()
40+
{
41+
$proxy = $this->getProxy($this->getMock());
42+
43+
$result = $proxy->makeRedirectWithCode('1234567890', array('redirect_uri' => 'example'));
44+
45+
$this->assertEquals('example?code=1234567890&state=', $result);
46+
47+
$result = $proxy->makeRedirectWithCode('1234567890', array('redirect_uri' => 'example', 'state' => 'random'));
48+
49+
$this->assertEquals('example?code=1234567890&state=random', $result);
50+
}
51+
52+
public function test_make_redirect_with_error()
53+
{
54+
$mock = $this->getMock();
55+
$mock->shouldReceive('getExceptionMessage')->twice()->andReturn('error_message');
56+
57+
$proxy = $this->getProxy($mock);
58+
59+
$result = $proxy->makeRedirectWithError(array('redirect_uri' => 'example'));
60+
61+
$this->assertEquals('example?error=access_denied&error_message=error_message&state=', $result);
62+
63+
$result = $proxy->makeRedirectWithError(array('redirect_uri' => 'example', 'state' => 'random'));
64+
65+
$this->assertEquals('example?error=access_denied&error_message=error_message&state=random', $result);
66+
}
67+
3968
public function test_check_authorize_params()
4069
{
4170
$mock = $this->getMock();

0 commit comments

Comments
 (0)