session id added to claims

This commit is contained in:
nquidox 2025-09-08 23:58:44 +03:00
parent 360d265672
commit 305044a736
2 changed files with 9 additions and 9 deletions

View file

@ -6,7 +6,7 @@ import (
) )
type JWTProvider interface { type JWTProvider interface {
CreateAccessToken(userUuid string) (string, error) CreateAccessToken(userUuid, sessionUuid string) (string, error)
CreateRefreshToken(refreshUuid string, expires time.Time) *http.Cookie CreateRefreshToken(refreshUuid string, expires time.Time) *http.Cookie
Parse(token string) (string, string, error) Parse(token string) (string, string, error)
} }

View file

@ -33,7 +33,7 @@ func NewJWT(deps Deps) *JWT {
} }
} }
func (j *JWT) CreateAccessToken(userUuid string) (string, error) { func (j *JWT) CreateAccessToken(userUuid, sessionUuid string) (string, error) {
now := time.Now() now := time.Now()
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
@ -42,6 +42,7 @@ func (j *JWT) CreateAccessToken(userUuid string) (string, error) {
"iss": j.Issuer, "iss": j.Issuer,
"nbf": now.Unix(), "nbf": now.Unix(),
"sub": userUuid, "sub": userUuid,
"sid": sessionUuid,
}) })
signedToken, err := token.SignedString([]byte(j.SecretKey)) signedToken, err := token.SignedString([]byte(j.SecretKey))
@ -56,7 +57,6 @@ func (j *JWT) CreateRefreshToken(refreshUuid string, expires time.Time) *http.Co
return &http.Cookie{ return &http.Cookie{
Name: "refresh_uuid", Name: "refresh_uuid",
Value: refreshUuid, Value: refreshUuid,
Path: "",
Expires: expires, Expires: expires,
Secure: true, Secure: true,
HttpOnly: true, HttpOnly: true,
@ -84,16 +84,16 @@ func (j *JWT) Parse(token string) (string, string, error) {
if claims, ok := parse.Claims.(jwt.MapClaims); ok && parse.Valid { if claims, ok := parse.Claims.(jwt.MapClaims); ok && parse.Valid {
userUuid := claims["sub"].(string) userUuid := claims["sub"].(string)
var refreshUuid string var sessionUuid string
if tkn, exists := claims["tkn"]; exists { if sid, exists := claims["sid"]; exists {
if tknStr, okay := tkn.(string); okay { if tknStr, okay := sid.(string); okay {
refreshUuid = tknStr sessionUuid = tknStr
} else { } else {
return "", "", fmt.Errorf("invalid type for 'tkn' claim") return "", "", fmt.Errorf("invalid type for 'sid' claim")
} }
} }
return userUuid, refreshUuid, nil return userUuid, sessionUuid, nil
} }
return "", "", fmt.Errorf("invalid token") return "", "", fmt.Errorf("invalid token")