Skip to content

Commit 7741df0

Browse files
author
Luca Degasperi
committed
Added a proxy to extend the authorization server class easily
1 parent b016cce commit 7741df0

7 files changed

+184
-75
lines changed
Lines changed: 0 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,13 @@
11
<?php namespace LucaDegasperi\OAuth2Server\Facades;
22

33
use Illuminate\Support\Facades\Facade;
4-
use League\OAuth2\Server\Util\RedirectUri;
5-
use League\OAuth2\Server\Exception\ClientException;
6-
use Exception;
7-
use Response;
84

95
class AuthorizationServerFacade extends Facade {
106

117
/**
128
* @codeCoverageIgnore
139
*/
1410
protected static function getFacadeAccessor() { return 'oauth2.authorization-server'; }
15-
16-
public static function makeRedirect($uri, $params = array(), $queryDelimeter = '?')
17-
{
18-
return RedirectUri::make($uri, $params, $queryDelimeter);
19-
}
20-
21-
public static function performAccessTokenFlow()
22-
{
23-
try {
24-
25-
// Tell the auth server to issue an access token
26-
$response = self::issueAccessToken();
27-
28-
} catch (ClientException $e) {
29-
30-
// Throw an exception because there was a problem with the client's request
31-
$response = array(
32-
'error' => self::getExceptionType($e->getCode()),
33-
'error_description' => $e->getMessage()
34-
);
35-
36-
$headers = self::getExceptionHttpHeaders(self::getExceptionType($e->getCode()));
37-
foreach ($headers as $header) {
38-
header($header);
39-
}
40-
41-
} catch (Exception $e) {
42-
43-
// Throw an error when a non-library specific exception has been thrown
44-
$response = array(
45-
'error' => 'undefined_error',
46-
'error_description' => $e->getMessage()
47-
);
48-
}
49-
50-
return Response::json($response);
51-
}
5211
}
5312

5413

src/LucaDegasperi/OAuth2Server/Filters/CheckAuthorizationParamsFilter.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,29 @@
33
use AuthorizationServer;
44
use Response;
55
use Session;
6+
use League\OAuth2\Server\Exception\ClientException;
7+
use Exception;
68

79
class CheckAuthorizationParamsFilter {
810

911
public function filter($route, $request, $scope = null)
1012
{
1113
try {
1214

13-
$params = AuthorizationServer::getGrantType('authorization_code')->checkAuthoriseParams();
15+
$params = AuthorizationServer::checkAuthorizeParams();
1416

15-
Session::put('client_id', $params['client_id']);
16-
Session::put('client_details', $params['client_details']);
17-
Session::put('redirect_uri', $params['redirect_uri']);
18-
Session::put('response_type', $params['response_type']);
19-
Session::put('scopes', $params['scopes']);
20-
Session::put('state', $params['state']);
17+
Session::put('authorize-params', $params);
2118

2219

23-
} catch (\League\OAuth2\Server\Exception\ClientException $e) {
20+
} catch (ClientException $e) {
2421

2522
return Response::json(array(
2623
'status' => 400,
2724
'error' => 'bad_request',
2825
'error_message' => $e->getMessage(),
2926
), 400);
3027

31-
} catch (\Exception $e) {
28+
} catch (Exception $e) {
3229

3330
return Response::json(array(
3431
'status' => 500,

src/LucaDegasperi/OAuth2Server/OAuth2ServerServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php namespace LucaDegasperi\OAuth2Server;
22

33
use Illuminate\Support\ServiceProvider;
4+
use LucaDegasperi\OAuth2Server\Proxies\AuthorizationServerProxy;
45

56
class OAuth2ServerServiceProvider extends ServiceProvider {
67

@@ -73,7 +74,7 @@ public function register()
7374

7475
$server->setAccessTokenTTL($config['access_token_ttl']);
7576

76-
return $server;
77+
return new AuthorizationServerProxy($server);
7778

7879
});
7980

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php namespace LucaDegasperi\OAuth2Server\Proxies;
2+
3+
use League\OAuth2\Server\Authorization as Authorization;
4+
use League\OAuth2\Server\Util\RedirectUri;
5+
use League\OAuth2\Server\Exception\ClientException;
6+
use Exception;
7+
use Response;
8+
9+
class AuthorizationServerProxy {
10+
11+
protected $authServer;
12+
13+
public function __construct(Authorization $authServer)
14+
{
15+
$this->authServer = $authServer;
16+
}
17+
18+
public function __call($method, $args)
19+
{
20+
switch (count($args)) {
21+
case 0:
22+
return $this->authServer->$method();
23+
case 1:
24+
return $this->authServer->$method($args[0]);
25+
case 2:
26+
return $this->authServer->$method($args[0], $args[1]);
27+
case 3:
28+
return $this->authServer->$method($args[0], $args[1], $args[2]);
29+
case 4:
30+
return $this->authServer->$method($args[0], $args[1], $args[2], $args[3]);
31+
default:
32+
return call_user_func_array(array($this->authServer, $method), $args);
33+
}
34+
}
35+
36+
public function makeRedirect($uri, $params = array(), $queryDelimeter = '?')
37+
{
38+
return RedirectUri::make($uri, $params, $queryDelimeter);
39+
}
40+
41+
public function checkAuthorizeParams()
42+
{
43+
return $this->authServer->getGrantType('authorization_code')->checkAuthoriseParams();
44+
}
45+
46+
public function performAccessTokenFlow()
47+
{
48+
try {
49+
50+
// Tell the auth server to issue an access token
51+
$response = $this->authServer->issueAccessToken();
52+
53+
} catch (ClientException $e) {
54+
55+
// Throw an exception because there was a problem with the client's request
56+
$response = array(
57+
'error' => $this->authServer->getExceptionType($e->getCode()),
58+
'error_description' => $e->getMessage()
59+
);
60+
61+
// make this better in order to return the correct headers via the response object
62+
$headers = $this->authServer->getExceptionHttpHeaders($this->authServer->getExceptionType($e->getCode()));
63+
foreach ($headers as $header) {
64+
header($header);
65+
}
66+
67+
} catch (Exception $e) {
68+
69+
// Throw an error when a non-library specific exception has been thrown
70+
$response = array(
71+
'error' => 'undefined_error',
72+
'error_description' => $e->getMessage()
73+
);
74+
75+
return Response::json($response, 500);
76+
}
77+
78+
return Response::json($response);
79+
}
80+
81+
}

tests/AuthorizationServerFacadeTest.php

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
3+
use \Mockery as m;
4+
use LucaDegasperi\OAuth2Server\Proxies\AuthorizationServerProxy;
5+
6+
class AuthorizationServerProxyTest extends TestCase {
7+
8+
public function getProxy($mock)
9+
{
10+
return new AuthorizationServerProxy($mock);
11+
}
12+
13+
public function getMock()
14+
{
15+
return m::mock('League\OAuth2\Server\Authorization');
16+
}
17+
18+
public function getStub()
19+
{
20+
return array(
21+
'client_id' => 1,
22+
'client_details' => 'foo',
23+
'redirect_uri' => 'http://www.example.com/',
24+
'response_type' => 'code',
25+
'scopes' => 'scope',
26+
'state' => '123456789',
27+
);
28+
}
29+
30+
public function test_make_redirect()
31+
{
32+
$proxy = $this->getProxy($this->getMock());
33+
34+
$result = $proxy->makeRedirect('example');
35+
36+
$this->assertEquals('example?', $result);
37+
}
38+
39+
public function test_check_authorize_params()
40+
{
41+
$mock = $this->getMock();
42+
$mock->shouldReceive('getGrantType->checkAuthoriseParams')->andReturn($this->getStub());
43+
44+
$response = $this->getProxy($mock)->checkAuthorizeParams();
45+
46+
$this->assertEquals($this->getStub(), $response);
47+
}
48+
49+
public function test_access_token_correctly_issued()
50+
{
51+
$mock = $this->getMock();
52+
$mock->shouldReceive('issueAccessToken')->once()->andReturn(array('foo' => 'bar'));
53+
54+
$response = $this->getProxy($mock)->performAccessTokenFlow();
55+
56+
$this->assertEquals('{"foo":"bar"}', $response->getContent());
57+
$this->assertTrue($response instanceof Illuminate\Http\JsonResponse);
58+
$this->assertTrue($response->isOk());
59+
}
60+
61+
public function test_access_token_with_client_error()
62+
{
63+
$mock = $this->getMock();
64+
$mock->shouldReceive('issueAccessToken')->once()->andThrow(new League\OAuth2\Server\Exception\ClientException('client exception'));
65+
$mock->shouldReceive('getExceptionType')->twice()->andReturn('foo');
66+
$mock->shouldReceive('getExceptionHttpHeaders')->once()->andReturn(array());
67+
68+
$response = $this->getProxy($mock)->performAccessTokenFlow();
69+
70+
$this->assertTrue($response instanceof Illuminate\Http\JsonResponse);
71+
$this->assertTrue($response->isOk());
72+
73+
}
74+
75+
public function test_access_token_with_generic_error()
76+
{
77+
$mock = $this->getMock();
78+
$mock->shouldReceive('issueAccessToken')->once()->andThrow(new Exception('internal server error'));
79+
80+
$response = $this->getProxy($mock)->performAccessTokenFlow();
81+
82+
$this->assertTrue($response instanceof Illuminate\Http\JsonResponse);
83+
$this->assertTrue($response->isServerError());
84+
85+
}
86+
87+
public function tearDown() {
88+
m::close();
89+
}
90+
91+
}

tests/CheckAuthorizationParamsFilterTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ public function test_with_valid_params()
2525
{
2626
$stub = $this->getStub();
2727

28-
AuthorizationServer::shouldReceive('getGrantType->checkAuthoriseParams')
28+
AuthorizationServer::shouldReceive('checkAuthorizeParams')
2929
->once()
3030
->andReturn($stub);
3131

32-
Session::shouldReceive('put')->times(6);
32+
Session::shouldReceive('put')->once();
3333

3434
$response = $this->getFilter()->filter('','', null);
3535

@@ -39,7 +39,7 @@ public function test_with_valid_params()
3939
public function test_with_invalid_valid_params()
4040
{
4141

42-
AuthorizationServer::shouldReceive('getGrantType->checkAuthoriseParams')
42+
AuthorizationServer::shouldReceive('checkAuthorizeParams')
4343
->once()
4444
->andThrow(new \League\OAuth2\Server\Exception\ClientException('Invalid Request'));
4545

@@ -52,7 +52,7 @@ public function test_with_invalid_valid_params()
5252
public function test_with_server_error()
5353
{
5454

55-
AuthorizationServer::shouldReceive('getGrantType->checkAuthoriseParams')
55+
AuthorizationServer::shouldReceive('checkAuthorizeParams')
5656
->once()
5757
->andThrow(new Exception('Internal Server Error'));
5858

0 commit comments

Comments
 (0)