-
Notifications
You must be signed in to change notification settings - Fork 202
/
factory.go
78 lines (65 loc) · 1.51 KB
/
factory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package jwt
import (
"errors"
"reflect"
"github.com/eolinker/eosc/utils/schema"
"github.com/eolinker/apinto/application"
"github.com/eolinker/apinto/application/auth"
)
var _ auth.IAuthFactory = (*factory)(nil)
var driverName = "jwt"
//Register 注册auth驱动工厂
func Register() {
auth.FactoryRegister(driverName, NewFactory())
}
type factory struct {
configType reflect.Type
render *schema.Schema
userType reflect.Type
}
func (f *factory) Render() interface{} {
return f.render
}
func (f *factory) ConfigType() reflect.Type {
return f.configType
}
func (f *factory) UserType() reflect.Type {
return f.userType
}
func (f *factory) Alias() []string {
return []string{
"jwt",
}
}
func (f *factory) Create(tokenName string, position string, rule interface{}) (application.IAuth, error) {
baseConfig, ok := rule.(*application.BaseConfig)
if !ok {
return nil, errors.New("invalid jwt config")
}
cfg, ok := baseConfig.Config().(*Rule)
if !ok {
return nil, errors.New("invalid jwt config")
}
id, err := cfg.ToID()
if err != nil {
return nil, err
}
a := &jwt{
id: id,
tokenName: tokenName,
position: position,
cfg: cfg,
users: application.NewUserManager(),
}
return a, nil
}
//NewFactory 生成一个 auth_apiKey工厂
func NewFactory() auth.IAuthFactory {
typ := reflect.TypeOf((*Config)(nil))
render, _ := schema.Generate(typ, nil)
return &factory{
configType: reflect.TypeOf((*Rule)(nil)),
render: render,
userType: reflect.TypeOf((*User)(nil)),
}
}