-
Notifications
You must be signed in to change notification settings - Fork 3
/
security_scheme.go
155 lines (131 loc) · 4.16 KB
/
security_scheme.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
package oas
type WithSecuritySchemes struct {
SecuritySchemes map[string]*SecurityScheme `json:"securitySchemes,omitempty"`
}
func (o *WithSecuritySchemes) AddSecurityScheme(name string, ss *SecurityScheme) {
if ss == nil {
return
}
if o.SecuritySchemes == nil {
o.SecuritySchemes = make(map[string]*SecurityScheme)
}
o.SecuritySchemes[name] = ss
}
type WithSecurityRequirement struct {
Security []*SecurityRequirement `json:"security,omitempty"`
}
func (o *WithSecurityRequirement) AddSecurityRequirement(sr *SecurityRequirement) {
if sr == nil {
return
}
o.Security = append(o.Security, sr)
}
type SecurityRequirement map[string][]string
func NewAPIKeySecurityScheme(name string, in Position) *SecurityScheme {
return &SecurityScheme{
SecuritySchemeObject: SecuritySchemeObject{
Type: SecurityTypeAPIKey,
Name: name,
In: in,
},
}
}
func NewHTTPSecurityScheme(scheme string, bearerFormat string) *SecurityScheme {
if bearerFormat != "" {
scheme = "bearer"
}
return &SecurityScheme{
SecuritySchemeObject: SecuritySchemeObject{
Type: SecurityTypeHttp,
Scheme: scheme,
BearerFormat: bearerFormat,
},
}
}
func NewOAuth2SecurityScheme(oauthFlowsObject OAuthFlowsObject) *SecurityScheme {
return &SecurityScheme{
SecuritySchemeObject: SecuritySchemeObject{
Type: SecurityTypeOAuth2,
Flows: &OAuthFlows{
OAuthFlowsObject: oauthFlowsObject,
},
},
}
}
func NewOpenIdConnectSecurityScheme(openIdConnectUrl string) *SecurityScheme {
return &SecurityScheme{
SecuritySchemeObject: SecuritySchemeObject{
Type: SecurityTypeOpenIdConnect,
OpenIdConnectUrl: openIdConnectUrl,
},
}
}
type SecurityScheme struct {
SecuritySchemeObject
SpecExtensions
}
func (i SecurityScheme) MarshalJSON() ([]byte, error) {
return flattenMarshalJSON(i.SecuritySchemeObject, i.SpecExtensions)
}
func (i *SecurityScheme) UnmarshalJSON(data []byte) error {
return flattenUnmarshalJSON(data, &i.SecuritySchemeObject, &i.SpecExtensions)
}
type SecuritySchemeObject struct {
Type SecurityType `json:"type"`
Description string `json:"description,omitempty"`
Name string `json:"name,omitempty"`
In Position `json:"in,omitempty"`
Scheme string `json:"scheme,omitempty"`
BearerFormat string `json:"bearerFormat,omitempty"`
Flows *OAuthFlows `json:"flows,omitempty"`
OpenIdConnectUrl string `json:"openIdConnectUrl,omitempty"`
}
type SecurityType string
const (
SecurityTypeAPIKey SecurityType = "apiKey"
SecurityTypeHttp SecurityType = "http"
SecurityTypeOAuth2 SecurityType = "oauth2"
SecurityTypeOpenIdConnect SecurityType = "openIdConnect"
)
type OAuthFlows struct {
OAuthFlowsObject
SpecExtensions
}
func (i OAuthFlows) MarshalJSON() ([]byte, error) {
return flattenMarshalJSON(i.OAuthFlowsObject, i.SpecExtensions)
}
func (i *OAuthFlows) UnmarshalJSON(data []byte) error {
return flattenUnmarshalJSON(data, &i.OAuthFlowsObject, &i.SpecExtensions)
}
type OAuthFlowsObject struct {
Implicit *OAuthFlow `json:"implicit,omitempty"`
Password *OAuthFlow `json:"password,omitempty"`
ClientCredentials *OAuthFlow `json:"clientCredentials,omitempty"`
AuthorizationCode *OAuthFlow `json:"authorizationCode,omitempty"`
}
func NewOAuthFlow(authorizationURL string, tokenURL string, refreshURL string, scopes map[string]string) *OAuthFlow {
return &OAuthFlow{
OAuthFlowObject: OAuthFlowObject{
AuthorizationURL: authorizationURL,
TokenURL: tokenURL,
RefreshURL: refreshURL,
Scopes: scopes,
},
}
}
type OAuthFlow struct {
OAuthFlowObject
SpecExtensions
}
func (i OAuthFlow) MarshalJSON() ([]byte, error) {
return flattenMarshalJSON(i.OAuthFlowObject, i.SpecExtensions)
}
func (i *OAuthFlow) UnmarshalJSON(data []byte) error {
return flattenUnmarshalJSON(data, &i.OAuthFlowObject, &i.SpecExtensions)
}
type OAuthFlowObject struct {
AuthorizationURL string `json:"authorizationUrl"`
TokenURL string `json:"tokenUrl"`
RefreshURL string `json:"refreshUrl,omitempty"`
Scopes map[string]string `json:"scopes"`
}