-
Notifications
You must be signed in to change notification settings - Fork 52
/
auth.go
92 lines (72 loc) · 1.68 KB
/
auth.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
91
92
package authentication
import (
"net/url"
)
type GrantType string
func (g GrantType) String() string {
return string(g)
}
type ResponseType string
func (r ResponseType) String() string {
return string(r)
}
type TokenRequest struct {
grantType GrantType
params url.Values
responseTypes []ResponseType
}
func NewTokenRequest(grantType GrantType) *TokenRequest {
return &TokenRequest{
grantType: grantType,
params: make(url.Values),
}
}
func (r *TokenRequest) GrantType() GrantType {
return r.grantType
}
func (r *TokenRequest) ResponseTypes() []ResponseType {
return r.responseTypes
}
func (r *TokenRequest) GetTokenParam(key string) string {
return r.params.Get(key)
}
func (r *TokenRequest) SetResponseType(responseTypes ...ResponseType) {
r.responseTypes = responseTypes
}
func (r *TokenRequest) SetTokenParam(key, value string) {
r.params.Set(key, value)
}
func (r *TokenRequest) WithOption(opt TokenOption) *TokenRequest {
opt(r)
return r
}
func (r *TokenRequest) SetValue(v url.Values) {
if r == nil {
panic("authentication: token request is nil")
}
v.Set("grant_type", r.grantType.String())
var responseTypeStr string
for i, t := range r.responseTypes {
if i > 0 {
responseTypeStr += ","
}
responseTypeStr += t.String()
}
v.Set("response_type", responseTypeStr)
for k, ss := range r.params {
for _, s := range ss {
v.Set(k, s)
}
}
}
type TokenOption func(r *TokenRequest)
func SetResponseType(responseTypes ...ResponseType) TokenOption {
return func(r *TokenRequest) {
r.SetResponseType(responseTypes...)
}
}
func SetTokenParam(key, value string) TokenOption {
return func(r *TokenRequest) {
r.SetTokenParam(key, value)
}
}