forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
security.go
381 lines (342 loc) · 10.9 KB
/
security.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 apidsl
import (
"github.com/goadesign/goa/design"
"github.com/goadesign/goa/dslengine"
)
// Security defines an authentication requirements to access a goa Action. When defined on a
// Resource, it applies to all Actions, unless overriden by individual actions. When defined at the
// API level, it will apply to all resources by default, following the same logic.
//
// The scheme refers to previous definitions of either OAuth2Security, BasicAuthSecurity,
// APIKeySecurity or JWTSecurity. It can be a string, corresponding to the first parameter of
// those definitions, or a SecuritySchemeDefinition, returned by those same functions. Examples:
//
// Security(BasicAuth)
//
// Security("oauth2", func() {
// Scope("api:read") // Requires "api:read" oauth2 scope
// })
//
func Security(scheme interface{}, dsl ...func()) {
var def *design.SecurityDefinition
switch val := scheme.(type) {
case string:
def = &design.SecurityDefinition{}
for _, scheme := range design.Design.SecuritySchemes {
if scheme.SchemeName == val {
def.Scheme = scheme
}
}
if def.Scheme == nil {
dslengine.ReportError("security scheme %q not found", val)
return
}
case *design.SecuritySchemeDefinition:
def = &design.SecurityDefinition{Scheme: val}
default:
dslengine.ReportError("invalid value for 'scheme' parameter, specify a string or a *SecuritySchemeDefinition")
return
}
if len(dsl) != 0 {
if !dslengine.Execute(dsl[0], def) {
return
}
}
parentDef := dslengine.CurrentDefinition()
switch parent := parentDef.(type) {
case *design.ActionDefinition:
parent.Security = def
case *design.FileServerDefinition:
parent.Security = def
case *design.ResourceDefinition:
parent.Security = def
case *design.APIDefinition:
parent.Security = def
default:
dslengine.IncompatibleDSL()
return
}
}
// NoSecurity resets the authentication schemes for an Action or a Resource. It also prevents
// fallback to Resource or API-defined Security.
func NoSecurity() {
def := &design.SecurityDefinition{
Scheme: &design.SecuritySchemeDefinition{Kind: design.NoSecurityKind},
}
parentDef := dslengine.CurrentDefinition()
switch parent := parentDef.(type) {
case *design.ActionDefinition:
parent.Security = def
case *design.FileServerDefinition:
parent.Security = def
case *design.ResourceDefinition:
parent.Security = def
default:
dslengine.IncompatibleDSL()
return
}
}
// BasicAuthSecurity defines a "basic" security scheme for the API.
//
// Example:
//
// BasicAuthSecurity("password", func() {
// Description("Use your own password!")
// })
//
func BasicAuthSecurity(name string, dsl ...func()) *design.SecuritySchemeDefinition {
switch dslengine.CurrentDefinition().(type) {
case *design.APIDefinition, *dslengine.TopLevelDefinition:
default:
dslengine.IncompatibleDSL()
return nil
}
if securitySchemeRedefined(name) {
return nil
}
def := &design.SecuritySchemeDefinition{
Kind: design.BasicAuthSecurityKind,
SchemeName: name,
Type: "basic",
}
if len(dsl) != 0 {
def.DSLFunc = dsl[0]
}
design.Design.SecuritySchemes = append(design.Design.SecuritySchemes, def)
return def
}
func securitySchemeRedefined(name string) bool {
for _, previousScheme := range design.Design.SecuritySchemes {
if previousScheme.SchemeName == name {
dslengine.ReportError("cannot redefine SecurityScheme with name %q", name)
return true
}
}
return false
}
// APIKeySecurity defines an "apiKey" security scheme available throughout the API.
//
// Example:
//
// APIKeySecurity("key", func() {
// Description("Shared secret")
// Header("Authorization")
// })
//
func APIKeySecurity(name string, dsl ...func()) *design.SecuritySchemeDefinition {
switch dslengine.CurrentDefinition().(type) {
case *design.APIDefinition, *dslengine.TopLevelDefinition:
default:
dslengine.IncompatibleDSL()
return nil
}
if securitySchemeRedefined(name) {
return nil
}
def := &design.SecuritySchemeDefinition{
Kind: design.APIKeySecurityKind,
SchemeName: name,
Type: "apiKey",
}
if len(dsl) != 0 {
def.DSLFunc = dsl[0]
}
design.Design.SecuritySchemes = append(design.Design.SecuritySchemes, def)
return def
}
// OAuth2Security defines an OAuth2 security scheme. The child DSL must define one and exactly one
// flow. One of AccessCodeFlow, ImplicitFlow, PasswordFlow or ApplicationFlow. Each flow defines
// endpoints for retrieving OAuth2 authorization codes and/or refresh and access tokens. The
// endpoint URLs may be complete or may be just a path in which case the API scheme and host are
// used to build the full URL. See for example [Aaron Parecki's
// writeup](https://aaronparecki.com/2012/07/29/2/oauth2-simplified) for additional details on
// OAuth2 flows.
//
// The OAuth2 DSL also allows for defining scopes that must be associated with the incoming request
// token for successful authorization.
//
// Example:
//
// OAuth2Security("googAuth", func() {
// AccessCodeFlow("/authorization", "/token")
// // ImplicitFlow("/authorization")
// // PasswordFlow("/token"...)
// // ApplicationFlow("/token")
//
// Scope("my_system:write", "Write to the system")
// Scope("my_system:read", "Read anything in there")
// })
//
func OAuth2Security(name string, dsl ...func()) *design.SecuritySchemeDefinition {
switch dslengine.CurrentDefinition().(type) {
case *design.APIDefinition, *dslengine.TopLevelDefinition:
default:
dslengine.IncompatibleDSL()
return nil
}
if securitySchemeRedefined(name) {
return nil
}
def := &design.SecuritySchemeDefinition{
SchemeName: name,
Kind: design.OAuth2SecurityKind,
Type: "oauth2",
}
if len(dsl) != 0 {
def.DSLFunc = dsl[0]
}
design.Design.SecuritySchemes = append(design.Design.SecuritySchemes, def)
return def
}
// JWTSecurity defines an APIKey security scheme, with support for Scopes and a TokenURL.
//
// Since Scopes and TokenURLs are not compatible with the Swagger specification, the swagger
// generator inserts comments in the description of the different elements on which they are
// defined.
//
// Example:
//
// JWTSecurity("jwt", func() {
// Header("Authorization")
// TokenURL("https://example.com/token")
// Scope("my_system:write", "Write to the system")
// Scope("my_system:read", "Read anything in there")
// })
//
func JWTSecurity(name string, dsl ...func()) *design.SecuritySchemeDefinition {
switch dslengine.CurrentDefinition().(type) {
case *design.APIDefinition, *dslengine.TopLevelDefinition:
default:
dslengine.IncompatibleDSL()
return nil
}
if securitySchemeRedefined(name) {
return nil
}
def := &design.SecuritySchemeDefinition{
SchemeName: name,
Kind: design.JWTSecurityKind,
Type: "apiKey",
}
if len(dsl) != 0 {
def.DSLFunc = dsl[0]
}
design.Design.SecuritySchemes = append(design.Design.SecuritySchemes, def)
return def
}
// Scope defines an authorization scope. Used within SecurityScheme, a description may be provided
// explaining what the scope means. Within a Security block, only a scope is needed.
func Scope(name string, desc ...string) {
switch current := dslengine.CurrentDefinition().(type) {
case *design.SecurityDefinition:
if len(desc) >= 1 {
dslengine.ReportError("too many arguments")
return
}
current.Scopes = append(current.Scopes, name)
case *design.SecuritySchemeDefinition:
if len(desc) > 1 {
dslengine.ReportError("too many arguments")
return
}
if current.Scopes == nil {
current.Scopes = make(map[string]string)
}
d := "no description"
if len(desc) == 1 {
d = desc[0]
}
current.Scopes[name] = d
default:
dslengine.IncompatibleDSL()
}
}
// inHeader is called by `Header()`, see documentation there.
func inHeader(headerName string) {
if current, ok := dslengine.CurrentDefinition().(*design.SecuritySchemeDefinition); ok {
if current.Kind == design.APIKeySecurityKind || current.Kind == design.JWTSecurityKind {
if current.In != "" {
dslengine.ReportError("'In' previously defined through Header or Query")
return
}
current.In = "header"
current.Name = headerName
return
}
}
dslengine.IncompatibleDSL()
}
// Query defines that an APIKeySecurity or JWTSecurity implementation must check in the query
// parameter named "parameterName" to get the api key.
func Query(parameterName string) {
if current, ok := dslengine.CurrentDefinition().(*design.SecuritySchemeDefinition); ok {
if current.Kind == design.APIKeySecurityKind || current.Kind == design.JWTSecurityKind {
if current.In != "" {
dslengine.ReportError("'In' previously defined through Header or Query")
return
}
current.In = "query"
current.Name = parameterName
return
}
}
dslengine.IncompatibleDSL()
}
// AccessCodeFlow defines an "access code" OAuth2 flow. Use within an OAuth2Security definition.
func AccessCodeFlow(authorizationURL, tokenURL string) {
if current, ok := dslengine.CurrentDefinition().(*design.SecuritySchemeDefinition); ok {
if current.Kind == design.OAuth2SecurityKind {
current.Flow = "accessCode"
current.AuthorizationURL = authorizationURL
current.TokenURL = tokenURL
return
}
}
dslengine.IncompatibleDSL()
}
// ApplicationFlow defines an "application" OAuth2 flow. Use within an OAuth2Security definition.
func ApplicationFlow(tokenURL string) {
if parent, ok := dslengine.CurrentDefinition().(*design.SecuritySchemeDefinition); ok {
if parent.Kind == design.OAuth2SecurityKind {
parent.Flow = "application"
parent.TokenURL = tokenURL
return
}
}
dslengine.IncompatibleDSL()
}
// PasswordFlow defines a "password" OAuth2 flow. Use within an OAuth2Security definition.
func PasswordFlow(tokenURL string) {
if parent, ok := dslengine.CurrentDefinition().(*design.SecuritySchemeDefinition); ok {
if parent.Kind == design.OAuth2SecurityKind {
parent.Flow = "password"
parent.TokenURL = tokenURL
return
}
}
dslengine.IncompatibleDSL()
}
// ImplicitFlow defines an "implicit" OAuth2 flow. Use within an OAuth2Security definition.
func ImplicitFlow(authorizationURL string) {
if parent, ok := dslengine.CurrentDefinition().(*design.SecuritySchemeDefinition); ok {
if parent.Kind == design.OAuth2SecurityKind {
parent.Flow = "implicit"
parent.AuthorizationURL = authorizationURL
return
}
}
dslengine.IncompatibleDSL()
}
// TokenURL defines a URL to get an access token. If you are defining OAuth2 flows, use
// `ImplicitFlow`, `PasswordFlow`, `AccessCodeFlow` or `ApplicationFlow` instead. This will set an
// endpoint where you can obtain a JWT with the JWTSecurity scheme. The URL may be a complete URL
// or just a path in which case the API scheme and host are used to build the full URL.
func TokenURL(tokenURL string) {
if parent, ok := dslengine.CurrentDefinition().(*design.SecuritySchemeDefinition); ok {
if parent.Kind == design.JWTSecurityKind {
parent.TokenURL = tokenURL
return
}
}
dslengine.IncompatibleDSL()
}