-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecurity.go
More file actions
96 lines (86 loc) · 2.74 KB
/
Copy pathsecurity.go
File metadata and controls
96 lines (86 loc) · 2.74 KB
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
package openapi
import "github.com/getkin/kin-openapi/openapi3"
// SecuritySchemeConfig describes a serializable OpenAPI security scheme.
type SecuritySchemeConfig struct {
Type string
Description string
Name string
In string
Scheme string
BearerFormat string
OpenIDConnectURL string
Flows *OAuthFlowsConfig
}
// OAuthFlowsConfig describes serializable OAuth2 flows.
type OAuthFlowsConfig struct {
Implicit *OAuthFlowConfig
Password *OAuthFlowConfig
ClientCredentials *OAuthFlowConfig
AuthorizationCode *OAuthFlowConfig
}
// OAuthFlowConfig describes a serializable OAuth2 flow.
type OAuthFlowConfig struct {
AuthorizationURL string
TokenURL string
RefreshURL string
Scopes map[string]string
}
// SecurityScheme registers an OpenAPI security scheme.
func SecurityScheme(name string, scheme *openapi3.SecurityScheme) Option {
return func(g *Generator) {
if g.spec.Components.SecuritySchemes == nil {
g.spec.Components.SecuritySchemes = openapi3.SecuritySchemes{}
}
g.spec.Components.SecuritySchemes[name] = &openapi3.SecuritySchemeRef{Value: scheme}
}
}
// SecuritySchemeFromConfig registers a serializable OpenAPI security scheme.
func SecuritySchemeFromConfig(name string, scheme SecuritySchemeConfig) Option {
return SecurityScheme(name, securitySchemeFromConfig(scheme))
}
// HTTPBearerSecurity creates an HTTP bearer security scheme.
func HTTPBearerSecurity(description string) *openapi3.SecurityScheme {
return &openapi3.SecurityScheme{
Type: "http",
Scheme: "bearer",
BearerFormat: "JWT",
Description: description,
}
}
func securitySchemeFromConfig(s SecuritySchemeConfig) *openapi3.SecurityScheme {
scheme := &openapi3.SecurityScheme{
Type: s.Type,
Description: s.Description,
Name: s.Name,
In: s.In,
Scheme: s.Scheme,
BearerFormat: s.BearerFormat,
OpenIdConnectUrl: s.OpenIDConnectURL,
}
if s.Flows != nil {
scheme.Flows = oauthFlowsFromConfig(s.Flows)
}
return scheme
}
func oauthFlowsFromConfig(flows *OAuthFlowsConfig) *openapi3.OAuthFlows {
if flows == nil {
return nil
}
return &openapi3.OAuthFlows{
Implicit: oauthFlowFromConfig(flows.Implicit),
Password: oauthFlowFromConfig(flows.Password),
ClientCredentials: oauthFlowFromConfig(flows.ClientCredentials),
AuthorizationCode: oauthFlowFromConfig(flows.AuthorizationCode),
}
}
func oauthFlowFromConfig(flow *OAuthFlowConfig) *openapi3.OAuthFlow {
if flow == nil {
return nil
}
return &openapi3.OAuthFlow{
AuthorizationURL: flow.AuthorizationURL,
TokenURL: flow.TokenURL,
RefreshURL: flow.RefreshURL,
Scopes: flow.Scopes,
}
}