9
9
use Illuminate \Support \Carbon ;
10
10
use Illuminate \Support \Facades \Gate ;
11
11
use Illuminate \Support \Str ;
12
+ use Illuminate \Support \Facades \Cache ;
13
+
14
+ use Auth0 \SDK \Token ;
15
+ use Auth0 \SDK \Configuration \SdkConfiguration ;
12
16
13
17
class GsvAuth0Provider
14
18
{
15
- protected $ auth0_domain ;
16
-
17
- protected $ api_identifier ;
18
-
19
- protected $ jwks_uri ;
19
+ protected $ configuration ;
20
20
21
- protected $ cache ;
22
-
23
- public function __construct (?string $ auth0_domain , ?string $ api_identifier , ?string $ jwks_uri = null )
21
+ public function __construct (?string $ domain , ?string $ audience )
24
22
{
25
- $ this ->auth0_domain = $ auth0_domain ;
26
- $ this ->api_identifier = $ api_identifier ;
27
-
28
- $ this ->jwks_uri = $ jwks_uri ?: sprintf ('https://%s/.well-known/jwks.json ' , $ this ->auth0_domain );
29
-
30
- $ this ->cache = app ()->make ('cache.store ' );
31
-
32
- // Add a neat little custom method that only caches if a condition is met
33
- $ this ->cache ->macro ('rememberWhen ' , function ($ condition , $ key , $ ttl , $ callback ) {
34
- if ($ condition ) {
35
- return $ this ->remember ($ key , $ ttl , $ callback );
36
- } else {
37
- return $ callback ();
38
- }
39
- });
23
+ $ this ->configuration = new SdkConfiguration (
24
+ domain: $ domain ,
25
+ audience: [$ audience ],
26
+ clientId: 'dummy ' , // Don't need a real value as we only validate jwt's
27
+ clientSecret: 'dummy ' , // Don't need a real value as we only validate jwt's
28
+ cookieSecret: 'dummy ' , // Don't need a real value as we only validate jwt's
29
+ );
40
30
}
41
31
42
32
/**
43
- * Authenticate the token
33
+ * Authenticate the jwt
44
34
*
45
35
* @param string $token
46
36
* @return self
47
37
*/
48
- public function authenticate (string $ token ): self
38
+ public function authenticate (string $ jwt ): self
49
39
{
50
- if ($ this ->auth0_domain === null ) {
51
- throw new Exception ('Auth0 domain not set ' );
52
- }
53
-
54
- if ($ this ->api_identifier === null ) {
55
- throw new Exception ('API identifier not set ' );
40
+ if ($ this ->configuration === null ) {
41
+ throw new Exception ('Auth0 configuration not set ' );
56
42
}
57
43
58
44
try {
59
- $ info = $ this ->decodeJWT ($ token );
60
-
61
- $ this ->setUser ($ info , $ token );
62
- } catch (InvalidTokenException $ e ) {
45
+ $ token = new Token ($ this ->configuration , $ jwt , \Auth0 \SDK \Token::TYPE_ACCESS_TOKEN );
46
+ $ token ->verify ();
47
+ $ token ->validate ();
48
+ $ this ->setUser ($ token ->toArray (), $ jwt );
49
+ } catch (\Exception $ e ) {
63
50
// Re-throw into a 401
64
51
throw new InvalidTokenException ($ e ->getMessage (), 401 );
65
52
}
@@ -78,14 +65,17 @@ public function loadUserData(Auth0User $user = null): self
78
65
79
66
$ client = app ()->make ('gsv-auth0-user-service ' );
80
67
81
- $ userData = $ this ->cache ->rememberWhen (
82
- $ user ->expires ->isAfter (Carbon::now ()), // Only cache if this condition is met
83
- md5 ($ user ->token ), // The cache key
84
- $ user ->expires ->diffInSeconds (Carbon::now ()), // Cache expires when the auth expires
85
- function () use ($ client , $ user ) {
86
- return $ client ->setToken ($ user ->token )->fetch ($ user ->auth0_id );
87
- }
88
- );
68
+ if ($ user ->expires ->isAfter (Carbon::now ())) {
69
+ $ userData = Cache::remember (
70
+ md5 ($ user ->sub ),
71
+ $ user ->expires ->diffInSeconds (Carbon::now ()),
72
+ function () use ($ client , $ user ) {
73
+ return $ client ->setToken ($ user ->token )->fetch ($ user ->auth0_id );
74
+ }
75
+ );
76
+ } else {
77
+ $ userData = $ client ->setToken ($ user ->token )->fetch ($ user ->auth0_id );
78
+ }
89
79
90
80
if ((isset ($ userData ['status ' ]) && $ userData ['status ' ] === 'Error ' ) || empty ($ userData ['data ' ])) {
91
81
throw new UserNotFoundException ($ userData ['message ' ], 401 );
@@ -158,29 +148,4 @@ protected function setUser(array $info, string $token): self
158
148
159
149
return $ this ;
160
150
}
161
-
162
- /**
163
- * Verify a JWT from Auth0
164
- *
165
- * @see https://github.com/auth0/laravel-auth0/blob/8377bd09644de60d5a8688653589ea299ccd2969/src/Auth0/Login/Auth0Service.php#L206
166
- * @param string $encUser
167
- * @param array $verifierOptions
168
- * @throws InvalidTokenException
169
- * @return array
170
- */
171
- protected function decodeJWT (string $ encUser , array $ verifierOptions = []): array
172
- {
173
- $ jwks_fetcher = app ()->make ('gsv-auth0-jwks-fetcher ' , [
174
- 'cache ' => $ this ->cache ,
175
- ]);
176
- $ jwks = $ jwks_fetcher ->getKeys ($ this ->jwks_uri );
177
-
178
- $ token_verifier = app ()->make ('gsv-auth0-token-verifier ' , [
179
- 'domain ' => $ this ->auth0_domain ,
180
- 'apiIdentifier ' => $ this ->api_identifier ,
181
- 'jwks ' => $ jwks ,
182
- ]);
183
-
184
- return $ token_verifier ->verify ($ encUser , $ verifierOptions );
185
- }
186
151
}
0 commit comments