|
3 | 3 | /**
|
4 | 4 | * @file
|
5 | 5 | * Sample OAuth2 Library Mongo DB Implementation.
|
6 |
| - * |
| 6 | + * |
7 | 7 | */
|
8 | 8 |
|
9 | 9 | require_once __DIR__.'/../../../../lib/OAuth2.php';
|
|
14 | 14 | /**
|
15 | 15 | * Mongo storage engine for the OAuth2 Library.
|
16 | 16 | */
|
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 | + } |
187 | 204 | }
|
0 commit comments