feat: add gen jwt func

v3
veypi 2 months ago
parent 09a38f910f
commit e485e305c9

@ -28,6 +28,9 @@ type Access []struct {
} }
func (a *Access) Check(target string, tid string, l AuthLevel) bool { func (a *Access) Check(target string, tid string, l AuthLevel) bool {
if l == DoNone {
return true
}
for _, line := range *a { for _, line := range *a {
if target == line.Name && l >= line.Level { if target == line.Name && l >= line.Level {
if line.TID == "" || line.TID == tid { if line.TID == "" || line.TID == tid {

@ -13,11 +13,24 @@ import (
"oa/cfg" "oa/cfg"
"oa/errs" "oa/errs"
"strings" "strings"
"time"
"github.com/golang-jwt/jwt/v5" "github.com/golang-jwt/jwt/v5"
"github.com/veypi/OneBD/rest" "github.com/veypi/OneBD/rest"
) )
func GenJwt(claim *Claims) (string, error) {
if claim.ExpiresAt == nil {
claim.ExpiresAt = jwt.NewNumericDate(time.Now().Add(5 * time.Minute))
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claim)
tokenString, err := token.SignedString(cfg.Config.JWT)
if err != nil {
return "", err
}
return tokenString, nil
}
func CheckJWT(x *rest.X) (*Claims, error) { func CheckJWT(x *rest.X) (*Claims, error) {
authHeader := x.Request.Header.Get("Authorization") authHeader := x.Request.Header.Get("Authorization")
if authHeader == "" { if authHeader == "" {

Loading…
Cancel
Save