李伟乐 2 years ago
parent 9ad8b647c2
commit 8b2e767347
  1. 18
      internal/token/token_test.go
  2. 2
      middleware/jwt/aes.go
  3. 11
      middleware/jwt/jwt.go
  4. 2
      middleware/jwt/token.go

@ -1,18 +0,0 @@
package token
import (
"fmt"
"testing"
)
func TestGetToken(t *testing.T) {
tokenKey := "JssLx22bjQwnyqby"
token, uniqId, err := GetToken(tokenKey, &UserInfo{
UserId: 111,
UserType: "admin",
Permissions: []string{"user:search"},
})
fmt.Println(token)
fmt.Println(uniqId, err)
fmt.Println(Parse(tokenKey, token))
}

@ -1,4 +1,4 @@
package token
package jwt
import (
"bytes"

@ -3,7 +3,6 @@ package jwt
import (
"context"
"git.diulo.com/mogfee/kit/errors"
"git.diulo.com/mogfee/kit/internal/token"
"git.diulo.com/mogfee/kit/middleware"
"git.diulo.com/mogfee/kit/transport"
)
@ -16,7 +15,7 @@ type userIdKey struct{}
type authKey struct {
}
type ParseFunc func(key string, tokenStr string) (*token.UserInfo, error)
type ParseFunc func(key string, tokenStr string) (*UserInfo, error)
type JwtOption func(o *options)
func WithJwtKey(jwtKey string) JwtOption {
@ -54,7 +53,7 @@ func JWT(opts ...JwtOption) middleware.Middleware {
var cfg = &options{
jwtKey: "JssLx22bjQwnyqby",
validatePermission: InSlice,
parseFunc: token.Parse,
parseFunc: Parse,
}
for _, o := range opts {
o(cfg)
@ -99,11 +98,11 @@ func InSlice(validatePermission []string, key string) bool {
}
return false
}
func SetUserContext(ctx context.Context, user *token.UserInfo) context.Context {
func SetUserContext(ctx context.Context, user *UserInfo) context.Context {
return context.WithValue(ctx, userIdKey{}, user)
}
func FromUserContext(ctx context.Context) (user *token.UserInfo, ok bool) {
user, ok = ctx.Value(userIdKey{}).(*token.UserInfo)
func FromUserContext(ctx context.Context) (user *UserInfo, ok bool) {
user, ok = ctx.Value(userIdKey{}).(*UserInfo)
return
}

@ -1,4 +1,4 @@
package token
package jwt
import (
"encoding/json"
Loading…
Cancel
Save