67 lines
1.4 KiB
Go
67 lines
1.4 KiB
Go
package jwt
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"github.com/lestrrat-go/jwx/v2/jwt"
|
|
)
|
|
|
|
type CommonClaims struct {
|
|
ExpiresAt int64 `json:"exp,omitempty"`
|
|
IssuedAt int64 `json:"iat,omitempty"`
|
|
}
|
|
|
|
// AppClaims represent the claims parsed from JWT access token.
|
|
type AppClaims struct {
|
|
ID int `json:"id,omitempty"`
|
|
Sub string `json:"sub,omitempty"`
|
|
Roles []string `json:"roles,omitempty"`
|
|
CommonClaims
|
|
}
|
|
|
|
// ParseClaims parses JWT claims into AppClaims.
|
|
func (c *AppClaims) ParseClaims(claims map[string]any) error {
|
|
id, ok := claims["id"]
|
|
if !ok {
|
|
return errors.New("could not parse claim id")
|
|
}
|
|
c.ID = int(id.(float64))
|
|
|
|
sub, ok := claims[jwt.SubjectKey]
|
|
if !ok {
|
|
return errors.New("could not parse claim sub")
|
|
}
|
|
c.Sub = sub.(string)
|
|
|
|
rl, ok := claims["roles"]
|
|
if !ok {
|
|
return errors.New("could not parse claims roles")
|
|
}
|
|
|
|
var roles []string
|
|
if rl != nil {
|
|
for _, v := range rl.([]any) {
|
|
roles = append(roles, v.(string))
|
|
}
|
|
}
|
|
c.Roles = roles
|
|
|
|
return nil
|
|
}
|
|
|
|
// RefreshClaims represents the claims parsed from JWT refresh token.
|
|
type RefreshClaims struct {
|
|
ID int `json:"id,omitempty"`
|
|
Token string `json:"token,omitempty"`
|
|
CommonClaims
|
|
}
|
|
|
|
// ParseClaims parses the JWT claims into RefreshClaims.
|
|
func (c *RefreshClaims) ParseClaims(claims map[string]any) error {
|
|
token, ok := claims["token"]
|
|
if !ok {
|
|
return errors.New("could not parse claim token")
|
|
}
|
|
c.Token = token.(string)
|
|
return nil
|
|
}
|