forked from lucadegasperi/oauth2-server-laravel
/
FluentAuthCode.php
130 lines (117 loc) · 3.73 KB
/
FluentAuthCode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/*
* This file is part of OAuth 2.0 Laravel.
*
* (c) Luca Degasperi <packages@lucadegasperi.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace LucaDegasperi\OAuth2Server\Storage;
use Carbon\Carbon;
use League\OAuth2\Server\Entity\AuthCodeEntity;
use League\OAuth2\Server\Entity\ScopeEntity;
use League\OAuth2\Server\Storage\AuthCodeInterface;
/**
* This is the fluent auth code class.
*
* @author Luca Degasperi <packages@lucadegasperi.com>
*/
class FluentAuthCode extends AbstractFluentAdapter implements AuthCodeInterface
{
/**
* Get the auth code.
*
* @param string $code
*
* @return \League\OAuth2\Server\Entity\AuthCodeEntity
*/
public function get($code)
{
$result = $this->getConnection()->table('oauth_auth_codes')
->where('oauth_auth_codes.id', $code)
->where('oauth_auth_codes.expire_time', '>=', time())
->first();
if (is_null($result)) {
return;
}
return (new AuthCodeEntity($this->getServer()))
->setId($result->id)
->setRedirectUri($result->redirect_uri)
->setExpireTime((int) $result->expire_time);
}
/**
* Get the scopes for an access token.
*
* @param \League\OAuth2\Server\Entity\AuthCodeEntity $token The auth code
*
* @return array Array of \League\OAuth2\Server\Entity\ScopeEntity
*/
public function getScopes(AuthCodeEntity $token)
{
$result = $this->getConnection()->table('oauth_auth_code_scopes')
->select('oauth_scopes.*')
->join('oauth_scopes', 'oauth_auth_code_scopes.scope_id', '=', 'oauth_scopes.id')
->where('oauth_auth_code_scopes.auth_code_id', $token->getId())
->get();
$scopes = [];
foreach ($result as $scope) {
$scopes[] = (new ScopeEntity($this->getServer()))->hydrate([
'id' => $scope->id,
'description' => $scope->description,
]);
}
return $scopes;
}
/**
* Associate a scope with an access token.
*
* @param \League\OAuth2\Server\Entity\AuthCodeEntity $token The auth code
* @param \League\OAuth2\Server\Entity\ScopeEntity $scope The scope
*
* @return void
*/
public function associateScope(AuthCodeEntity $token, ScopeEntity $scope)
{
$this->getConnection()->table('oauth_auth_code_scopes')->insert([
'auth_code_id' => $token->getId(),
'scope_id' => $scope->getId(),
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]);
}
/**
* Delete an access token.
*
* @param \League\OAuth2\Server\Entity\AuthCodeEntity $token The access token to delete
*
* @return void
*/
public function delete(AuthCodeEntity $token)
{
$this->getConnection()->table('oauth_auth_codes')
->where('oauth_auth_codes.id', $token->getId())
->delete();
}
/**
* Create an auth code.
*
* @param string $token The token ID
* @param int $expireTime Token expire time
* @param int $sessionId Session identifier
* @param string $redirectUri Client redirect uri
*
* @return void
*/
public function create($token, $expireTime, $sessionId, $redirectUri)
{
$this->getConnection()->table('oauth_auth_codes')->insert([
'id' => $token,
'session_id' => $sessionId,
'redirect_uri' => $redirectUri,
'expire_time' => $expireTime,
'created_at' => Carbon::now(),
'updated_at' => Carbon::now(),
]);
}
}