李伟乐 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 ( import (
"bytes" "bytes"

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

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