You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
feat: make getClaims() non experimental, add global cache (#1078)
`supabase.auth.getClaims()` loses `@expermental` status. To further
improve performance since Vercel have now added Fluid Compute which
shares a lot more memory between requests, every client's JWKS cache is
stored in a global variable under the client's storage key.
@@ -242,8 +267,12 @@ export default class GoTrueClient {
242
267
}else{
243
268
this.lock=lockNoOp
244
269
}
245
-
this.jwks={keys: []}
246
-
this.jwks_cached_at=Number.MIN_SAFE_INTEGER
270
+
271
+
if(!this.jwks){
272
+
this.jwks={keys: []}
273
+
this.jwks_cached_at=Number.MIN_SAFE_INTEGER
274
+
}
275
+
247
276
this.mfa={
248
277
verify: this._verify.bind(this),
249
278
enroll: this._enroll.bind(this),
@@ -2946,11 +2975,13 @@ export default class GoTrueClient {
2946
2975
returnjwk
2947
2976
}
2948
2977
2978
+
constnow=Date.now()
2979
+
2949
2980
// try fetching from cache
2950
2981
jwk=this.jwks.keys.find((key)=>key.kid===kid)
2951
2982
2952
2983
// jwk exists and jwks isn't stale
2953
-
if(jwk&&this.jwks_cached_at+JWKS_TTL>Date.now()){
2984
+
if(jwk&&this.jwks_cached_at+JWKS_TTL>now){
2954
2985
returnjwk
2955
2986
}
2956
2987
// jwk isn't cached in memory so we need to fetch it from the well-known endpoint
@@ -2963,8 +2994,10 @@ export default class GoTrueClient {
2963
2994
if(!data.keys||data.keys.length===0){
2964
2995
thrownewAuthInvalidJwtError('JWKS is empty')
2965
2996
}
2997
+
2966
2998
this.jwks=data
2967
-
this.jwks_cached_at=Date.now()
2999
+
this.jwks_cached_at=now
3000
+
2968
3001
// Find the signing key
2969
3002
jwk=data.keys.find((key: any)=>key.kid===kid)
2970
3003
if(!jwk){
@@ -2974,12 +3007,35 @@ export default class GoTrueClient {
2974
3007
}
2975
3008
2976
3009
/**
2977
-
* @experimental This method may change in future versions.
2978
-
* @description Gets the claims from a JWT. If the JWT is symmetric JWTs, it will call getUser() to verify against the server. If the JWT is asymmetric, it will be verified against the JWKS using the WebCrypto API.
3010
+
* Extracts the JWT claims present in the access token by first verifying the
3011
+
* JWT against the server's JSON Web Key Set endpoint
3012
+
* `/.well-known/jwks.json` which is often cached, resulting in significantly
3013
+
* faster responses. Prefer this method over {@link #getUser} which always
3014
+
* sends a request to the Auth server for each JWT.
3015
+
*
3016
+
* If the project is not using an asymmetric JWT signing key (like ECC or
3017
+
* RSA) it always sends a request to the Auth server (similar to {@link
3018
+
* #getUser}) to verify the JWT.
3019
+
*
3020
+
* @param jwt An optional specific JWT you wish to verify, not the one you
3021
+
* can obtain from {@link #getSession}.
3022
+
* @param options Various additional options that allow you to customize the
3023
+
* behavior of this method.
2979
3024
*/
2980
3025
asyncgetClaims(
2981
3026
jwt?: string,
2982
-
jwks: {keys: JWK[]}={keys: []}
3027
+
options: {
3028
+
/**
3029
+
* @deprecated Please use options.jwks instead.
3030
+
*/
3031
+
keys?: JWK[]
3032
+
3033
+
/** If set to `true` the `exp` claim will not be validated against the current time. */
3034
+
allowExpired?: boolean
3035
+
3036
+
/** If set, this JSON Web Key Set is going to have precedence over the cached value available on the server. */
0 commit comments