🐛 Fix context return null on first time
This commit is contained in:
parent
31bf242587
commit
e7c84a91a2
@ -15,35 +15,42 @@ import (
|
|||||||
|
|
||||||
const authContextBucket = "AuthContext"
|
const authContextBucket = "AuthContext"
|
||||||
|
|
||||||
func Authenticate(access, refresh string, depth int) (models.Account, string, string, error) {
|
func Authenticate(access, refresh string, depth int) (user models.Account, newAccess, newRefresh string, err error) {
|
||||||
var user models.Account
|
var claims security.PayloadClaims
|
||||||
claims, err := security.DecodeJwt(access)
|
claims, err = security.DecodeJwt(access)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if len(refresh) > 0 && depth < 1 {
|
if len(refresh) > 0 && depth < 1 {
|
||||||
// Auto refresh and retry
|
// Auto refresh and retry
|
||||||
access, refresh, err := security.RefreshToken(refresh)
|
newAccess, newRefresh, err = security.RefreshToken(refresh)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return Authenticate(access, refresh, depth+1)
|
return Authenticate(newAccess, newRefresh, depth+1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return user, access, refresh, fiber.NewError(fiber.StatusUnauthorized, fmt.Sprintf("invalid auth key: %v", err))
|
err = fiber.NewError(fiber.StatusUnauthorized, fmt.Sprintf("invalid auth key: %v", err))
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newAccess = access
|
||||||
|
newRefresh = refresh
|
||||||
|
|
||||||
var ctx models.AuthContext
|
var ctx models.AuthContext
|
||||||
|
|
||||||
ctx, lookupErr := GetAuthContext(claims.ID)
|
ctx, lookupErr := GetAuthContext(claims.ID)
|
||||||
if lookupErr == nil {
|
if lookupErr == nil {
|
||||||
log.Debug().Str("jti", claims.ID).Msg("Hit auth context cache once!")
|
log.Debug().Str("jti", claims.ID).Msg("Hit auth context cache once!")
|
||||||
return ctx.Account, access, refresh, nil
|
user = ctx.Account
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, err = GrantAuthContext(claims.ID)
|
ctx, err = GrantAuthContext(claims.ID)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Debug().Str("jti", claims.ID).Err(lookupErr).Msg("Missed auth context cache once!")
|
log.Debug().Str("jti", claims.ID).Err(lookupErr).Msg("Missed auth context cache once!")
|
||||||
return user, access, refresh, nil
|
user = ctx.Account
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return user, access, refresh, fiber.NewError(fiber.StatusUnauthorized, err.Error())
|
err = fiber.NewError(fiber.StatusUnauthorized, err.Error())
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetAuthContext(jti string) (models.AuthContext, error) {
|
func GetAuthContext(jti string) (models.AuthContext, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user