master
parent
7d77373168
commit
9e80f60a59
19 changed files with 501 additions and 839 deletions
@ -0,0 +1,37 @@ |
||||
package jwt |
||||
|
||||
import "golang.org/x/net/context" |
||||
|
||||
type userIdKey struct{} |
||||
type authKey struct{} |
||||
type needAuthKey struct{} |
||||
|
||||
func SetUserContext(ctx context.Context, user *UserInfo) context.Context { |
||||
return context.WithValue(ctx, userIdKey{}, user) |
||||
} |
||||
func FromUserContext(ctx context.Context) (user *UserInfo, ok bool) { |
||||
user, ok = ctx.Value(userIdKey{}).(*UserInfo) |
||||
return |
||||
} |
||||
|
||||
func SetAuthKeyContext(ctx context.Context, key string) context.Context { |
||||
return context.WithValue(ctx, authKey{}, key) |
||||
} |
||||
func FromAuthKeyContext(ctx context.Context) string { |
||||
v := ctx.Value(authKey{}) |
||||
if v == nil { |
||||
return "" |
||||
} |
||||
return v.(string) |
||||
} |
||||
|
||||
func SetNeedAuthContext(ctx context.Context, auth bool) context.Context { |
||||
return context.WithValue(ctx, needAuthKey{}, auth) |
||||
} |
||||
func FromNeedAuthContext(ctx context.Context) bool { |
||||
v := ctx.Value(needAuthKey{}) |
||||
if v == nil { |
||||
return false |
||||
} |
||||
return v.(bool) |
||||
} |
@ -0,0 +1,62 @@ |
||||
package jwt |
||||
|
||||
import ( |
||||
"context" |
||||
"git.diulo.com/mogfee/kit/errors" |
||||
"git.diulo.com/mogfee/kit/transport" |
||||
"git.diulo.com/mogfee/kit/transport/http" |
||||
"strings" |
||||
) |
||||
|
||||
type jwtDefault struct { |
||||
} |
||||
|
||||
func (j *jwtDefault) GetToken(ctx context.Context, key string) (tokenStr string) { |
||||
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 "" |
||||
} |
||||
|
||||
func (j *jwtDefault) ParseToken(ctx context.Context, key string, token string) (*UserInfo, error) { |
||||
userInfo, err := Parse(key, token) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
return userInfo, nil |
||||
} |
||||
|
||||
func (j *jwtDefault) Validate(ctx context.Context, permission string, permissions []string) error { |
||||
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 |
||||
} |
||||
} |
||||
return errors.Unauthorized("TOKEN_PERMISSION_BAD", "") |
||||
} |
@ -1,126 +0,0 @@ |
||||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.28.1
|
||||
// protoc v3.17.3
|
||||
// source: auth.proto
|
||||
|
||||
package auth |
||||
|
||||
import ( |
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect" |
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl" |
||||
descriptorpb "google.golang.org/protobuf/types/descriptorpb" |
||||
reflect "reflect" |
||||
) |
||||
|
||||
const ( |
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) |
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) |
||||
) |
||||
|
||||
var file_auth_proto_extTypes = []protoimpl.ExtensionInfo{ |
||||
{ |
||||
ExtendedType: (*descriptorpb.MethodOptions)(nil), |
||||
ExtensionType: (*string)(nil), |
||||
Field: 1111, |
||||
Name: "auth.auth_key", |
||||
Tag: "bytes,1111,opt,name=auth_key", |
||||
Filename: "auth.proto", |
||||
}, |
||||
{ |
||||
ExtendedType: (*descriptorpb.MethodOptions)(nil), |
||||
ExtensionType: (*bool)(nil), |
||||
Field: 1112, |
||||
Name: "auth.auth", |
||||
Tag: "varint,1112,opt,name=auth", |
||||
Filename: "auth.proto", |
||||
}, |
||||
{ |
||||
ExtendedType: (*descriptorpb.MethodOptions)(nil), |
||||
ExtensionType: (*bool)(nil), |
||||
Field: 1113, |
||||
Name: "auth.auto_auth", |
||||
Tag: "varint,1113,opt,name=auto_auth", |
||||
Filename: "auth.proto", |
||||
}, |
||||
} |
||||
|
||||
// Extension fields to descriptorpb.MethodOptions.
|
||||
var ( |
||||
// 权限|分割多个权限
|
||||
//
|
||||
// optional string auth_key = 1111;
|
||||
E_AuthKey = &file_auth_proto_extTypes[0] |
||||
// 是否必须授权
|
||||
//
|
||||
// optional bool auth = 1112;
|
||||
E_Auth = &file_auth_proto_extTypes[1] |
||||
// 可以不授权
|
||||
//
|
||||
// optional bool auto_auth = 1113;
|
||||
E_AutoAuth = &file_auth_proto_extTypes[2] |
||||
) |
||||
|
||||
var File_auth_proto protoreflect.FileDescriptor |
||||
|
||||
var file_auth_proto_rawDesc = []byte{ |
||||
0x0a, 0x0a, 0x61, 0x75, 0x74, 0x68, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x61, 0x75, |
||||
0x74, 0x68, 0x1a, 0x20, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, |
||||
0x62, 0x75, 0x66, 0x2f, 0x64, 0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 0x6f, 0x72, 0x2e, 0x70, |
||||
0x72, 0x6f, 0x74, 0x6f, 0x3a, 0x3a, 0x0a, 0x08, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x6b, 0x65, 0x79, |
||||
0x12, 0x1e, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, |
||||
0x75, 0x66, 0x2e, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, |
||||
0x18, 0xd7, 0x08, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x75, 0x74, 0x68, 0x4b, 0x65, 0x79, |
||||
0x3a, 0x33, 0x0a, 0x04, 0x61, 0x75, 0x74, 0x68, 0x12, 0x1e, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, |
||||
0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x4d, 0x65, 0x74, 0x68, 0x6f, |
||||
0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0xd8, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, |
||||
0x04, 0x61, 0x75, 0x74, 0x68, 0x3a, 0x3c, 0x0a, 0x09, 0x61, 0x75, 0x74, 0x6f, 0x5f, 0x61, 0x75, |
||||
0x74, 0x68, 0x12, 0x1e, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e, 0x70, 0x72, 0x6f, 0x74, |
||||
0x6f, 0x62, 0x75, 0x66, 0x2e, 0x4d, 0x65, 0x74, 0x68, 0x6f, 0x64, 0x4f, 0x70, 0x74, 0x69, 0x6f, |
||||
0x6e, 0x73, 0x18, 0xd9, 0x08, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x61, 0x75, 0x74, 0x6f, 0x41, |
||||
0x75, 0x74, 0x68, 0x42, 0x2b, 0x5a, 0x29, 0x67, 0x69, 0x74, 0x2e, 0x64, 0x69, 0x75, 0x6c, 0x6f, |
||||
0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6d, 0x6f, 0x67, 0x66, 0x65, 0x65, 0x2f, 0x6b, 0x69, 0x74, 0x2f, |
||||
0x74, 0x68, 0x69, 0x72, 0x64, 0x5f, 0x70, 0x61, 0x72, 0x74, 0x79, 0x2f, 0x61, 0x75, 0x74, 0x68, |
||||
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, |
||||
} |
||||
|
||||
var file_auth_proto_goTypes = []interface{}{ |
||||
(*descriptorpb.MethodOptions)(nil), // 0: google.protobuf.MethodOptions
|
||||
} |
||||
var file_auth_proto_depIdxs = []int32{ |
||||
0, // 0: auth.auth_key:extendee -> google.protobuf.MethodOptions
|
||||
0, // 1: auth.auth:extendee -> google.protobuf.MethodOptions
|
||||
0, // 2: auth.auto_auth:extendee -> google.protobuf.MethodOptions
|
||||
3, // [3:3] is the sub-list for method output_type
|
||||
3, // [3:3] is the sub-list for method input_type
|
||||
3, // [3:3] is the sub-list for extension type_name
|
||||
0, // [0:3] is the sub-list for extension extendee
|
||||
0, // [0:0] is the sub-list for field type_name
|
||||
} |
||||
|
||||
func init() { file_auth_proto_init() } |
||||
func file_auth_proto_init() { |
||||
if File_auth_proto != nil { |
||||
return |
||||
} |
||||
type x struct{} |
||||
out := protoimpl.TypeBuilder{ |
||||
File: protoimpl.DescBuilder{ |
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(), |
||||
RawDescriptor: file_auth_proto_rawDesc, |
||||
NumEnums: 0, |
||||
NumMessages: 0, |
||||
NumExtensions: 3, |
||||
NumServices: 0, |
||||
}, |
||||
GoTypes: file_auth_proto_goTypes, |
||||
DependencyIndexes: file_auth_proto_depIdxs, |
||||
ExtensionInfos: file_auth_proto_extTypes, |
||||
}.Build() |
||||
File_auth_proto = out.File |
||||
file_auth_proto_rawDesc = nil |
||||
file_auth_proto_goTypes = nil |
||||
file_auth_proto_depIdxs = nil |
||||
} |
Loading…
Reference in new issue