-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptions.go
90 lines (76 loc) · 1.61 KB
/
options.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
79
80
81
82
83
84
85
86
87
88
89
90
package oauth2s
import (
"github.com/admpub/oauth2/v4"
"github.com/admpub/oauth2/v4/manage"
"github.com/admpub/oauth2/v4/server"
"github.com/golang-jwt/jwt"
)
func NewConfig() *Config {
return &Config{
JWTMethod: jwt.SigningMethodHS512,
HandlerInfo: &HandlerInfo{},
}
}
type Config struct {
JWTKeyID string
JWTKey []byte
JWTMethod jwt.SigningMethod
Store oauth2.TokenStore
ClientStore oauth2.ClientStore
HandlerInfo *HandlerInfo
manager *manage.Manager
server *server.Server
}
func (c *Config) InitConfig(options ...OptionsSetter) *Config {
for _, fn := range options {
fn(c)
}
return c
}
func (c *Config) Init(options ...OptionsSetter) error {
var err error
c.InitConfig(options...)
c.manager, err = NewManager(c)
if err != nil {
return err
}
c.server, err = NewServer(c)
return err
}
func (c *Config) Manager() *manage.Manager {
return c.manager
}
func (c *Config) Server() *server.Server {
return c.server
}
type OptionsSetter func(*Config)
func JWTKeyID(keyID string) OptionsSetter {
return func(c *Config) {
c.JWTKeyID = keyID
}
}
func JWTKey(key []byte) OptionsSetter {
return func(c *Config) {
c.JWTKey = key
}
}
func JWTMethod(method jwt.SigningMethod) OptionsSetter {
return func(c *Config) {
c.JWTMethod = method
}
}
func SetStore(store oauth2.TokenStore) OptionsSetter {
return func(c *Config) {
c.Store = store
}
}
func ClientStore(store oauth2.ClientStore) OptionsSetter {
return func(c *Config) {
c.ClientStore = store
}
}
func SetHandler(handlerInfo *HandlerInfo) OptionsSetter {
return func(c *Config) {
c.HandlerInfo = handlerInfo
}
}