You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.4 KiB

1 year ago
package jwt
import (
"context"
11 months ago
"git.diulo.com/mogfee/kit/errors"
1 year ago
"git.diulo.com/mogfee/kit/transport"
"git.diulo.com/mogfee/kit/transport/http"
"strings"
)
1 year ago
type JwtDefault struct {
1 year ago
}
1 year ago
func (j *JwtDefault) GetToken(ctx context.Context, key string) (tokenStr string) {
1 year ago
arr := strings.Split(key, ":")
if len(arr) != 2 {
return ""
}
switch arr[0] {
case "cookie":
if tr, ok := transport.FromServerContext(ctx); ok {
if tr1, ok := tr.(http.Transporter); ok {
if co, err := tr1.Request().Cookie(arr[1]); err == nil {
return co.Value
}
}
}
case "header":
if tr, ok := transport.FromServerContext(ctx); ok {
return tr.RequestHeader().Get(arr[1])
}
case "query":
if tr, ok := transport.FromServerContext(ctx); ok {
if ht, ok := tr.(http.Transporter); ok {
return ht.Request().URL.Query().Get(arr[1])
}
}
}
return ""
}
1 year ago
func (j *JwtDefault) ParseToken(ctx context.Context, key string, token string) (*UserInfo, error) {
1 year ago
return Parse(key, token)
1 year ago
}
1 year ago
func (j *JwtDefault) Validate(ctx context.Context, permission string, permissions []string) error {
1 year ago
allowPers := strings.Split(permission, "|")
allowMap := make(map[string]bool, len(allowPers))
for _, v := range allowPers {
allowMap[v] = true
}
for _, v := range permissions {
if allowMap[v] {
return nil
}
}
11 months ago
return errors.Forbidden("TOKEN_PERMISSION_BAD", "权限不足")
1 year ago
}