Skip to content

Commit 2aa6cf9

Browse files
Spomkyalanbem
Spomky
authored andcommittedFeb 9, 2014
CS fixes
1 parent ffb78f7 commit 2aa6cf9

40 files changed

+4353
-3962
lines changed
 

‎lib/OAuth2/IOAuth2GrantClient.php

+11-14
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,34 @@
55
use OAuth2\Model\IOAuth2Client;
66

77
/**
8-
* Storage engines that support the "Client Credentials"
9-
* grant type should implement this interface
8+
* Storage engines that support the "Client Credentials" grant type should implement this interface
109
*
1110
* @author Dave Rochwerger <catch.dave@gmail.com>
12-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.4
11+
*
12+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.4
1313
*/
14-
interface IOAuth2GrantClient extends IOAuth2Storage {
15-
14+
interface IOAuth2GrantClient extends IOAuth2Storage
15+
{
1616
/**
1717
* Required for OAuth2::GRANT_TYPE_CLIENT_CREDENTIALS.
1818
*
19-
* @param IOAuth2Client $client
20-
* The client for which to check credentials.
21-
* @param string $client_secret
22-
* (optional) If a secret is required, check that they've given the right one.
19+
* @param IOAuth2Client $client The client for which to check credentials.
20+
* @param string $clientSecret (optional) If a secret is required, check that they've given the right one.
2321
*
24-
* @return
25-
* TRUE if the client credentials are valid, and MUST return FALSE if they aren't.
22+
* @return bool|array Returns true if the client credentials are valid, and MUST return false if they aren't.
2623
* When using "client credentials" grant mechanism and you want to
2724
* verify the scope of a user's access, return an associative array
2825
* with the scope values as below. We'll check the scope you provide
2926
* against the requested scope before providing an access token:
3027
* @code
3128
* return array(
32-
* 'scope' => <stored scope values (space-separated string)>,
29+
* 'scope' => <stored scope values (space-separated string)>,
3330
* );
3431
* @endcode
3532
*
36-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.4.2
33+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.4.2
3734
*
3835
* @ingroup oauth2_section_4
3936
*/
40-
public function checkClientCredentialsGrant(IOAuth2Client $client, $client_secret);
37+
public function checkClientCredentialsGrant(IOAuth2Client $client, $clientSecret);
4138
}

‎lib/OAuth2/IOAuth2GrantCode.php

+16-27
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
use OAuth2\Model\IOAuth2AuthCode;
77

88
/**
9-
* Storage engines that support the "Authorization Code"
10-
* grant type should implement this interface
9+
* Storage engines that support the "Authorization Code" grant type should implement this interface
1110
*
1211
* @author Dave Rochwerger <catch.dave@gmail.com>
13-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.1
12+
*
13+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.1
1414
*/
15-
interface IOAuth2GrantCode extends IOAuth2Storage {
16-
15+
interface IOAuth2GrantCode extends IOAuth2Storage
16+
{
1717
/**
1818
* The Authorization Code grant type supports a response type of "code".
1919
*
@@ -27,15 +27,13 @@ interface IOAuth2GrantCode extends IOAuth2Storage {
2727
* Fetch authorization code data (probably the most common grant type).
2828
*
2929
* Retrieve the stored data for the given authorization code.
30-
*
3130
* Required for OAuth2::GRANT_TYPE_AUTH_CODE.
3231
*
33-
* @param string $code
34-
* The authorization code string for which to fetch data.
32+
* @param string $code The authorization code string for which to fetch data.
3533
*
3634
* @return IOAuth2AuthCode
3735
*
38-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.1
36+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.1
3937
*
4038
* @ingroup oauth2_section_4
4139
*/
@@ -45,29 +43,20 @@ public function getAuthCode($code);
4543
* Take the provided authorization code values and store them somewhere.
4644
*
4745
* This function should be the storage counterpart to getAuthCode().
48-
*
49-
* If storage fails for some reason, we're not currently checking for
50-
* any sort of success/failure, so you should bail out of the script
51-
* and provide a descriptive fail message.
52-
*
46+
* If storage fails for some reason, we're not currently checking for any sort of success/failure, so you should
47+
* bail out of the script and provide a descriptive fail message.
5348
* Required for OAuth2::GRANT_TYPE_AUTH_CODE.
5449
*
55-
* @param string $code
56-
* Authorization code string to be stored.
57-
* @param IOAuth2Client $client
58-
* The client associated with this authorization code.
59-
* @param mixed $data
60-
* Application data to associate with this authorization code, such as a User object.
61-
* @param string $redirect_uri
62-
* Redirect URI to be stored.
63-
* @param int $expires
64-
* The timestamp when the authorization code will expire.
65-
* @param string $scope
66-
* (optional) Scopes to be stored in space-separated string.
50+
* @param string $code Authorization code string to be stored.
51+
* @param IOAuth2Client $client The client associated with this authorization code.
52+
* @param mixed $data Application data to associate with this authorization code, such as a User object.
53+
* @param string $redirectUri Redirect URI to be stored.
54+
* @param int $expires The timestamp when the authorization code will expire.
55+
* @param string $scope l(optional) Scopes to be stored in space-separated string.
6756
*
6857
* @ingroup oauth2_section_4
6958
*/
70-
public function createAuthCode($code, IOAuth2Client $client, $data, $redirect_uri, $expires, $scope = NULL);
59+
public function createAuthCode($code, IOAuth2Client $client, $data, $redirectUri, $expires, $scope = null);
7160

7261
/**
7362
* Marks auth code as expired.

‎lib/OAuth2/IOAuth2GrantExtension.php

+11-13
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,22 @@
99
* grant types should implement this interface
1010
*
1111
* @author Dave Rochwerger <catch.dave@gmail.com>
12-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.5
12+
*
13+
*
14+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.5
1315
*/
14-
interface IOAuth2GrantExtension extends IOAuth2Storage {
15-
16+
interface IOAuth2GrantExtension extends IOAuth2Storage
17+
{
1618
/**
1719
* Check any extended grant types.
1820
*
1921
* @param IOAuth2Client $client
20-
* @param string $uri
21-
* URI of the grant type definition
22-
* @param array $inputData
23-
* Unfiltered input data. The source is *not* guaranteed to be POST (but
24-
* is likely to be).
25-
* @param array $authHeaders
26-
* Authorization headers
27-
* @return
28-
* FALSE if the authorization is rejected or not support.
29-
* TRUE or an associative array if you want to verify the scope:
22+
* @param string $uri URI of the grant type definition
23+
* @param array $inputData Unfiltered input data. The source is *not* guaranteed to be POST (but is likely to be).
24+
* @param array $authHeaders Authorization headers
25+
*
26+
* @return bool|array Returns false if the authorization is rejected or not support. Returns true or an associative array if you
27+
* want to verify the scope:
3028
* @code
3129
* return array(
3230
* 'scope' => <stored scope values (space-separated string)>,

‎lib/OAuth2/IOAuth2GrantImplicit.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
namespace OAuth2;
44

55
/**
6-
* Storage engines that support the "Implicit"
7-
* grant type should implement this interface
6+
* Storage engines that support the "Implicit" grant type should implement this interface
87
*
98
* @author Dave Rochwerger <catch.dave@gmail.com>
10-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.2
9+
*
10+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.2
1111
*/
12-
interface IOAuth2GrantImplicit extends IOAuth2Storage {
13-
12+
interface IOAuth2GrantImplicit extends IOAuth2Storage
13+
{
1414
/**
1515
* The Implicit grant type supports a response type of "token".
1616
*
1717
* @var string
18+
*
1819
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-1.4.2
1920
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.2
2021
*/

‎lib/OAuth2/IOAuth2GrantUser.php

+12-19
Original file line numberDiff line numberDiff line change
@@ -5,44 +5,37 @@
55
use OAuth2\Model\IOAuth2Client;
66

77
/**
8-
* Storage engines that support the "Resource Owner Password Credentials"
9-
* grant type should implement this interface
8+
* Storage engines that support the "Resource Owner Password Credentials" grant type should implement this interface
109
*
1110
* @author Dave Rochwerger <catch.dave@gmail.com>
12-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.3
11+
*
12+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.3
1313
*/
14-
interface IOAuth2GrantUser extends IOAuth2Storage {
15-
14+
interface IOAuth2GrantUser extends IOAuth2Storage
15+
{
1616
/**
1717
* Grant access tokens for basic user credentials.
1818
*
1919
* Check the supplied username and password for validity.
20-
*
21-
* You can also use the $client param to do any checks required based
22-
* on a client, if you need that.
23-
*
20+
* You can also use the $client param to do any checks required based on a client, if you need that.
2421
* Required for OAuth2::GRANT_TYPE_USER_CREDENTIALS.
2522
*
26-
* @param IOAuth2Client $client
27-
* Client to check.
28-
* @param string $username
29-
* Username to check.
30-
* @param string $password
31-
* Password to check.
23+
* @param IOAuth2Client $client Client to check.
24+
* @param string $username Username to check.
25+
* @param string $password Password to check.
3226
*
33-
* @return
34-
* TRUE if the username and password are valid, and FALSE if they aren't.
27+
* @return bool|array Returns true if the username and password are valid or false if they aren't.
3528
* Moreover, if the username and password are valid, and you want to
3629
* verify the scope of a user's access, return an associative array
3730
* with the scope values as below. We'll check the scope you provide
3831
* against the requested scope before providing an access token:
3932
* @code
4033
* return array(
41-
* 'scope' => <stored scope values (space-separated string)>,
34+
* 'scope' => <stored scope values (space-separated string)>,
4235
* );
4336
* @endcode
4437
*
45-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.3
38+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.3
4639
*
4740
* @ingroup oauth2_section_4
4841
*/

‎lib/OAuth2/IOAuth2RefreshTokens.php

+21-34
Original file line numberDiff line numberDiff line change
@@ -6,74 +6,61 @@
66
use OAuth2\Model\IOAuth2Token;
77

88
/**
9-
* Storage engines that want to support refresh tokens should
10-
* implement this interface.
9+
* Storage engines that want to support refresh tokens should implement this interface.
1110
*
1211
* @author Dave Rochwerger <catch.dave@gmail.com>
13-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-6
14-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-1.5
12+
*
13+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-6
14+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-1.5
1515
*/
16-
interface IOAuth2RefreshTokens extends IOAuth2Storage {
17-
16+
interface IOAuth2RefreshTokens extends IOAuth2Storage
17+
{
1818
/**
1919
* Grant refresh access tokens.
2020
*
2121
* Retrieve the stored data for the given refresh token.
22-
*
2322
* Required for OAuth2::GRANT_TYPE_REFRESH_TOKEN.
2423
*
25-
* @param string $refresh_token
26-
* Refresh token string.
24+
* @param string $refreshToken Refresh token string.
2725
*
2826
* @return IOAuth2Token
2927
*
30-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-6
28+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-6
3129
*
3230
* @ingroup oauth2_section_6
3331
*/
34-
public function getRefreshToken($refresh_token);
32+
public function getRefreshToken($refreshToken);
3533

3634
/**
3735
* Take the provided refresh token values and store them somewhere.
3836
*
3937
* This function should be the storage counterpart to getRefreshToken().
40-
*
4138
* If storage fails for some reason, we're not currently checking for
4239
* any sort of success/failure, so you should bail out of the script
4340
* and provide a descriptive fail message.
44-
*
4541
* Required for OAuth2::GRANT_TYPE_REFRESH_TOKEN.
4642
*
47-
* @param string $refresh_token
48-
* The refresh token string to be stored.
49-
* @param IOAuth2Client $client
50-
* The client associated with this refresh token.
51-
* @param mixed $data
52-
* Application data associated with the refresh token, such as a User object.
53-
* @param int $expires
54-
* The timestamp when the refresh token will expire.
55-
* @param string $scope
56-
* (optional) Scopes to be stored in space-separated string.
43+
* @param string $refreshToken The refresh token string to be stored.
44+
* @param IOAuth2Client $client The client associated with this refresh token.
45+
* @param mixed $data Application data associated with the refresh token, such as a User object.
46+
* @param int $expires The timestamp when the refresh token will expire.
47+
* @param string $scope (optional) Scopes to be stored in space-separated string.
5748
*
5849
* @ingroup oauth2_section_6
5950
*/
60-
public function createRefreshToken($refresh_token, IOAuth2Client $client, $data, $expires, $scope = NULL);
51+
public function createRefreshToken($refreshToken, IOAuth2Client $client, $data, $expires, $scope = null);
6152

6253
/**
6354
* Expire a used refresh token.
6455
*
65-
* This is not explicitly required in the spec, but is almost implied.
66-
* After granting a new refresh token, the old one is no longer useful and
67-
* so should be forcibly expired in the data store so it can't be used again.
68-
*
69-
* If storage fails for some reason, we're not currently checking for
70-
* any sort of success/failure, so you should bail out of the script
71-
* and provide a descriptive fail message.
56+
* This is not explicitly required in the spec, but is almost implied. After granting a new refresh token, the old
57+
* one is no longer useful and so should be forcibly expired in the data store so it can't be used again.
58+
* If storage fails for some reason, we're not currently checking for any sort of success/failure, so you should
59+
* bail out of the script and provide a descriptive fail message.
7260
*
73-
* @param string $refresh_token
74-
* The refresh token string to expire.
61+
* @param string $refreshToken The refresh token string to expire.
7562
*
7663
* @ingroup oauth2_section_6
7764
*/
78-
public function unsetRefreshToken($refresh_token);
65+
public function unsetRefreshToken($refreshToken);
7966
}

‎lib/OAuth2/IOAuth2Storage.php

+23-35
Original file line numberDiff line numberDiff line change
@@ -3,90 +3,78 @@
33
namespace OAuth2;
44

55
use OAuth2\Model\IOAuth2Client;
6+
use OAuth2\Model\IOAuth2AccessToken;
67

78
/**
89
* All storage engines need to implement this interface in order to use OAuth2 server
910
*
1011
* @author David Rochwerger <catch.dave@gmail.com>
1112
*/
12-
interface IOAuth2Storage {
13-
13+
interface IOAuth2Storage
14+
{
1415
/**
1516
* Get a client by its ID.
1617
*
17-
* @param string $client_id
18+
* @param string $clientId
19+
*
1820
* @return IOAuth2Client
1921
*/
20-
public function getClient($client_id);
22+
public function getClient($clientId);
2123

2224
/**
2325
* Make sure that the client credentials are valid.
2426
*
25-
* @param IOAuth2Client $client
26-
* The client for which to check credentials.
27-
* @param string $client_secret
28-
* (optional) If a secret is required, check that they've given the right one.
27+
* @param IOAuth2Client $client The client for which to check credentials.
28+
* @param string $clientSecret (optional) If a secret is required, check that they've given the right one.
2929
*
30-
* @return
31-
* TRUE if the client credentials are valid, and MUST return FALSE if they aren't.
32-
* @endcode
30+
* @return bool TRUE if the client credentials are valid, and MUST return FALSE if they aren't.
3331
*
34-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-3.1
32+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-3.1
3533
*
3634
* @ingroup oauth2_section_3
3735
*/
38-
public function checkClientCredentials(IOAuth2Client $client, $client_secret = NULL);
36+
public function checkClientCredentials(IOAuth2Client $client, $clientSecret = null);
3937

4038
/**
4139
* Look up the supplied oauth_token from storage.
4240
*
4341
* We need to retrieve access token data as we create and verify tokens.
4442
*
45-
* @param string $oauth_token
46-
* The token string.
43+
* @param string $oauthToken The token string.
4744
*
4845
* @return IOAuth2AccessToken
4946
*
5047
* @ingroup oauth2_section_7
5148
*/
52-
public function getAccessToken($oauth_token);
49+
public function getAccessToken($oauthToken);
5350

5451
/**
5552
* Store the supplied access token values to storage.
5653
*
5754
* We need to store access token data as we create and verify tokens.
5855
*
59-
* @param string $oauth_token
60-
* The access token string to be stored.
61-
* @param IOAuth2Client $client
62-
* The client associated with this refresh token.
63-
* @param mixed $data
64-
* Application data associated with the refresh token, such as a User object.
65-
* @param int $expires
66-
* The timestamp when the refresh token will expire.
67-
* @param string $scope
68-
* (optional) Scopes to be stored in space-separated string.
56+
* @param string $oauthToken The access token string to be stored.
57+
* @param IOAuth2Client $client The client associated with this refresh token.
58+
* @param mixed $data Application data associated with the refresh token, such as a User object.
59+
* @param int $expires The timestamp when the refresh token will expire.
60+
* @param string $scope (optional) Scopes to be stored in space-separated string.
6961
*
7062
* @ingroup oauth2_section_4
7163
*/
72-
public function createAccessToken($oauth_token, IOAuth2Client $client, $data, $expires, $scope = NULL);
64+
public function createAccessToken($oauthToken, IOAuth2Client $client, $data, $expires, $scope = null);
7365

7466
/**
7567
* Check restricted grant types of corresponding client identifier.
7668
*
7769
* If you want to restrict clients to certain grant types, override this
7870
* function.
7971
*
80-
* @param IOAuth2Client $client
81-
* Client to check.
82-
* @param string $grant_type
83-
* Grant type to check. One of the values contained in OAuth2::GRANT_TYPE_REGEXP.
72+
* @param IOAuth2Client $client Client to check.
73+
* @param string $grantType Grant type to check. One of the values contained in OAuth2::GRANT_TYPE_REGEXP.
8474
*
85-
* @return
86-
* TRUE if the grant type is supported by this client identifier, and
87-
* FALSE if it isn't.
75+
* @return bool Returns true if the grant type is supported by this client identifier or false if it isn't.
8876
*
8977
* @ingroup oauth2_section_4
9078
*/
91-
public function checkRestrictedGrantType(IOAuth2Client $client, $grant_type);
79+
public function checkRestrictedGrantType(IOAuth2Client $client, $grantType);
9280
}

‎lib/OAuth2/Model/IOAuth2AccessToken.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace OAuth2\Model;
44

5-
interface IOAuth2AccessToken extends IOAuth2Token {
5+
interface IOAuth2AccessToken extends IOAuth2Token
6+
{
67
}
7-

‎lib/OAuth2/Model/IOAuth2AuthCode.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace OAuth2\Model;
44

5-
interface IOAuth2AuthCode extends IOAuth2Token {
6-
5+
interface IOAuth2AuthCode extends IOAuth2Token
6+
{
7+
/**
8+
* @return string
9+
*/
710
public function getRedirectUri();
811
}
9-

‎lib/OAuth2/Model/IOAuth2Client.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
namespace OAuth2\Model;
44

5-
interface IOAuth2Client {
6-
5+
interface IOAuth2Client
6+
{
7+
/**
8+
* @return string
9+
*/
710
public function getPublicId();
11+
12+
/**
13+
* @return array
14+
*/
815
public function getRedirectUris();
916
}
10-

‎lib/OAuth2/Model/IOAuth2RefreshToken.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace OAuth2\Model;
44

5-
interface IOAuth2RefreshToken extends IOAuth2Token {
5+
interface IOAuth2RefreshToken extends IOAuth2Token
6+
{
67
}
7-

‎lib/OAuth2/Model/IOAuth2Token.php

+21-3
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,35 @@
22

33
namespace OAuth2\Model;
44

5-
interface IOAuth2Token {
6-
5+
interface IOAuth2Token
6+
{
7+
/**
8+
* @return string
9+
*/
710
public function getClientId();
811

12+
/**
13+
* @return integer
14+
*/
915
public function getExpiresIn();
16+
17+
/**
18+
* @return boolean
19+
*/
1020
public function hasExpired();
1121

22+
/**
23+
* @return string
24+
*/
1225
public function getToken();
1326

27+
/**
28+
* @return null|string
29+
*/
1430
public function getScope();
1531

32+
/**
33+
* @return mixed
34+
*/
1635
public function getData();
1736
}
18-

‎lib/OAuth2/Model/OAuth2AccessToken.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace OAuth2\Model;
44

5-
class OAuth2AccessToken extends OAuth2Token implements IOAuth2AccessToken {
5+
class OAuth2AccessToken extends OAuth2Token implements IOAuth2AccessToken
6+
{
67
}
7-

‎lib/OAuth2/Model/OAuth2AuthCode.php

+26-5
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,40 @@
22

33
namespace OAuth2\Model;
44

5-
class OAuth2AuthCode extends OAuth2Token implements IOAuth2AuthCode {
5+
class OAuth2AuthCode extends OAuth2Token implements IOAuth2AuthCode
6+
{
7+
/**
8+
* @var null|string
9+
*/
10+
private $redirectUri;
611

7-
public function __construct($clientId, $token, $expiresAt = NULL, $scope = NULL, $data = NULL, $redirectUri = NULL) {
12+
/**
13+
* @param string $clientId
14+
* @param string $token
15+
* @param null|integer $expiresAt
16+
* @param null|string $scope
17+
* @param mixed $data
18+
* @param null|string $redirectUri
19+
*/
20+
public function __construct($clientId, $token, $expiresAt = null, $scope = null, $data = null, $redirectUri = null)
21+
{
822
parent::__construct($clientId, $token, $expiresAt, $scope, $data);
923
$this->setRedirectUri($redirectUri);
1024
}
1125

12-
public function setRedirectUri($uri) {
26+
/**
27+
* @param null|string $uri
28+
*/
29+
public function setRedirectUri($uri)
30+
{
1331
$this->redirectUri = $uri;
1432
}
1533

16-
public function getRedirectUri() {
34+
/**
35+
* {@inheritdoc}
36+
*/
37+
public function getRedirectUri()
38+
{
1739
return $this->redirectUri;
1840
}
1941
}
20-

‎lib/OAuth2/Model/OAuth2Client.php

+53-11
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,82 @@
22

33
namespace OAuth2\Model;
44

5-
class OAuth2Client implements IOAuth2Client {
6-
5+
class OAuth2Client implements IOAuth2Client
6+
{
7+
/**
8+
* @var string
9+
*/
710
private $id;
11+
12+
/**
13+
* @var array
14+
*/
815
private $redirectUris;
16+
17+
/**
18+
* @var null|string
19+
*/
920
private $secret;
1021

11-
public function __construct($id, $secret = NULL, array $redirectUris = array()) {
22+
/**
23+
* @param string $id
24+
* @param null $secret
25+
* @param array $redirectUris
26+
*/
27+
public function __construct($id, $secret = null, array $redirectUris = array())
28+
{
1229
$this->setPublicId($id);
1330
$this->setSecret($secret);
1431
$this->setRedirectUris($redirectUris);
1532
}
1633

17-
public function setPublicId($id) {
34+
/**
35+
* @param string $id
36+
*/
37+
public function setPublicId($id)
38+
{
1839
$this->id = $id;
1940
}
2041

21-
public function getPublicId() {
42+
/**
43+
* {@inheritdoc}
44+
*/
45+
public function getPublicId()
46+
{
2247
return $this->id;
2348
}
2449

25-
public function setSecret($secret) {
50+
/**
51+
* @param string $secret
52+
*/
53+
public function setSecret($secret)
54+
{
2655
$this->secret = $secret;
2756
}
2857

29-
public function checkSecret($secret) {
30-
return $this->secret === NULL || $secret === $this->secret;
58+
/**
59+
* @param mixed $secret
60+
*
61+
* @return boolean
62+
*/
63+
public function checkSecret($secret)
64+
{
65+
return $this->secret === null || $secret === $this->secret;
3166
}
3267

33-
public function setRedirectUris(array $redirectUris) {
68+
/**
69+
* @param array $redirectUris
70+
*/
71+
public function setRedirectUris(array $redirectUris)
72+
{
3473
$this->redirectUris = $redirectUris;
3574
}
3675

37-
public function getRedirectUris() {
76+
/**
77+
* {@inheritdoc}
78+
*/
79+
public function getRedirectUris()
80+
{
3881
return $this->redirectUris;
3982
}
4083
}
41-

‎lib/OAuth2/Model/OAuth2RefreshToken.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
namespace OAuth2\Model;
44

5-
class OAuth2RefreshToken extends OAuth2Token implements IOAuth2RefreshToken {
5+
class OAuth2RefreshToken extends OAuth2Token implements IOAuth2RefreshToken
6+
{
67
}
7-

‎lib/OAuth2/Model/OAuth2Token.php

+79-12
Original file line numberDiff line numberDiff line change
@@ -2,71 +2,138 @@
22

33
namespace OAuth2\Model;
44

5-
class OAuth2Token implements IOAuth2Token {
6-
5+
class OAuth2Token implements IOAuth2Token
6+
{
7+
/**
8+
* @var string
9+
*/
710
private $clientId;
11+
12+
/**
13+
* @var string
14+
*/
815
private $token;
16+
17+
/**
18+
* @var null|integer
19+
*/
920
private $expiresAt;
21+
22+
/**
23+
* @var null|string
24+
*/
1025
private $scope;
26+
27+
/**
28+
* @var mixed
29+
*/
1130
private $data;
1231

13-
public function __construct($clientId, $token, $expiresAt = NULL, $scope = NULL, $data = NULL) {
32+
/**
33+
* @param string $clientId
34+
* @param string $token
35+
* @param null|integer $expiresAt
36+
* @param null|string $scope
37+
* @param mixed $data
38+
*/
39+
public function __construct($clientId, $token, $expiresAt = null, $scope = null, $data = null)
40+
{
1441
$this->setClientId($clientId);
1542
$this->setToken($token);
1643
$this->setExpiresAt($expiresAt);
1744
$this->setScope($scope);
1845
$this->setData($data);
1946
}
2047

21-
public function setClientId($id) {
48+
/**
49+
* @param string $id
50+
*/
51+
public function setClientId($id)
52+
{
2253
$this->clientId = $id;
2354
}
2455

25-
public function getClientId() {
56+
/**
57+
* {@inheritdoc}
58+
*/
59+
public function getClientId()
60+
{
2661
return $this->clientId;
2762
}
2863

29-
public function setExpiresAt($timestamp) {
64+
/**
65+
* @param null|integer $timestamp
66+
*/
67+
public function setExpiresAt($timestamp)
68+
{
3069
$this->expiresAt = $timestamp;
3170
}
3271

33-
public function getExpiresIn() {
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function getExpiresIn()
76+
{
3477
if ($this->expiresAt) {
3578
return $this->expiresAt - time();
3679
} else {
3780
return PHP_INT_MAX;
3881
}
3982
}
4083

41-
public function hasExpired() {
84+
/**
85+
* {@inheritdoc}
86+
*/
87+
public function hasExpired()
88+
{
4289
return time() > $this->expiresAt;
4390
}
4491

45-
public function setToken($token) {
92+
/**
93+
* @param string $token
94+
*/
95+
public function setToken($token)
96+
{
4697
$this->token = $token;
4798
}
4899

100+
/**
101+
* {@inheritdoc}
102+
*/
49103
public function getToken()
50104
{
51105
return $this->token;
52106
}
53107

54-
public function setScope($scope) {
108+
/**
109+
* @param null|string $scope
110+
*/
111+
public function setScope($scope)
112+
{
55113
$this->scope = $scope;
56114
}
57115

116+
/**
117+
* {@inheritdoc}
118+
*/
58119
public function getScope()
59120
{
60121
return $this->scope;
61122
}
62123

63-
public function setData($data) {
124+
/**
125+
* @param null|string $data
126+
*/
127+
public function setData($data)
128+
{
64129
$this->data = $data;
65130
}
66131

132+
/**
133+
* {@inheritdoc}
134+
*/
67135
public function getData()
68136
{
69137
return $this->data;
70138
}
71139
}
72-

‎lib/OAuth2/OAuth2.php

+1,289-1,118
Large diffs are not rendered by default.

‎lib/OAuth2/OAuth2AuthenticateException.php

+62-48
Original file line numberDiff line numberDiff line change
@@ -6,60 +6,74 @@
66
* Send an error header with the given realm and an error, if provided.
77
* Suitable for the bearer token type.
88
*
9-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-bearer-04#section-2.4
9+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-bearer-04#section-2.4
1010
*
1111
* @ingroup oauth2_error
1212
*/
13-
class OAuth2AuthenticateException extends OAuth2ServerException {
14-
15-
protected $header;
16-
17-
/**
18-
*
19-
* @param $http_status_code
20-
* HTTP status code message as predefined.
21-
* @param $error
22-
* The "error" attribute is used to provide the client with the reason
23-
* why the access request was declined.
24-
* @param $error_description
25-
* (optional) The "error_description" attribute provides a human-readable text
26-
* containing additional information, used to assist in the understanding
27-
* and resolution of the error occurred.
28-
* @param $scope
29-
* A space-delimited list of scope values indicating the required scope
30-
* of the access token for accessing the requested resource.
31-
*/
32-
public function __construct($httpCode, $tokenType, $realm, $error, $error_description = NULL, $scope = NULL) {
33-
parent::__construct($httpCode, $error, $error_description);
34-
35-
if ($scope) {
36-
$this->errorData['scope'] = $scope;
37-
}
38-
39-
// Build header
40-
$header = sprintf('%s realm=%s', ucwords($tokenType), $this->quotedString($realm));
41-
foreach ($this->errorData as $key => $value) {
42-
$header .= sprintf(', %s=%s', $key, $this->quotedString($value));
13+
class OAuth2AuthenticateException extends OAuth2ServerException
14+
{
15+
/**
16+
* @var array
17+
*/
18+
protected $header;
19+
20+
/**
21+
* @param string $httpCode
22+
* @param string $tokenType
23+
* @param string $realm
24+
* @param string $error The "error" attribute is used to provide the client with the reason why the access request was declined.
25+
* @param string $errorDescription (optional) Human-readable text containing additional information, used to assist in the understanding and resolution of the error occurred.
26+
* @param string $scope (optional) A space-delimited list of scope values indicating the required scope of the access token for accessing the requested resource.
27+
*/
28+
public function __construct($httpCode, $tokenType, $realm, $error, $errorDescription = null, $scope = null)
29+
{
30+
parent::__construct($httpCode, $error, $errorDescription);
31+
32+
if ($scope) {
33+
$this->errorData['scope'] = $scope;
34+
}
35+
36+
// Build header
37+
$header = sprintf('%s realm=%s', ucwords($tokenType), $this->quote($realm));
38+
foreach ($this->errorData as $key => $value) {
39+
$header .= sprintf(', %s=%s', $key, $this->quote($value));
40+
}
41+
42+
$this->header = array('WWW-Authenticate' => $header);
4343
}
44-
$this->header = array('WWW-Authenticate' => $header);
45-
}
4644

47-
public function getResponseHeaders() {
48-
return $this->header + parent::getResponseHeaders();
49-
}
45+
/**
46+
* @return array
47+
*/
48+
public function getResponseHeaders()
49+
{
50+
return $this->header + parent::getResponseHeaders();
51+
}
5052

51-
private function quotedString($str) {
53+
/**
54+
* Adds quotes around $text
55+
*
56+
* @param string $text
57+
*
58+
* @return string
59+
*/
60+
private function quote($text)
61+
{
62+
// https://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-17#section-3.2.3
63+
$text = preg_replace(
64+
'~
65+
[^
66+
\x21-\x7E
67+
\x80-\xFF
68+
\ \t
69+
]
70+
~x',
71+
'',
72+
$text
73+
);
5274

53-
// https://tools.ietf.org/html/draft-ietf-httpbis-p1-messaging-17#section-3.2.3
54-
$str = preg_replace('~
55-
[^
56-
\x21-\x7E
57-
\x80-\xFF
58-
\ \t
59-
]
60-
~x', '', $str);
61-
$str = addcslashes($str, '"\\');
75+
$text = addcslashes($text, '"\\');
6276

63-
return '"' . $str . '"';
64-
}
77+
return '"' . $text . '"';
78+
}
6579
}

‎lib/OAuth2/OAuth2Client.php

+696-675
Large diffs are not rendered by default.

‎lib/OAuth2/OAuth2Exception.php

+65-64
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace OAuth2;
44

5-
65
/**
76
* OAuth2.0 draft v10 exception handling.
87
*
@@ -11,78 +10,80 @@
1110
*
1211
* @sa <a href="https://github.com/facebook/php-sdk">Facebook PHP SDK</a>.
1312
*/
14-
class OAuth2Exception extends \Exception {
13+
class OAuth2Exception extends \Exception
14+
{
15+
/**
16+
* The result from the API server that represents the exception information.
17+
*
18+
* @var array
19+
*/
20+
protected $result;
1521

16-
/**
17-
* The result from the API server that represents the exception information.
18-
*/
19-
protected $result;
22+
/**
23+
* Make a new API Exception with the given result.
24+
*
25+
* @param array $result The result from the API server.
26+
*/
27+
public function __construct($result)
28+
{
29+
$this->result = $result;
2030

21-
/**
22-
* Make a new API Exception with the given result.
23-
*
24-
* @param $result
25-
* The result from the API server.
26-
*/
27-
public function __construct($result) {
28-
$this->result = $result;
31+
$code = isset($result['code']) ? $result['code'] : 0;
2932

30-
$code = isset($result['code']) ? $result['code'] : 0;
33+
if (isset($result['error'])) {
34+
// OAuth 2.0 Draft 10 style
35+
$message = $result['error'];
36+
} elseif (isset($result['message'])) {
37+
// cURL style
38+
$message = $result['message'];
39+
} else {
40+
$message = 'Unknown Error. Check getResult()';
41+
}
3142

32-
if (isset($result['error'])) {
33-
// OAuth 2.0 Draft 10 style
34-
$message = $result['error'];
35-
}
36-
elseif (isset($result['message'])) {
37-
// cURL style
38-
$message = $result['message'];
39-
}
40-
else {
41-
$message = 'Unknown Error. Check getResult()';
43+
parent::__construct($message, $code);
4244
}
4345

44-
parent::__construct($message, $code);
45-
}
46+
/**
47+
* Return the associated result object returned by the API server.
48+
*
49+
* @return array The result from the API server.
50+
*/
51+
public function getResult()
52+
{
53+
return $this->result;
54+
}
4655

47-
/**
48-
* Return the associated result object returned by the API server.
49-
*
50-
* @returns
51-
* The result from the API server.
52-
*/
53-
public function getResult() {
54-
return $this->result;
55-
}
56+
/**
57+
* Returns the associated type for the error. This will default to
58+
* 'Exception' when a type is not available.
59+
*
60+
* @return string The type for the error.
61+
*/
62+
public function getType()
63+
{
64+
if (isset($this->result['error'])) {
65+
$message = $this->result['error'];
66+
if (is_string($message)) {
67+
// OAuth 2.0 Draft 10 style
68+
return $message;
69+
}
70+
}
5671

57-
/**
58-
* Returns the associated type for the error. This will default to
59-
* 'Exception' when a type is not available.
60-
*
61-
* @return
62-
* The type for the error.
63-
*/
64-
public function getType() {
65-
if (isset($this->result['error'])) {
66-
$message = $this->result['error'];
67-
if (is_string($message)) {
68-
// OAuth 2.0 Draft 10 style
69-
return $message;
70-
}
72+
return 'Exception';
7173
}
72-
return 'Exception';
73-
}
7474

75-
/**
76-
* To make debugging easier.
77-
*
78-
* @returns
79-
* The string representation of the error.
80-
*/
81-
public function __toString() {
82-
$str = $this->getType() . ': ';
83-
if ($this->code != 0) {
84-
$str .= $this->code . ': ';
75+
/**
76+
* To make debugging easier.
77+
*
78+
* @return string The string representation of the error.
79+
*/
80+
public function __toString()
81+
{
82+
$str = $this->getType() . ': ';
83+
if ($this->code != 0) {
84+
$str .= $this->code . ': ';
85+
}
86+
87+
return $str . $this->message;
8588
}
86-
return $str . $this->message;
87-
}
8889
}

‎lib/OAuth2/OAuth2RedirectException.php

+74-74
Original file line numberDiff line numberDiff line change
@@ -5,86 +5,86 @@
55
/**
66
* Redirect the end-user's user agent with error message.
77
*
8-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.1
8+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.1
99
*
1010
* @ingroup oauth2_error
1111
*/
12-
class OAuth2RedirectException extends OAuth2ServerException {
13-
14-
protected $redirectUri;
15-
16-
/**
17-
* @param $redirect_uri
18-
* An absolute URI to which the authorization server will redirect the
19-
* user-agent to when the end-user authorization step is completed.
20-
* @param $error
21-
* A single error code as described in Section 4.1.2.1
22-
* @param $error_description
23-
* (optional) A human-readable text providing additional information,
24-
* used to assist in the understanding and resolution of the error
25-
* occurred.
26-
* @param $state
27-
* (optional) REQUIRED if the "state" parameter was present in the client
28-
* authorization request. Set to the exact value received from the client.
29-
*
30-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.1.2.1
31-
*
32-
* @ingroup oauth2_error
33-
*/
34-
public function __construct($redirect_uri, $error, $error_description = NULL, $state = NULL) {
35-
parent::__construct(OAuth2::HTTP_FOUND, $error, $error_description);
36-
37-
$this->redirectUri = $redirect_uri;
38-
if ($state) {
39-
$this->errorData['state'] = $state;
12+
class OAuth2RedirectException extends OAuth2ServerException
13+
{
14+
/**
15+
* Redirect URI
16+
*
17+
* @var string
18+
*/
19+
protected $redirectUri;
20+
21+
/**
22+
* @param string $redirectUri An absolute URI to which the authorization server will redirect the user-agent to when the end-user authorization step is completed.
23+
* @param string $error A single error code as described in Section 4.1.2.1
24+
* @param string $errorDescription (optional) A human-readable text providing additional information, used to assist in the understanding and resolution of the error occurred.
25+
* @param string $state (optional) REQUIRED if the "state" parameter was present in the client authorization request. Set to the exact value received from the client.
26+
*
27+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-4.1.2.1
28+
*
29+
* @ingroup oauth2_error
30+
*/
31+
public function __construct($redirectUri, $error, $errorDescription = null, $state = null)
32+
{
33+
parent::__construct(OAuth2::HTTP_FOUND, $error, $errorDescription);
34+
35+
$this->redirectUri = $redirectUri;
36+
if ($state) {
37+
$this->errorData['state'] = $state;
38+
}
4039
}
41-
42-
}
43-
44-
/**
45-
* Redirect the user agent.
46-
*
47-
* @ingroup oauth2_section_4
48-
*/
49-
public function getResponseHeaders() {
50-
$params = array('query' => $this->errorData);
51-
return array(
52-
'Location' => $this->buildUri($this->redirectUri, $params),
53-
);
54-
}
5540

56-
/**
57-
* Build the absolute URI based on supplied URI and parameters.
58-
*
59-
* @param $uri
60-
* An absolute URI.
61-
* @param $params
62-
* Parameters to be append as GET.
63-
*
64-
* @return
65-
* An absolute URI with supplied parameters.
66-
*
67-
* @ingroup oauth2_section_4
68-
*/
69-
protected function buildUri($uri, $params) {
70-
$parse_url = parse_url($uri);
41+
/**
42+
* Redirect the user agent.
43+
*
44+
* @return array
45+
*
46+
* @ingroup oauth2_section_4
47+
*/
48+
public function getResponseHeaders()
49+
{
50+
$params = array('query' => $this->errorData);
7151

72-
// Add our params to the parsed uri
73-
foreach ($params as $k => $v) {
74-
if (isset($parse_url[$k]))
75-
$parse_url[$k] .= "&" . http_build_query($v);
76-
else
77-
$parse_url[$k] = http_build_query($v);
52+
return array(
53+
'Location' => $this->buildUri($this->redirectUri, $params),
54+
);
7855
}
7956

80-
// Put humpty dumpty back together
81-
return
82-
((isset($parse_url["scheme"])) ? $parse_url["scheme"] . "://" : "")
83-
. ((isset($parse_url["user"])) ? $parse_url["user"] . ((isset($parse_url["pass"])) ? ":" . $parse_url["pass"] : "") . "@" : "")
84-
. ((isset($parse_url["host"])) ? $parse_url["host"] : "")
85-
. ((isset($parse_url["port"])) ? ":" . $parse_url["port"] : "")
86-
. ((isset($parse_url["path"])) ? $parse_url["path"] : "")
87-
. ((isset($parse_url["query"])) ? "?" . $parse_url["query"] : "")
88-
. ((isset($parse_url["fragment"])) ? "#" . $parse_url["fragment"] : "");
89-
}
57+
/**
58+
* Build the absolute URI based on supplied URI and parameters.
59+
*
60+
* @param string $uri An absolute URI.
61+
* @param array $params Parameters to be append as GET.
62+
*
63+
* @return string An absolute URI with supplied parameters.
64+
*
65+
* @ingroup oauth2_section_4
66+
*/
67+
protected function buildUri($uri, $params)
68+
{
69+
$parse_url = parse_url($uri);
70+
71+
// Add our params to the parsed uri
72+
foreach ($params as $k => $v) {
73+
if (isset($parse_url[$k])) {
74+
$parse_url[$k] .= "&" . http_build_query($v);
75+
} else {
76+
$parse_url[$k] = http_build_query($v);
77+
}
78+
}
79+
80+
// Put humpty dumpty back together
81+
return
82+
((isset($parse_url["scheme"])) ? $parse_url["scheme"] . "://" : "")
83+
. ((isset($parse_url["user"])) ? $parse_url["user"] . ((isset($parse_url["pass"])) ? ":" . $parse_url["pass"] : "") . "@" : "")
84+
. ((isset($parse_url["host"])) ? $parse_url["host"] : "")
85+
. ((isset($parse_url["port"])) ? ":" . $parse_url["port"] : "")
86+
. ((isset($parse_url["path"])) ? $parse_url["path"] : "")
87+
. ((isset($parse_url["query"])) ? "?" . $parse_url["query"] : "")
88+
. ((isset($parse_url["fragment"])) ? "#" . $parse_url["fragment"] : "");
89+
}
9090
}

‎lib/OAuth2/OAuth2ServerException.php

+106-85
Original file line numberDiff line numberDiff line change
@@ -2,98 +2,119 @@
22

33
namespace OAuth2;
44

5-
use Exception;
65
use Symfony\Component\HttpFoundation\Response;
76

87
/**
9-
* OAuth2 errors that require termination of OAuth2 due to
10-
* an error.
11-
*
8+
* OAuth2 errors that require termination of OAuth2 due to an error.
129
*/
13-
class OAuth2ServerException extends Exception {
14-
15-
protected $httpCode;
16-
protected $errorData = array();
17-
18-
/**
19-
* @param $http_status_code
20-
* HTTP status code message as predefined.
21-
* @param $error
22-
* A single error code.
23-
* @param $error_description
24-
* (optional) A human-readable text providing additional information,
25-
* used to assist in the understanding and resolution of the error
26-
* occurred.
27-
*/
28-
public function __construct($http_status_code, $error, $error_description = NULL) {
29-
parent::__construct($error);
30-
31-
$this->httpCode = $http_status_code;
32-
33-
$this->errorData['error'] = $error;
34-
if ($error_description) {
35-
$this->errorData['error_description'] = $error_description;
10+
class OAuth2ServerException extends \Exception
11+
{
12+
/**
13+
* @var string
14+
*/
15+
protected $httpCode;
16+
17+
/**
18+
* @var array
19+
*/
20+
protected $errorData = array();
21+
22+
/**
23+
* @param string $httpStatusCode HTTP status code message as predefined.
24+
* @param string $error A single error code.
25+
* @param string $errorDescription (optional) A human-readable text providing additional information, used to assist in the understanding and resolution of the error occurred.
26+
*/
27+
public function __construct($httpStatusCode, $error, $errorDescription = null)
28+
{
29+
parent::__construct($error);
30+
31+
$this->httpCode = $httpStatusCode;
32+
33+
$this->errorData['error'] = $error;
34+
$this->errorData['error_description'] = $errorDescription;
35+
}
36+
37+
/**
38+
* Get error description
39+
*
40+
* @return string
41+
*/
42+
public function getDescription()
43+
{
44+
return $this->errorData['error_description'];
45+
}
46+
47+
/**
48+
* Get HTTP code
49+
*
50+
* @return string
51+
*/
52+
public function getHttpCode()
53+
{
54+
return $this->httpCode;
3655
}
37-
}
3856

39-
/**
40-
* @return string
41-
*/
42-
public function getDescription() {
43-
return isset($this->errorData['error_description']) ? $this->errorData['error_description'] : null;
44-
}
45-
46-
/**
47-
* @return string
48-
*/
49-
public function getHttpCode() {
50-
return $this->httpCode;
51-
}
57+
/**
58+
* Get HTTP Error Response
59+
*
60+
* @return Response
61+
*
62+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.1
63+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.2
64+
*
65+
* @ingroup oauth2_error
66+
*/
67+
public function getHttpResponse()
68+
{
69+
return new Response(
70+
$this->getResponseBody(),
71+
$this->getHttpCode(),
72+
$this->getResponseHeaders()
73+
);
74+
}
5275

53-
/**
54-
* Get HTTP Error Response
55-
*
56-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.1
57-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.2
58-
*
59-
* @ingroup oauth2_error
60-
*/
61-
public function getHttpResponse() {
62-
return new Response(
63-
$this->getResponseBody(),
64-
$this->getHttpCode(),
65-
$this->getResponseHeaders()
66-
);
67-
}
76+
/**
77+
* Get HTTP Error Response headers
78+
*
79+
* @return array
80+
*
81+
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.2
82+
*
83+
* @ingroup oauth2_error
84+
*/
85+
public function getResponseHeaders()
86+
{
87+
return array(
88+
'Content-Type' => 'application/json',
89+
'Cache-Control' => 'no-store',
90+
'Pragma' => 'no-cache',
91+
);
92+
}
6893

69-
/**
70-
* Get HTTP Error Response headers
71-
*
72-
* @see http://tools.ietf.org/html/draft-ietf-oauth-v2-20#section-5.2
73-
*
74-
* @ingroup oauth2_error
75-
*/
76-
public function getResponseHeaders() {
77-
return array(
78-
'Content-Type' => 'application/json',
79-
'Cache-Control' => 'no-store',
80-
'Pragma' => 'no-cache',
81-
);
82-
}
94+
/**
95+
* Get response body as JSON string
96+
*
97+
* @return string
98+
*/
99+
public function getResponseBody()
100+
{
101+
return json_encode($this->errorData);
102+
}
83103

84-
public function getResponseBody() {
85-
return json_encode($this->errorData);
86-
}
87-
88-
public function sendHttpResponse() {
89-
$this->getHttpResponse()->send();
90-
exit;
91-
}
92-
93-
/**
94-
* @see Exception::__toString()
95-
*/
96-
public function __toString() {
97-
return $this->getResponseBody();
98-
}
104+
/**
105+
* Outputs response
106+
*/
107+
public function sendHttpResponse()
108+
{
109+
$this->getHttpResponse()->send();
110+
exit; // TODO: refactor out this piece of code
111+
}
112+
113+
/**
114+
* @see \Exception::__toString()
115+
*/
116+
public function __toString()
117+
{
118+
return $this->getResponseBody();
119+
}
99120
}

‎server/examples/mongo/addclient.php

+21-21
Original file line numberDiff line numberDiff line change
@@ -7,32 +7,32 @@
77
* Obviously not production-ready code, just simple and to the point.
88
*/
99

10-
include "lib/MongoOAuth2.php";
10+
include 'lib/MongoOAuth2.php';
1111

1212
if ($_POST && isset($_POST["client_id"]) && isset($_POST["client_secret"]) && isset($_POST["redirect_uri"])) {
13-
$oauth = new MongoOAuth2();
14-
$oauth->addClient($_POST["client_id"], $_POST["client_secret"], $_POST["redirect_uri"]);
13+
$oauth = new MongoOAuth2();
14+
$oauth->addClient($_POST["client_id"], $_POST["client_secret"], $_POST["redirect_uri"]);
1515
}
1616

1717
?>
1818

1919
<html>
20-
<head>Add Client</head>
21-
<body>
22-
<form method="post" action="addclient.php">
23-
<p>
24-
<label for="client_id">Client ID:</label>
25-
<input type="text" name="client_id" id="client_id" />
26-
</p>
27-
<p>
28-
<label for="client_secret">Client Secret (password/key):</label>
29-
<input type="text" name="client_secret" id="client_secret" />
30-
</p>
31-
<p>
32-
<label for="redirect_uri">Redirect URI:</label>
33-
<input type="text" name="redirect_uri" id="redirect_uri" />
34-
</p>
35-
<input type="submit" value="Submit" />
36-
</form>
37-
</body>
20+
<head>Add Client</head>
21+
<body>
22+
<form method="post" action="addclient.php">
23+
<p>
24+
<label for="client_id">Client ID:</label>
25+
<input type="text" name="client_id" id="client_id" />
26+
</p>
27+
<p>
28+
<label for="client_secret">Client Secret (password/key):</label>
29+
<input type="text" name="client_secret" id="client_secret" />
30+
</p>
31+
<p>
32+
<label for="redirect_uri">Redirect URI:</label>
33+
<input type="text" name="redirect_uri" id="redirect_uri" />
34+
</p>
35+
<input type="submit" value="Submit" />
36+
</form>
37+
</body>
3838
</html>

‎server/examples/mongo/authorize.php

+17-17
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,33 @@
99
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
1010
*/
1111

12-
require "lib/MongoOAuth2.php";
12+
require 'lib/MongoOAuth2.php';
1313

1414
$oauth = new MongoOAuth2();
1515

1616
if ($_POST) {
17-
$oauth->finishClientAuthorization($_POST["accept"] == "Yep", $_POST);
17+
$oauth->finishClientAuthorization($_POST["accept"] == "Yep", $_POST);
1818
}
1919

2020
try {
21-
$auth_params = $oauth->getAuthorizeParams();
21+
$auth_params = $oauth->getAuthorizeParams();
2222
} catch (OAuth2ServerException $oauthError) {
23-
$oauthError->sendHttpResponse();
23+
$oauthError->sendHttpResponse();
2424
}
2525

2626
?>
2727
<html>
28-
<head>Authorize</head>
29-
<body>
30-
<form method="post" action="authorize.php">
31-
<?php foreach ($auth_params as $k => $v) { ?>
32-
<input type="hidden" name="<?php echo $k ?>" value="<?php echo $v ?>" />
33-
<?php } ?>
34-
Do you authorize the app to do its thing?
35-
<p>
36-
<input type="submit" name="accept" value="Yep" />
37-
<input type="submit" name="accept" value="Nope" />
38-
</p>
39-
</form>
40-
</body>
28+
<head>Authorize</head>
29+
<body>
30+
<form method="post" action="authorize.php">
31+
<?php foreach ($auth_params as $k => $v) { ?>
32+
<input type="hidden" name="<?php echo $k ?>" value="<?php echo $v ?>" />
33+
<?php } ?>
34+
Do you authorize the app to do its thing?
35+
<p>
36+
<input type="submit" name="accept" value="Yep" />
37+
<input type="submit" name="accept" value="Nope" />
38+
</p>
39+
</form>
40+
</body>
4141
</html>

‎server/examples/mongo/lib/OAuth2StorageMongo.php

+188-171
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* @file
55
* Sample OAuth2 Library Mongo DB Implementation.
6-
*
6+
*
77
*/
88

99
require_once __DIR__.'/../../../../lib/OAuth2.php';
@@ -14,174 +14,191 @@
1414
/**
1515
* Mongo storage engine for the OAuth2 Library.
1616
*/
17-
class OAuth2StorageMongo implements IOAuth2GrantCode, IOAuth2RefreshTokens {
18-
19-
/**
20-
* Change this to something unique for your system
21-
* @var string
22-
*/
23-
const SALT = 'CHANGE_ME!';
24-
25-
const CONNECTION = 'mongodb://user:pass@mongoserver/mydb';
26-
const DB = 'mydb';
27-
28-
/**
29-
* @var Mongo
30-
*/
31-
private $db;
32-
33-
/**
34-
* Implements OAuth2::__construct().
35-
*/
36-
public function __construct(PDO $db) {
37-
38-
$mongo = new Mongo(self::CONNECTION);
39-
$this->db = $mongo->selectDB(self::DB);
40-
}
41-
42-
/**
43-
* Release DB connection during destruct.
44-
*/
45-
function __destruct() {
46-
$this->db = NULL; // Release db connection
47-
}
48-
49-
/**
50-
* Handle PDO exceptional cases.
51-
*/
52-
private function handleException($e) {
53-
echo 'Database error: '. $e->getMessage();
54-
exit;
55-
}
56-
57-
/**
58-
* Little helper function to add a new client to the database.
59-
*
60-
* @param $client_id
61-
* Client identifier to be stored.
62-
* @param $client_secret
63-
* Client secret to be stored.
64-
* @param $redirect_uri
65-
* Redirect URI to be stored.
66-
*/
67-
public function addClient($client_id, $client_secret, $redirect_uri) {
68-
$this->db->clients->insert(array(
69-
"_id" => $client_id,
70-
"pw" => $this->hash($client_secret, $client_id),
71-
"redirect_uri" => $redirect_uri
72-
));
73-
}
74-
75-
/**
76-
* Implements IOAuth2Storage::checkClientCredentials().
77-
*
78-
*/
79-
public function checkClientCredentials($client_id, $client_secret = NULL) {
80-
$client = $this->db->clients->findOne(array("_id" => $client_id, "pw" => $client_secret));
81-
return $this->checkPassword($client_secret, $result['client_secret'], $client_id);
82-
}
83-
84-
/**
85-
* Implements IOAuth2Storage::getRedirectUri().
86-
*/
87-
public function getClientDetails($client_id) {
88-
$result = $this->db->clients->findOne(array("_id" => $client_id), array("redirect_uri"));
89-
}
90-
91-
/**
92-
* Implements IOAuth2Storage::getAccessToken().
93-
*/
94-
public function getAccessToken($oauth_token) {
95-
return $this->db->tokens->findOne(array("_id" => $oauth_token));
96-
}
97-
98-
/**
99-
* Implements IOAuth2Storage::setAccessToken().
100-
*/
101-
public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = NULL) {
102-
$this->db->tokens->insert(array(
103-
"_id" => $oauth_token,
104-
"client_id" => $client_id,
105-
"expires" => $expires,
106-
"scope" => $scope
107-
));
108-
}
109-
110-
/**
111-
* @see IOAuth2Storage::getRefreshToken()
112-
*/
113-
public function getRefreshToken($refresh_token) {
114-
return $this->getToken($refresh_token, TRUE);
115-
}
116-
117-
/**
118-
* @see IOAuth2Storage::setRefreshToken()
119-
*/
120-
public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope = NULL) {
121-
return $this->setToken($refresh_token, $client_id, $user_id, $expires, $scope, TRUE);
122-
}
123-
124-
/**
125-
* @see IOAuth2Storage::unsetRefreshToken()
126-
*/
127-
public function unsetRefreshToken($refresh_token) {
128-
try {
129-
$sql = 'DELETE FROM '.self::TABLE_TOKENS.' WHERE refresh_token = :refresh_token';
130-
$stmt = $this->db->prepare($sql);
131-
$stmt->bindParam(':refresh_token', $refresh_token, PDO::PARAM_STR);
132-
$stmt->execute();
133-
} catch (PDOException $e) {
134-
$this->handleException($e);
135-
}
136-
}
137-
138-
/**
139-
* Implements IOAuth2Storage::getAuthCode().
140-
*/
141-
public function getAuthCode($code) {
142-
$stored_code = $this->db->auth_codes->findOne(array("_id" => $code));
143-
return $stored_code !== NULL ? $stored_code : FALSE;
144-
}
145-
146-
/**
147-
* Implements IOAuth2Storage::setAuthCode().
148-
*/
149-
public function setAuthCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = NULL) {
150-
$this->db->auth_codes->insert(array(
151-
"_id" => $code,
152-
"client_id" => $client_id,
153-
"redirect_uri" => $redirect_uri,
154-
"expires" => $expires,
155-
"scope" => $scope
156-
));
157-
}
158-
159-
/**
160-
* @see IOAuth2Storage::checkRestrictedGrantType()
161-
*/
162-
public function checkRestrictedGrantType($client_id, $grant_type) {
163-
return TRUE; // Not implemented
164-
}
165-
166-
/**
167-
* Change/override this to whatever your own password hashing method is.
168-
*
169-
* @param string $secret
170-
* @return string
171-
*/
172-
protected function hash($client_secret, $client_id) {
173-
return hash('blowfish', $client_id.$client_secret.self::SALT);
174-
}
175-
176-
/**
177-
* Checks the password.
178-
* Override this if you need to
179-
*
180-
* @param string $client_id
181-
* @param string $client_secret
182-
* @param string $actualPassword
183-
*/
184-
protected function checkPassword($try, $client_secret, $client_id) {
185-
return $try == $this->hash($client_secret, $client_id);
186-
}
17+
class OAuth2StorageMongo implements IOAuth2GrantCode, IOAuth2RefreshTokens
18+
{
19+
/**
20+
* Change this to something unique for your system
21+
* @var string
22+
*/
23+
const SALT = 'CHANGE_ME!';
24+
25+
const CONNECTION = 'mongodb://user:pass@mongoserver/mydb';
26+
const DB = 'mydb';
27+
28+
/**
29+
* @var Mongo
30+
*/
31+
private $db;
32+
33+
/**
34+
* Implements OAuth2::__construct().
35+
*/
36+
public function __construct(PDO $db)
37+
{
38+
$mongo = new Mongo(self::CONNECTION);
39+
$this->db = $mongo->selectDB(self::DB);
40+
}
41+
42+
/**
43+
* Release DB connection during destruct.
44+
*/
45+
public function __destruct()
46+
{
47+
$this->db = null; // Release db connection
48+
}
49+
50+
/**
51+
* Handle PDO exceptional cases.
52+
*/
53+
private function handleException($e)
54+
{
55+
echo 'Database error: '. $e->getMessage();
56+
exit;
57+
}
58+
59+
/**
60+
* Little helper function to add a new client to the database.
61+
*
62+
* @param $client_id
63+
* Client identifier to be stored.
64+
* @param $client_secret
65+
* Client secret to be stored.
66+
* @param $redirect_uri
67+
* Redirect URI to be stored.
68+
*/
69+
public function addClient($client_id, $client_secret, $redirect_uri)
70+
{
71+
$this->db->clients->insert(array(
72+
"_id" => $client_id,
73+
"pw" => $this->hash($client_secret, $client_id),
74+
"redirect_uri" => $redirect_uri
75+
));
76+
}
77+
78+
/**
79+
* Implements IOAuth2Storage::checkClientCredentials().
80+
*
81+
*/
82+
public function checkClientCredentials($client_id, $client_secret = null)
83+
{
84+
$client = $this->db->clients->findOne(array("_id" => $client_id, "pw" => $client_secret));
85+
86+
return $this->checkPassword($client_secret, $result['client_secret'], $client_id);
87+
}
88+
89+
/**
90+
* Implements IOAuth2Storage::getRedirectUri().
91+
*/
92+
public function getClientDetails($client_id)
93+
{
94+
$result = $this->db->clients->findOne(array("_id" => $client_id), array("redirect_uri"));
95+
}
96+
97+
/**
98+
* Implements IOAuth2Storage::getAccessToken().
99+
*/
100+
public function getAccessToken($oauth_token)
101+
{
102+
return $this->db->tokens->findOne(array("_id" => $oauth_token));
103+
}
104+
105+
/**
106+
* Implements IOAuth2Storage::setAccessToken().
107+
*/
108+
public function setAccessToken($oauth_token, $client_id, $user_id, $expires, $scope = null)
109+
{
110+
$this->db->tokens->insert(array(
111+
"_id" => $oauth_token,
112+
"client_id" => $client_id,
113+
"expires" => $expires,
114+
"scope" => $scope
115+
));
116+
}
117+
118+
/**
119+
* @see IOAuth2Storage::getRefreshToken()
120+
*/
121+
public function getRefreshToken($refresh_token)
122+
{
123+
return $this->getToken($refresh_token, true);
124+
}
125+
126+
/**
127+
* @see IOAuth2Storage::setRefreshToken()
128+
*/
129+
public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope = null)
130+
{
131+
return $this->setToken($refresh_token, $client_id, $user_id, $expires, $scope, true);
132+
}
133+
134+
/**
135+
* @see IOAuth2Storage::unsetRefreshToken()
136+
*/
137+
public function unsetRefreshToken($refresh_token)
138+
{
139+
try {
140+
$sql = 'DELETE FROM '.self::TABLE_TOKENS.' WHERE refresh_token = :refresh_token';
141+
$stmt = $this->db->prepare($sql);
142+
$stmt->bindParam(':refresh_token', $refresh_token, PDO::PARAM_STR);
143+
$stmt->execute();
144+
} catch (PDOException $e) {
145+
$this->handleException($e);
146+
}
147+
}
148+
149+
/**
150+
* Implements IOAuth2Storage::getAuthCode().
151+
*/
152+
public function getAuthCode($code)
153+
{
154+
$stored_code = $this->db->auth_codes->findOne(array("_id" => $code));
155+
156+
return $stored_code !== null ? $stored_code : false;
157+
}
158+
159+
/**
160+
* Implements IOAuth2Storage::setAuthCode().
161+
*/
162+
public function setAuthCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope = null)
163+
{
164+
$this->db->auth_codes->insert(array(
165+
"_id" => $code,
166+
"client_id" => $client_id,
167+
"redirect_uri" => $redirect_uri,
168+
"expires" => $expires,
169+
"scope" => $scope
170+
));
171+
}
172+
173+
/**
174+
* @see IOAuth2Storage::checkRestrictedGrantType()
175+
*/
176+
public function checkRestrictedGrantType($client_id, $grant_type)
177+
{
178+
return true; // Not implemented
179+
}
180+
181+
/**
182+
* Change/override this to whatever your own password hashing method is.
183+
*
184+
* @param string $secret
185+
* @return string
186+
*/
187+
protected function hash($client_secret, $client_id)
188+
{
189+
return hash('blowfish', $client_id.$client_secret.self::SALT);
190+
}
191+
192+
/**
193+
* Checks the password.
194+
* Override this if you need to
195+
*
196+
* @param string $client_id
197+
* @param string $client_secret
198+
* @param string $actualPassword
199+
*/
200+
protected function checkPassword($try, $client_secret, $client_id)
201+
{
202+
return $try == $this->hash($client_secret, $client_id);
203+
}
187204
}

‎server/examples/mongo/protected_resource.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
1010
*/
1111

12-
require "lib/OAuth2StorageMongo.php";
12+
require 'lib/OAuth2StorageMongo.php';
1313

1414
$token = isset($_GET[OAuth2::TOKEN_PARAM_NAME]) ? $_GET[OAuth2::TOKEN_PARAM_NAME] : null;
1515
$oauth = new OAuth2(new OAuth2StorageMongo());
1616

1717
try {
18-
$oauth->verifyAccessToken($token);
18+
$oauth->verifyAccessToken($token);
1919
} catch (OAuth2ServerException $oauthError) {
20-
$oauthError->sendHttpResponse();
20+
$oauthError->sendHttpResponse();
2121
}
2222

2323
// With a particular scope, you'd do:
@@ -26,10 +26,10 @@
2626
?>
2727

2828
<html>
29-
<head>
30-
<title>Hello!</title>
31-
</head>
32-
<body>
33-
<p>This is a secret.</p>
34-
</body>
29+
<head>
30+
<title>Hello!</title>
31+
</head>
32+
<body>
33+
<p>This is a secret.</p>
34+
</body>
3535
</html>

‎server/examples/mongo/token.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@
99
* In reality, you'd probably use a nifty framework to handle most of the crud for you.
1010
*/
1111

12-
require "lib/MongoOAuth2.php";
12+
require 'lib/MongoOAuth2.php';
1313

1414
$oauth = new MongoOAuth2();
1515
try {
16-
$oauth->grantAccessToken();
17-
}
18-
catch (OAuth2ServerException $oauthError) {
19-
$oauthError->sendHttpResponse();
16+
$oauth->grantAccessToken();
17+
} catch (OAuth2ServerException $oauthError) {
18+
$oauthError->sendHttpResponse();
2019
}

‎server/examples/pdo/addclient.php

+22-22
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,31 @@
1010
require 'lib/bootstrap.php';
1111

1212
if ($_POST && isset($_POST["client_id"]) && isset($_POST["client_secret"]) && isset($_POST["redirect_uri"])) {
13-
$oauth = new OAuth2StoragePDO(newPDO());
14-
$oauth->addClient($_POST["client_id"], $_POST["client_secret"], $_POST["redirect_uri"]);
13+
$oauth = new OAuth2StoragePDO(newPDO());
14+
$oauth->addClient($_POST["client_id"], $_POST["client_secret"], $_POST["redirect_uri"]);
1515
}
1616

1717
?>
1818

1919
<html>
20-
<head>
21-
<title>Add Client</title>
22-
</head>
23-
<body>
24-
<form method="post" action="addclient.php">
25-
<p>
26-
<label for="client_id">Client ID:</label>
27-
<input type="text" name="client_id" id="client_id" />
28-
</p>
29-
<p>
30-
<label for="client_secret">Client Secret (password/key):</label>
31-
<input type="text" name="client_secret" id="client_secret" />
32-
</p>
33-
<p>
34-
<label for="redirect_uri">Redirect URI:</label>
35-
<input type="text" name="redirect_uri" id="redirect_uri" />
36-
</p>
37-
<input type="submit" value="Submit" />
38-
</form>
39-
</body>
20+
<head>
21+
<title>Add Client</title>
22+
</head>
23+
<body>
24+
<form method="post" action="addclient.php">
25+
<p>
26+
<label for="client_id">Client ID:</label>
27+
<input type="text" name="client_id" id="client_id" />
28+
</p>
29+
<p>
30+
<label for="client_secret">Client Secret (password/key):</label>
31+
<input type="text" name="client_secret" id="client_secret" />
32+
</p>
33+
<p>
34+
<label for="redirect_uri">Redirect URI:</label>
35+
<input type="text" name="redirect_uri" id="redirect_uri" />
36+
</p>
37+
<input type="submit" value="Submit" />
38+
</form>
39+
</body>
4040
</html>

‎server/examples/pdo/authorize.php

+37-37
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* @file
44
* Sample authorize endpoint.
55
*
6-
* This sample provides two click-jacking prevention methods, neither which are perfect.
7-
* The javascript solution is similar to what facebook used to have (but can be defeated with a
6+
* This sample provides two click-jacking prevention methods, neither which are perfect.
7+
* The javascript solution is similar to what facebook used to have (but can be defeated with a
88
* specially crafted frame-wrapper).
99
*/
1010

@@ -18,56 +18,56 @@
1818

1919
/*
2020
* You would need to authenticate the user before authorization.
21-
*
21+
*
2222
* Below is some psudeo-code to show what you might do:
23-
*
23+
*
2424
session_start();
2525
if (!isLoggedIn()) {
26-
redirectToLoginPage();
27-
exit();
26+
redirectToLoginPage();
27+
exit();
2828
}
2929
*/
3030

3131
$oauth = new OAuth2(new OAuth2StoragePDO(newPDO()));
3232

3333
if ($_POST) {
34-
$userId = 123; // Use whatever method you have for identifying users.
35-
try {
36-
$response = $oauth->finishClientAuthorization($_POST["accept"] == "Yep", $userId);
37-
$response->send();
38-
} catch(OAuth2ServerException $e) {
39-
$e->getHttpResponse()->send();
40-
}
41-
exit;
34+
$userId = 123; // Use whatever method you have for identifying users.
35+
try {
36+
$response = $oauth->finishClientAuthorization($_POST["accept"] == "Yep", $userId);
37+
$response->send();
38+
} catch (OAuth2ServerException $e) {
39+
$e->getHttpResponse()->send();
40+
}
41+
exit;
4242
}
4343

4444
try {
45-
$auth_params = $oauth->getAuthorizeParams();
45+
$auth_params = $oauth->getAuthorizeParams();
4646
} catch (OAuth2ServerException $oauthError) {
47-
$oauthError->sendHttpResponse();
47+
$oauthError->sendHttpResponse();
4848
}
4949

5050
?>
5151
<html>
52-
<head>
53-
<title>Authorize</title>
54-
<script>
55-
if (top != self) {
56-
window.document.write("<div style='background:black; opacity:0.5; filter: alpha (opacity = 50); position: absolute; top:0px; left: 0px;"
57-
+ "width: 9999px; height: 9999px; zindex: 1000001' onClick='top.location.href=window.location.href'></div>");
58-
}
59-
</script>
60-
</head>
61-
<body>
62-
<form method="post" action="">
63-
<?php foreach ($auth_params as $key => $value) : ?>
64-
<input type="hidden" name="<?php htmlspecialchars($key, ENT_QUOTES); ?>" value="<?php echo htmlspecialchars($value, ENT_QUOTES); ?>" />
65-
<?php endforeach; ?>
66-
Do you authorize the app to do its thing?
67-
<p>
68-
<input type="submit" name="accept" value="Yep" />
69-
<input type="submit" name="accept" value="Nope" />
70-
</p>
71-
</form>
72-
</body>
52+
<head>
53+
<title>Authorize</title>
54+
<script>
55+
if (top != self) {
56+
window.document.write("<div style='background:black; opacity:0.5; filter: alpha (opacity = 50); position: absolute; top:0px; left: 0px;"
57+
+ "width: 9999px; height: 9999px; zindex: 1000001' onClick='top.location.href=window.location.href'></div>");
58+
}
59+
</script>
60+
</head>
61+
<body>
62+
<form method="post" action="">
63+
<?php foreach ($auth_params as $key => $value) : ?>
64+
<input type="hidden" name="<?php htmlspecialchars($key, ENT_QUOTES); ?>" value="<?php echo htmlspecialchars($value, ENT_QUOTES); ?>" />
65+
<?php endforeach; ?>
66+
Do you authorize the app to do its thing?
67+
<p>
68+
<input type="submit" name="accept" value="Yep" />
69+
<input type="submit" name="accept" value="Nope" />
70+
</p>
71+
</form>
72+
</body>
7373
</html>

‎server/examples/pdo/lib/OAuth2StoragePdo.php

+283-262
Large diffs are not rendered by default.

‎server/examples/pdo/lib/bootstrap.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
require_once __DIR__ . '/OAuth2StoragePdo.php';
1818

19-
function newPDO() {
20-
19+
function newPDO()
20+
{
2121
$settings = parse_ini_file(__DIR__ . '/settings.ini');
2222
$pdo = new PDO($settings['dsn'], $settings['user'], $settings['password']);
2323
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
24+
2425
return $pdo;
2526
}
26-

‎server/examples/pdo/mysql_create_tables.sql

+24-24
Original file line numberDiff line numberDiff line change
@@ -6,37 +6,37 @@ DROP TABLE IF EXISTS access_tokens;
66
DROP TABLE IF EXISTS refresh_tokens;
77

88
CREATE TABLE `auth_codes` (
9-
`code` varchar(40) NOT NULL,
10-
`client_id` varchar(20) NOT NULL,
11-
`user_id` varchar(20) NOT NULL,
12-
`redirect_uri` varchar(200) NOT NULL,
13-
`expires` int(11) NOT NULL,
14-
`scope` varchar(250) DEFAULT NULL,
15-
PRIMARY KEY (`code`)
9+
`code` varchar(40) NOT NULL,
10+
`client_id` varchar(20) NOT NULL,
11+
`user_id` varchar(20) NOT NULL,
12+
`redirect_uri` varchar(200) NOT NULL,
13+
`expires` int(11) NOT NULL,
14+
`scope` varchar(250) DEFAULT NULL,
15+
PRIMARY KEY (`code`)
1616
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
1717

1818
CREATE TABLE `clients` (
19-
`client_id` varchar(20) NOT NULL,
20-
`client_secret` varchar(200) NOT NULL,
21-
`redirect_uri` varchar(200) NOT NULL,
22-
PRIMARY KEY (`client_id`)
19+
`client_id` varchar(20) NOT NULL,
20+
`client_secret` varchar(200) NOT NULL,
21+
`redirect_uri` varchar(200) NOT NULL,
22+
PRIMARY KEY (`client_id`)
2323
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
2424

2525
CREATE TABLE `access_tokens` (
26-
`oauth_token` varchar(40) NOT NULL,
27-
`client_id` varchar(20) NOT NULL,
28-
`user_id` int(11) UNSIGNED NOT NULL,
29-
`expires` int(11) NOT NULL,
30-
`scope` varchar(200) DEFAULT NULL,
31-
PRIMARY KEY (`oauth_token`)
26+
`oauth_token` varchar(40) NOT NULL,
27+
`client_id` varchar(20) NOT NULL,
28+
`user_id` int(11) UNSIGNED NOT NULL,
29+
`expires` int(11) NOT NULL,
30+
`scope` varchar(200) DEFAULT NULL,
31+
PRIMARY KEY (`oauth_token`)
3232
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
3333

3434
CREATE TABLE `refresh_tokens` (
35-
`oauth_token` varchar(40) NOT NULL,
36-
`refresh_token` varchar(40) NOT NULL,
37-
`client_id` varchar(20) NOT NULL,
38-
`user_id` int(11) UNSIGNED NOT NULL,
39-
`expires` int(11) NOT NULL,
40-
`scope` varchar(200) DEFAULT NULL,
41-
PRIMARY KEY (`oauth_token`)
35+
`oauth_token` varchar(40) NOT NULL,
36+
`refresh_token` varchar(40) NOT NULL,
37+
`client_id` varchar(20) NOT NULL,
38+
`user_id` int(11) UNSIGNED NOT NULL,
39+
`expires` int(11) NOT NULL,
40+
`scope` varchar(200) DEFAULT NULL,
41+
PRIMARY KEY (`oauth_token`)
4242
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

‎server/examples/pdo/protected_resource.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
$oauth = new OAuth2(new OAuth2StoragePDO(newPDO()));
1818

1919
try {
20-
$token = $oauth->getBearerToken();
21-
$oauth->verifyAccessToken($token);
20+
$token = $oauth->getBearerToken();
21+
$oauth->verifyAccessToken($token);
2222
} catch (OAuth2ServerException $oauthError) {
23-
$oauthError->sendHttpResponse();
23+
$oauthError->sendHttpResponse();
2424
}
2525

2626
// With a particular scope, you'd do:
@@ -29,10 +29,10 @@
2929
?>
3030

3131
<html>
32-
<head>
32+
<head>
3333
<title>Hello!</title>
34-
</head>
35-
<body>
34+
</head>
35+
<body>
3636
<p>This is a secret.</p>
37-
</body>
37+
</body>
3838
</html>

‎server/examples/pdo/token.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616

1717
$oauth = new OAuth2(new OAuth2StoragePDO(newPDO()));
1818
try {
19-
$response = $oauth->grantAccessToken();
20-
$response->send();
21-
}
22-
catch (OAuth2ServerException $oauthError) {
23-
$oauthError->getHttpResponse()->send();
19+
$response = $oauth->grantAccessToken();
20+
$response->send();
21+
} catch (OAuth2ServerException $oauthError) {
22+
$oauthError->getHttpResponse()->send();
2423
}

‎tests/OAuth2/Tests/Fixtures/OAuth2GrantCodeStub.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public function getLastAuthCode()
2828
return end($this->authCodes);
2929
}
3030

31-
public function createAuthCode($code, IOAuth2Client $client, $data, $redirect_uri, $expires, $scope = NULL)
31+
public function createAuthCode($code, IOAuth2Client $client, $data, $redirectUri, $expires, $scope = null)
3232
{
33-
$token = new OAuth2AuthCode($client->getPublicId(), $code, $expires, $scope, $data, $redirect_uri);
33+
$token = new OAuth2AuthCode($client->getPublicId(), $code, $expires, $scope, $data, $redirectUri);
3434
$this->authCodes[$code] = $token;
3535
}
3636

‎tests/OAuth2/Tests/Fixtures/OAuth2StorageStub.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,16 @@ public function getClients()
3333
return $this->clients;
3434
}
3535

36-
public function checkClientCredentials(IOAuth2Client $client, $client_secret = NULL)
36+
public function checkClientCredentials(IOAuth2Client $client, $clientSecret = null)
3737
{
38-
return $client->checkSecret($client_secret);
38+
return $client->checkSecret($clientSecret);
3939
}
4040

41-
public function createAccessToken($oauth_token, IOAuth2Client $client, $data, $expires, $scope = NULL)
41+
public function createAccessToken($oauthToken, IOAuth2Client $client, $data, $expires, $scope = null)
4242
{
43-
$token = new OAuth2AccessToken($client->getPublicId(), $oauth_token, $expires, $scope, $data);
44-
$this->accessTokens[$oauth_token] = $token;
43+
$token = new OAuth2AccessToken($client->getPublicId(), $oauthToken, $expires, $scope, $data);
44+
45+
$this->accessTokens[$oauthToken] = $token;
4546
}
4647

4748
public function getAccessToken($oauth_token)
@@ -66,8 +67,8 @@ public function setAllowedGrantTypes(array $types)
6667
$this->allowedGrantTypes = $types;
6768
}
6869

69-
public function checkRestrictedGrantType(IOAuth2Client $client, $grant_type)
70+
public function checkRestrictedGrantType(IOAuth2Client $client, $grantType)
7071
{
71-
return in_array($grant_type, $this->allowedGrantTypes);
72+
return in_array($grantType, $this->allowedGrantTypes);
7273
}
7374
}

‎tests/OAuth2/Tests/Model/OAuth2TokenTest.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use OAuth2\Model\OAuth2Token;
66

7-
class OAuth2TokenTest extends \PHPUnit_Framework_TestCase {
8-
9-
public function testConstruct() {
10-
7+
class OAuth2TokenTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testConstruct()
10+
{
1111
$expiresAt = time() + 42;
1212
$data = new \stdClass;
1313

@@ -23,14 +23,15 @@ public function testConstruct() {
2323
}
2424

2525
/** @dataProvider getTestExpiresData */
26-
public function testExpires($offset, $expired) {
27-
26+
public function testExpires($offset, $expired)
27+
{
2828
$token = new OAuth2Token('foo', 'bar', time() + $offset);
2929

3030
$this->assertSame($expired, $token->hasExpired());
3131
}
3232

33-
public function getTestExpiresData() {
33+
public function getTestExpiresData()
34+
{
3435
return array(
3536
array(-10, true),
3637
array(-5, true),
@@ -39,4 +40,3 @@ public function getTestExpiresData() {
3940
);
4041
}
4142
}
42-

‎tests/OAuth2OutputTest.php

+75-73
Original file line numberDiff line numberDiff line change
@@ -8,83 +8,85 @@
88
/**
99
* OAuth2 test cases that invovle capturing output.
1010
*/
11-
class OAuth2OutputTest extends PHPUnit_Framework_TestCase {
12-
13-
/**
14-
* @var OAuth2
15-
*/
16-
private $fixture;
17-
18-
/**
19-
* Tests OAuth2->grantAccessToken() with successful Auth code grant
20-
*
21-
*/
22-
public function testGrantAccessTokenWithGrantAuthCodeSuccess() {
23-
$request = new Request(
24-
array('grant_type' => OAuth2::GRANT_TYPE_AUTH_CODE, 'redirect_uri' => 'http://www.example.com/my/subdir', 'client_id' => 'my_little_app', 'client_secret' => 'b', 'code'=> 'foo')
25-
);
26-
$storedToken = new OAuth2AuthCode('my_little_app', '', time() + 60, NULL, NULL, 'http://www.example.com');
11+
class OAuth2OutputTest extends PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var OAuth2
15+
*/
16+
private $fixture;
2717

28-
$mockStorage = $this->createBaseMock('OAuth2\IOAuth2GrantCode');
29-
$mockStorage->expects($this->any())
30-
->method('getAuthCode')
31-
->will($this->returnValue($storedToken));
32-
33-
$this->fixture = new OAuth2($mockStorage);
34-
$response = $this->fixture->grantAccessToken($request);
18+
/**
19+
* Tests OAuth2->grantAccessToken() with successful Auth code grant
20+
*
21+
*/
22+
public function testGrantAccessTokenWithGrantAuthCodeSuccess()
23+
{
24+
$request = new Request(
25+
array('grant_type' => OAuth2::GRANT_TYPE_AUTH_CODE, 'redirect_uri' => 'http://www.example.com/my/subdir', 'client_id' => 'my_little_app', 'client_secret' => 'b', 'code'=> 'foo')
26+
);
27+
$storedToken = new OAuth2AuthCode('my_little_app', '', time() + 60, null, null, 'http://www.example.com');
3528

36-
// Successful token grant will return a JSON encoded token:
37-
$this->assertRegexp('/{"access_token":".*","expires_in":\d+,"token_type":"bearer"/', $response->getContent());
38-
}
39-
40-
/**
41-
* Tests OAuth2->grantAccessToken() with successful Auth code grant, but without redreict_uri in the input
42-
*/
43-
public function testGrantAccessTokenWithGrantAuthCodeSuccessWithoutRedirect() {
44-
$request = new Request(
45-
array('grant_type' => OAuth2::GRANT_TYPE_AUTH_CODE, 'client_id' => 'my_little_app', 'client_secret' => 'b', 'code'=> 'foo')
46-
);
47-
$storedToken = new OAuth2AuthCode('my_little_app', '', time() + 60, NULL, NULL, 'http://www.example.com');
48-
49-
$mockStorage = $this->createBaseMock('OAuth2\IOAuth2GrantCode');
50-
$mockStorage->expects($this->any())
51-
->method('getAuthCode')
52-
->will($this->returnValue($storedToken));
53-
54-
$this->fixture = new OAuth2($mockStorage);
55-
$this->fixture->setVariable(OAuth2::CONFIG_ENFORCE_INPUT_REDIRECT, false);
56-
$response = $this->fixture->grantAccessToken($request);
29+
$mockStorage = $this->createBaseMock('OAuth2\IOAuth2GrantCode');
30+
$mockStorage->expects($this->any())
31+
->method('getAuthCode')
32+
->will($this->returnValue($storedToken));
33+
34+
$this->fixture = new OAuth2($mockStorage);
35+
$response = $this->fixture->grantAccessToken($request);
36+
37+
// Successful token grant will return a JSON encoded token:
38+
$this->assertRegexp('/{"access_token":".*","expires_in":\d+,"token_type":"bearer"/', $response->getContent());
39+
}
40+
41+
/**
42+
* Tests OAuth2->grantAccessToken() with successful Auth code grant, but without redreict_uri in the input
43+
*/
44+
public function testGrantAccessTokenWithGrantAuthCodeSuccessWithoutRedirect()
45+
{
46+
$request = new Request(
47+
array('grant_type' => OAuth2::GRANT_TYPE_AUTH_CODE, 'client_id' => 'my_little_app', 'client_secret' => 'b', 'code'=> 'foo')
48+
);
49+
$storedToken = new OAuth2AuthCode('my_little_app', '', time() + 60, null, null, 'http://www.example.com');
50+
51+
$mockStorage = $this->createBaseMock('OAuth2\IOAuth2GrantCode');
52+
$mockStorage->expects($this->any())
53+
->method('getAuthCode')
54+
->will($this->returnValue($storedToken));
55+
56+
$this->fixture = new OAuth2($mockStorage);
57+
$this->fixture->setVariable(OAuth2::CONFIG_ENFORCE_INPUT_REDIRECT, false);
58+
$response = $this->fixture->grantAccessToken($request);
59+
60+
// Successful token grant will return a JSON encoded token:
61+
$this->assertRegexp('/{"access_token":".*","expires_in":\d+,"token_type":"bearer"/', $response->getContent());
62+
}
5763

58-
// Successful token grant will return a JSON encoded token:
59-
$this->assertRegexp('/{"access_token":".*","expires_in":\d+,"token_type":"bearer"/', $response->getContent());
60-
}
61-
6264
// Utility methods
63-
64-
/**
65-
*
66-
* @param string $interfaceName
67-
*/
68-
protected function createBaseMock($interfaceName) {
6965

70-
$client = new OAuth2Client('my_little_app');
66+
/**
67+
*
68+
* @param string $interfaceName
69+
*/
70+
protected function createBaseMock($interfaceName)
71+
{
72+
$client = new OAuth2Client('my_little_app');
73+
74+
$mockStorage = $this->getMock($interfaceName);
75+
$mockStorage->expects($this->any())
76+
->method('getClient')
77+
->will($this->returnCallback(function ($id) use ($client) {
78+
if ('my_little_app' === $id) {
79+
return $client;
80+
}
81+
}));
82+
$mockStorage->expects($this->any())
83+
->method('checkClientCredentials')
84+
->will($this->returnValue(true)); // Always return true for any combination of user/pass
85+
$mockStorage->expects($this->any())
86+
->method('checkRestrictedGrantType')
87+
->will($this->returnValue(true)); // Always return true for any combination of user/pass
88+
89+
return $mockStorage;
90+
}
7191

72-
$mockStorage = $this->getMock($interfaceName);
73-
$mockStorage->expects($this->any())
74-
->method('getClient')
75-
->will($this->returnCallback(function($id) use ($client) {
76-
if ('my_little_app' === $id) {
77-
return $client;
78-
}
79-
}));
80-
$mockStorage->expects($this->any())
81-
->method('checkClientCredentials')
82-
->will($this->returnValue(TRUE)); // Always return true for any combination of user/pass
83-
$mockStorage->expects($this->any())
84-
->method('checkRestrictedGrantType')
85-
->will($this->returnValue(TRUE)); // Always return true for any combination of user/pass
86-
87-
return $mockStorage;
88-
}
89-
9092
}

‎tests/OAuth2Test.php

+1,048-1,033
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.