25 OpenIDAuthorizationCodeInterface
32 if ($connection instanceof \MongoDB) {
33 $this->db = $connection;
35 if (!is_array($connection)) {
36 throw new \InvalidArgumentException(
'First argument to OAuth2\Storage\Mongo must be an instance of MongoDB or a configuration array');
38 $server = sprintf(
'mongodb://%s:%d', $connection[
'host'], $connection[
'port']);
39 $m = new \MongoClient($server);
40 $this->db = $m->{$connection[
'database']};
43 $this->config = array_merge(array(
44 'client_table' =>
'oauth_clients',
45 'access_token_table' =>
'oauth_access_tokens',
46 'refresh_token_table' =>
'oauth_refresh_tokens',
47 'code_table' =>
'oauth_authorization_codes',
48 'user_table' =>
'oauth_users',
49 'jwt_table' =>
'oauth_jwt',
56 return $this->db->{$this->config[$name]};
62 if ($result = $this->collection(
'client_table')->findOne(array(
'client_id' => $client_id))) {
63 return $result[
'client_secret'] == $client_secret;
71 if (!$result = $this->collection(
'client_table')->findOne(array(
'client_id' => $client_id))) {
75 return empty($result[
'client_secret']);
81 $result = $this->collection(
'client_table')->findOne(array(
'client_id' => $client_id));
83 return is_null($result) ?
false : $result;
86 public function setClientDetails($client_id, $client_secret =
null, $redirect_uri =
null, $grant_types =
null, $scope =
null, $user_id =
null)
88 if ($this->getClientDetails($client_id)) {
89 $this->collection(
'client_table')->update(
90 array(
'client_id' => $client_id),
91 array(
'$set' => array(
92 'client_secret' => $client_secret,
93 'redirect_uri' => $redirect_uri,
94 'grant_types' => $grant_types,
96 'user_id' => $user_id,
101 'client_id' => $client_id,
102 'client_secret' => $client_secret,
103 'redirect_uri' => $redirect_uri,
104 'grant_types' => $grant_types,
106 'user_id' => $user_id,
108 $this->collection(
'client_table')->insert($client);
116 $details = $this->getClientDetails($client_id);
117 if (isset($details[
'grant_types'])) {
118 $grant_types = explode(
' ', $details[
'grant_types']);
120 return in_array($grant_type, $grant_types);
130 $token = $this->collection(
'access_token_table')->findOne(array(
'access_token' => $access_token));
132 return is_null($token) ?
false : $token;
135 public function setAccessToken($access_token, $client_id, $user_id, $expires, $scope =
null)
138 if ($this->getAccessToken($access_token)) {
139 $this->collection(
'access_token_table')->update(
140 array(
'access_token' => $access_token),
141 array(
'$set' => array(
142 'client_id' => $client_id,
143 'expires' => $expires,
144 'user_id' => $user_id,
150 'access_token' => $access_token,
151 'client_id' => $client_id,
152 'expires' => $expires,
153 'user_id' => $user_id,
156 $this->collection(
'access_token_table')->insert($token);
164 $this->collection(
'access_token_table')->remove(array(
'access_token' => $access_token));
171 $code = $this->collection(
'code_table')->findOne(array(
'authorization_code' => $code));
173 return is_null($code) ?
false : $code;
176 public function setAuthorizationCode($code, $client_id, $user_id, $redirect_uri, $expires, $scope =
null, $id_token =
null)
179 if ($this->getAuthorizationCode($code)) {
180 $this->collection(
'code_table')->update(
181 array(
'authorization_code' => $code),
182 array(
'$set' => array(
183 'client_id' => $client_id,
184 'user_id' => $user_id,
185 'redirect_uri' => $redirect_uri,
186 'expires' => $expires,
188 'id_token' => $id_token,
193 'authorization_code' => $code,
194 'client_id' => $client_id,
195 'user_id' => $user_id,
196 'redirect_uri' => $redirect_uri,
197 'expires' => $expires,
199 'id_token' => $id_token,
201 $this->collection(
'code_table')->insert($token);
209 $this->collection(
'code_table')->remove(array(
'authorization_code' => $code));
217 if ($user = $this->getUser($username)) {
218 return $this->checkPassword($user, $password);
226 if ($user = $this->getUser($username)) {
227 $user[
'user_id'] = $user[
'username'];
236 $token = $this->collection(
'refresh_token_table')->findOne(array(
'refresh_token' => $refresh_token));
238 return is_null($token) ?
false : $token;
241 public function setRefreshToken($refresh_token, $client_id, $user_id, $expires, $scope =
null)
244 'refresh_token' => $refresh_token,
245 'client_id' => $client_id,
246 'user_id' => $user_id,
247 'expires' => $expires,
250 $this->collection(
'refresh_token_table')->insert($token);
257 $this->collection(
'refresh_token_table')->remove(array(
'refresh_token' => $refresh_token));
265 return $user[
'password'] == $password;
270 $result = $this->collection(
'user_table')->findOne(array(
'username' => $username));
272 return is_null($result) ?
false : $result;
275 public function setUser($username, $password, $firstName =
null, $lastName =
null)
277 if ($this->getUser($username)) {
278 $this->collection(
'user_table')->update(
279 array(
'username' => $username),
280 array(
'$set' => array(
281 'password' => $password,
282 'first_name' => $firstName,
283 'last_name' => $lastName
288 'username' => $username,
289 'password' => $password,
290 'first_name' => $firstName,
291 'last_name' => $lastName
293 $this->collection(
'user_table')->insert($user);
301 $result = $this->collection(
'jwt_table')->findOne(array(
302 'client_id' => $client_id,
303 'subject' => $subject
306 return is_null($result) ?
false : $result[
'key'];
311 if (!$clientDetails = $this->getClientDetails($client_id)) {
315 if (isset($clientDetails[
'scope'])) {
316 return $clientDetails[
'scope'];
322 public function getJti($client_id, $subject, $audience, $expiration, $jti)
325 throw new \Exception(
'getJti() for the MongoDB driver is currently unimplemented.');
328 public function setJti($client_id, $subject, $audience, $expiration, $jti)
331 throw new \Exception(
'setJti() for the MongoDB driver is currently unimplemented.');