-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPassportPgtServer.php
171 lines (147 loc) · 3.76 KB
/
PassportPgtServer.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
namespace Luchavez\PassportPgtServer\Services;
use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Application;
use Illuminate\Support\Carbon;
use Laravel\Passport\Passport;
use RuntimeException;
/**
* Class PassportPgtServer
*
* @author James Carlo Luchavez <jamescarloluchavez@gmail.com>
*/
class PassportPgtServer
{
/**
* @param Application $application
*/
public function __construct(protected Application $application)
{
//
}
/***** CONFIG-RELATED *****/
/**
* @link https://laravel.com/docs/8.x/passport#installation
*
* @return void
*/
public function setPassportAsApiDriver(): void
{
$key = 'auth.guards.api';
$value = [
'driver' => 'passport',
'provider' => 'users',
];
$apiAuthConfig = config($key);
if ($apiAuthConfig && isset($apiAuthConfig['driver'])) {
if ($apiAuthConfig['driver'] !== $value['driver']) {
throw new RuntimeException('Failed to set passport as api driver. Another driver already set.');
}
} else {
config([$key => $value]);
}
}
/**
* @param Closure|null $load_public_key
* @param Closure|null $load_private_key
* @return void
*/
public function setPassportEncryptionKeys(?Closure $load_public_key, ?Closure $load_private_key): void
{
if (! $this->application->configurationIsCached()) {
if ($load_public_key && $value = $load_public_key()) {
config(['passport.public_key' => $value]);
}
if ($load_private_key && $value = $load_private_key()) {
config(['passport.private_key' => $value]);
}
}
}
/***** TOKEN EXPIRATIONS *****/
/**
* @return bool
*/
public function hashClientSecrets(): bool
{
return config('passport-pgt-server.hash_client_secrets');
}
/**
* @return Carbon
*/
public function getTokensExpiresIn(): Carbon
{
return Carbon::parse(config('passport-pgt-server.access_token_expires_in'));
}
/**
* @return Carbon
*/
public function getRefreshTokensExpiresIn(): Carbon
{
return Carbon::parse(config('passport-pgt-server.refresh_token_expires_in'));
}
/**
* @return Carbon
*/
public function getPersonalAccessTokensExpiresIn(): Carbon
{
return Carbon::parse(config('passport-pgt-server.personal_access_token_expires_in'));
}
/***** MODELS & BUILDERS *****/
/**
* @return string
*/
public function getTokenModel(): string
{
return Passport::tokenModel();
}
/**
* @return Builder
*/
public function getTokenBuilder(): Builder
{
return Passport::tokenModel()::query();
}
/**
* @return string
*/
public function getRefreshTokenModel(): string
{
return Passport::refreshTokenModel();
}
/**
* @return Builder
*/
public function getRefreshTokenBuilder(): Builder
{
return Passport::refreshTokenModel()::query();
}
/**
* @return string
*/
public function getPersonalAccessTokenModel(): string
{
return Passport::personalAccessClientModel();
}
/**
* @return Builder
*/
public function getPersonalAccessTokenBuilder(): Builder
{
return Passport::personalAccessClientModel()::query();
}
/**
* @return string
*/
public function getClientModel(): string
{
return Passport::clientModel();
}
/**
* @return Builder
*/
public function getClientBuilder(): Builder
{
return Passport::clientModel()::query();
}
}