/
specsecuritybuilder.go
381 lines (324 loc) · 8.15 KB
/
specsecuritybuilder.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
package apic
import (
"fmt"
"github.com/Axway/agent-sdk/pkg/util"
"github.com/getkin/kin-openapi/openapi2"
"github.com/getkin/kin-openapi/openapi3"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
const (
oas2 = 2
oas3 = 3
httpScheme = "http"
cookie = "cookie"
header = "header"
query = "query"
implicit = "implicit"
authorizationCode = "authorizationCode"
clientCredentials = "clientCredentials"
password = "password"
accessCode = "accessCode"
application = "application"
)
// used by oas spec parsers to start the builder
func newSpecSecurityBuilder(oasMajorVersion int) SecurityBuilder {
return &specSecurity{
oasMajorVersion: oasMajorVersion,
}
}
// first select the type of security we are building
type SecurityBuilder interface {
HTTPBasic() HTTPBasicSecurityBuilder
APIKey() APIKeySecurityBuilder
OAuth() OAuthSecurityBuilder
Bearer() BearerSecurityBuilder
OpenID() OpenIDSecurityBuilder
}
type specSecurity struct {
oasMajorVersion int
}
func (s *specSecurity) HTTPBasic() HTTPBasicSecurityBuilder {
return &httpBasicSecurity{
specSecurity: s,
}
}
type HTTPBasicSecurityBuilder interface {
Build() map[string]interface{}
}
type httpBasicSecurity struct {
*specSecurity
}
// create http basic scheme
func (s *httpBasicSecurity) Build() map[string]interface{} {
const (
name = "basicAuth"
basic = "basic"
)
if s.oasMajorVersion == 2 {
return map[string]interface{}{
name: &openapi2.SecurityScheme{
Type: basic,
},
}
}
return map[string]interface{}{
name: &openapi3.SecurityScheme{
Type: httpScheme,
Scheme: basic,
},
}
}
func (s *specSecurity) APIKey() APIKeySecurityBuilder {
return &apiKeySecurity{
specSecurity: s,
locations: []string{},
}
}
type APIKeySecurityBuilder interface {
Build() map[string]interface{}
InCookie() APIKeySecurityBuilder // quests are the same for cookie vs api key in query or header
InHeader() APIKeySecurityBuilder
InQueryParam() APIKeySecurityBuilder
SetArgumentName(argName string) APIKeySecurityBuilder
}
type apiKeySecurity struct {
*specSecurity
locations []string
argName string
}
func (s *apiKeySecurity) InCookie() APIKeySecurityBuilder {
s.locations = append(s.locations, cookie)
return s
}
func (s *apiKeySecurity) InHeader() APIKeySecurityBuilder {
s.locations = append(s.locations, header)
return s
}
func (s *apiKeySecurity) InQueryParam() APIKeySecurityBuilder {
s.locations = append(s.locations, query)
return s
}
func (s *apiKeySecurity) SetArgumentName(argName string) APIKeySecurityBuilder {
s.argName = argName
return s
}
// create api key security type
func (s *apiKeySecurity) Build() map[string]interface{} {
const apiKey = "apiKey"
output := map[string]interface{}{}
for _, location := range s.locations {
name := fmt.Sprintf("%v%v", apiKey, cases.Title(language.English).String(location))
if s.oasMajorVersion == 2 {
if location == cookie {
// only supported on oas3, return empty for oas2
continue
}
output[name] = &openapi2.SecurityScheme{
Name: s.argName,
In: location,
Type: apiKey,
}
continue
}
output[name] = &openapi3.SecurityScheme{
Name: s.argName,
In: location,
Type: apiKey,
}
}
return output
}
type oAuthFlow struct {
flow string
authURL string
tokenURL string
refreshURL string
scopes map[string]string
}
func NewOAuthFlowBuilder() OAuthFlowBuilder {
return &oAuthFlow{}
}
// oauth flow options, setting flow type should be last, not all other methods are required
type OAuthFlowBuilder interface {
SetScopes(map[string]string) OAuthFlowBuilder
AddScope(scope, description string) OAuthFlowBuilder
SetAuthorizationURL(url string) OAuthFlowBuilder
SetRefreshURL(url string) OAuthFlowBuilder
SetTokenURL(url string) OAuthFlowBuilder
Implicit() *oAuthFlow
Password() *oAuthFlow
AuthorizationCode() *oAuthFlow
ClientCredentials() *oAuthFlow
}
func (s *oAuthFlow) SetScopes(scopes map[string]string) OAuthFlowBuilder {
s.scopes = scopes
return s
}
func (s *oAuthFlow) AddScope(scope, description string) OAuthFlowBuilder {
s.scopes[scope] = description
return s
}
func (s *oAuthFlow) SetTokenURL(url string) OAuthFlowBuilder {
s.tokenURL = url
return s
}
func (s *oAuthFlow) SetAuthorizationURL(url string) OAuthFlowBuilder {
s.authURL = url
return s
}
func (s *oAuthFlow) SetRefreshURL(url string) OAuthFlowBuilder {
s.refreshURL = url
return s
}
func (s *oAuthFlow) Implicit() *oAuthFlow {
s.flow = implicit
return s
}
func (s *oAuthFlow) Password() *oAuthFlow {
s.flow = password
return s
}
func (s *oAuthFlow) AuthorizationCode() *oAuthFlow {
s.flow = authorizationCode
return s
}
func (s *oAuthFlow) ClientCredentials() *oAuthFlow {
s.flow = clientCredentials
return s
}
type OAuthSecurityBuilder interface {
AddFlow(flow *oAuthFlow) OAuthSecurityBuilder
Build() map[string]interface{}
}
type oAuthSecurity struct {
*specSecurity
flows []*oAuthFlow
}
func (s *specSecurity) OAuth() OAuthSecurityBuilder {
return &oAuthSecurity{
specSecurity: s,
flows: []*oAuthFlow{},
}
}
func (s *oAuthSecurity) AddFlow(flow *oAuthFlow) OAuthSecurityBuilder {
s.flows = append(s.flows, flow)
return s
}
func (s *oAuthSecurity) Build() map[string]interface{} {
const oauth2 = "oauth2"
if s.oasMajorVersion == 2 {
// create separate scheme for each flow type
oauthFlows := map[string]interface{}{}
for _, f := range s.flows {
// adjust the name of the flow for oas2 support
if f.flow == authorizationCode {
f.flow = accessCode
} else if f.flow == clientCredentials {
f.flow = application
}
fName := fmt.Sprintf("%v%v", oauth2, cases.Title(language.English).String(f.flow))
oauthFlows[fName] = &openapi2.SecurityScheme{
Type: oauth2,
Flow: f.flow,
Scopes: util.OrderStringsInMap(f.scopes),
AuthorizationURL: f.authURL,
TokenURL: f.tokenURL,
}
}
return util.OrderStringsInMap(oauthFlows)
}
// create single scheme with all flows
oauthFlows := &openapi3.OAuthFlows{}
for _, f := range s.flows {
oFlow := &openapi3.OAuthFlow{
AuthorizationURL: f.authURL,
RefreshURL: f.refreshURL,
TokenURL: f.tokenURL,
Scopes: util.OrderStringsInMap(f.scopes),
}
switch f.flow {
case authorizationCode:
oauthFlows.AuthorizationCode = oFlow
case password:
oauthFlows.Password = oFlow
case clientCredentials:
oauthFlows.ClientCredentials = oFlow
case implicit:
oauthFlows.Implicit = oFlow
}
}
return map[string]interface{}{
oauth2: &openapi3.SecurityScheme{
Type: oauth2,
Flows: oauthFlows,
},
}
}
func (s *specSecurity) Bearer() BearerSecurityBuilder {
return &bearerSecurity{
specSecurity: s,
}
}
type BearerSecurityBuilder interface {
Build() map[string]interface{}
SetFormat(format string) BearerSecurityBuilder
}
type bearerSecurity struct {
*specSecurity
format string
}
func (s *bearerSecurity) SetFormat(format string) BearerSecurityBuilder {
s.format = format
return s
}
func (s *bearerSecurity) Build() map[string]interface{} {
const (
name = "bearerAuth"
bearer = "bearer"
)
if s.oasMajorVersion == 2 {
// only supported on oas3, return empty for oas2
return map[string]interface{}{}
}
return map[string]interface{}{
name: &openapi3.SecurityScheme{
Type: httpScheme,
Scheme: bearer,
BearerFormat: s.format,
},
}
}
func (s *specSecurity) OpenID() OpenIDSecurityBuilder {
return &openIDSecurity{
specSecurity: s,
}
}
type OpenIDSecurityBuilder interface {
Build() map[string]interface{}
SetURL(url string) OpenIDSecurityBuilder
}
type openIDSecurity struct {
*specSecurity
url string
}
func (s *openIDSecurity) SetURL(url string) OpenIDSecurityBuilder {
s.url = url
return s
}
func (s *openIDSecurity) Build() map[string]interface{} {
const (
name = "openId"
openIdConnect = "openIdConnect"
)
if s.oasMajorVersion == 2 {
// only supported on oas3, return empty for oas2
return map[string]interface{}{}
}
return map[string]interface{}{
name: &openapi3.SecurityScheme{
Type: openIdConnect,
OpenIdConnectUrl: s.url,
},
}
}