feat: setup config (viper =)), jwt settings && session struct
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package jwt
|
||||
|
||||
import jwt2 "github.com/golang-jwt/jwt/v5"
|
||||
|
||||
type UserClaims struct {
|
||||
jwt2.RegisteredClaims
|
||||
UserID uint
|
||||
Sections []string
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package jwt
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
jwt2 "github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
const UserClaimKey = "User-Claims"
|
||||
|
||||
func CreateToken(secret []byte, claims *UserClaims) (string, error) {
|
||||
return jwt2.NewWithClaims(jwt2.SigningMethodHS512, claims).SignedString(secret)
|
||||
}
|
||||
|
||||
func ParseToken(tokenString string, secret []byte) (*UserClaims, error) {
|
||||
token, err := jwt2.ParseWithClaims(tokenString, &UserClaims{}, func(t *jwt2.Token) (interface{}, error) {
|
||||
return secret, nil
|
||||
})
|
||||
|
||||
var claim *UserClaims
|
||||
if token.Claims != nil {
|
||||
cc, ok := token.Claims.(*UserClaims)
|
||||
if ok {
|
||||
claim = cc
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return claim, err
|
||||
}
|
||||
|
||||
if !token.Valid {
|
||||
return claim, errors.New("token is not valid")
|
||||
}
|
||||
|
||||
return claim, nil
|
||||
}
|
||||
Reference in New Issue
Block a user