-
Notifications
You must be signed in to change notification settings - Fork 17
/
auth_options.go
236 lines (210 loc) · 6.18 KB
/
auth_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package gcorecloud
import "fmt"
// AuthOptionsBuilder build auth options to map
type AuthOptionsBuilder interface {
ToMap() map[string]interface{}
}
// TokenOptionsBuilder build token options to map
type TokenOptionsBuilder interface {
ToMap() map[string]interface{}
}
// AuthOptions gcore API
type AuthOptions struct {
APIURL string `json:"-"`
AuthURL string `json:"-"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
AllowReauth bool `json:"-"`
ClientID string `json:"-"`
}
// ToMap implements AuthOptionsBuilder
func (ao AuthOptions) ToMap() map[string]interface{} {
return map[string]interface{}{
"username": ao.Username,
"password": ao.Password,
}
}
// TokenOptions gcore API
type TokenOptions struct {
APIURL string `json:"-"`
AccessToken string `json:"access,omitempty"`
RefreshToken string `json:"refresh,omitempty"`
AllowReauth bool `json:"-"`
}
// ExtractAccessToken implements AuthResult
func (to TokenOptions) ExtractAccessToken() (string, error) {
return to.AccessToken, nil
}
// ExtractRefreshToken implements AuthResult
func (to TokenOptions) ExtractRefreshToken() (string, error) {
return to.RefreshToken, nil
}
// ExtractTokensPair implements AuthResult
func (to TokenOptions) ExtractTokensPair() (string, string, error) {
return to.AccessToken, to.RefreshToken, nil
}
// ToMap implements TokenOptionsBuilder
func (to TokenOptions) ToMap() map[string]interface{} {
return map[string]interface{}{
"token": to.RefreshToken,
}
}
// APITokenOptions gcore API
type APITokenOptions struct {
APIURL string `json:"-"`
APIToken string `json:"-"`
}
// TokenClientSettings interface
type TokenClientSettings interface {
ToTokenOptions() TokenOptions
ToEndpointOptions() EndpointOpts
Validate() error
}
// APITokenClientSettings interface
type APITokenClientSettings interface {
ToAPITokenOptions() APITokenOptions
ToEndpointOptions() EndpointOpts
Validate() error
}
// AuthClientSettings interface
type AuthClientSettings interface {
ToAuthOptions() AuthOptions
ToEndpointOptions() EndpointOpts
Validate() error
}
// TokenAPISettings - settings for token client building
type TokenAPISettings struct {
APIURL string `json:"url,omitempty"`
AccessToken string `json:"access,omitempty"`
RefreshToken string `json:"refresh,omitempty"`
AllowReauth bool `json:"-"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Region int `json:"region,omitempty"`
Project int `json:"project,omitempty"`
Version string `json:"version,omitempty"`
Debug bool `json:"debug,omitempty"`
}
// ToTokenOptions implements TokenClientSettings interface
func (gs TokenAPISettings) ToTokenOptions() TokenOptions {
return TokenOptions{
APIURL: gs.APIURL,
AccessToken: gs.AccessToken,
RefreshToken: gs.RefreshToken,
AllowReauth: gs.AllowReauth,
}
}
// ToEndpointOptions implements TokenClientSettings interface
func (gs TokenAPISettings) ToEndpointOptions() EndpointOpts {
return EndpointOpts{
Region: gs.Region,
Project: gs.Project,
Version: gs.Version,
Name: gs.Name,
Type: gs.Type,
}
}
// Validate implements TokenClientSettings interface
func (gs TokenAPISettings) Validate() error {
if gs.APIURL == "" {
return fmt.Errorf("api-url endpoint required")
}
if gs.AccessToken == "" {
return fmt.Errorf("access token required")
}
if gs.RefreshToken == "" {
return fmt.Errorf("refresh token required")
}
if gs.APIURL == "" {
return fmt.Errorf("api url required. APIURL")
}
return nil
}
// APITokenAPISettings - settings for api token client building
type APITokenAPISettings struct {
APIURL string `json:"url,omitempty"`
APIToken string `json:"-"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Region int `json:"region,omitempty"`
Project int `json:"project,omitempty"`
Version string `json:"version,omitempty"`
Debug bool `json:"debug,omitempty"`
}
// ToEndpointOptions implements APITokenClientSettings interface
func (gs APITokenAPISettings) ToEndpointOptions() EndpointOpts {
return EndpointOpts{
Region: gs.Region,
Project: gs.Project,
Version: gs.Version,
Name: gs.Name,
Type: gs.Type,
}
}
// ToAPITokenOptions implements APITokenClientSettings interface
func (gs APITokenAPISettings) ToAPITokenOptions() APITokenOptions {
return APITokenOptions{
APIURL: gs.APIURL,
APIToken: gs.APIToken,
}
}
// Validate implements TokenClientSettings interface
func (gs APITokenAPISettings) Validate() error {
if gs.APIURL == "" {
return fmt.Errorf("api-url endpoint required")
}
if gs.APIToken == "" {
return fmt.Errorf("api token required")
}
return nil
}
// PasswordAPISettings - settings for password client building
type PasswordAPISettings struct {
APIURL string `json:"api-url,omitempty"`
AuthURL string `json:"auth-url,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
AllowReauth bool `json:"-"`
Type string `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Region int `json:"region,omitempty"`
Project int `json:"project,omitempty"`
Version string `json:"version,omitempty"`
Debug bool `json:"debug,omitempty"`
}
// ToAuthOptions implements AuthClientSettings interface
func (gs PasswordAPISettings) ToAuthOptions() AuthOptions {
return AuthOptions{
APIURL: gs.APIURL,
AuthURL: gs.AuthURL,
Username: gs.Username,
Password: gs.Password,
AllowReauth: gs.AllowReauth,
}
}
// ToEndpointOptions implements AuthClientSettings interface
func (gs PasswordAPISettings) ToEndpointOptions() EndpointOpts {
return EndpointOpts{
Region: gs.Region,
Project: gs.Project,
Version: gs.Version,
Name: gs.Name,
Type: gs.Type,
}
}
// Validate implements AuthClientSettings interface
func (gs PasswordAPISettings) Validate() error {
if gs.AuthURL == "" {
return fmt.Errorf("auth-url endpoint required")
}
if gs.APIURL == "" {
return fmt.Errorf("api-url endpoint required")
}
if gs.Username == "" {
return fmt.Errorf("username required")
}
if gs.Password == "" {
return fmt.Errorf("password required")
}
return nil
}