-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsecurity.go
More file actions
298 lines (257 loc) · 11.5 KB
/
Copy pathsecurity.go
File metadata and controls
298 lines (257 loc) · 11.5 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
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
package swagger
import (
"context"
"errors"
"fmt"
"net/url"
"strings"
"github.com/speakeasy-api/openapi/extensions"
"github.com/speakeasy-api/openapi/internal/interfaces"
"github.com/speakeasy-api/openapi/marshaller"
"github.com/speakeasy-api/openapi/sequencedmap"
"github.com/speakeasy-api/openapi/swagger/core"
"github.com/speakeasy-api/openapi/validation"
)
// SecuritySchemeType represents the type of security scheme.
type SecuritySchemeType string
const (
// SecuritySchemeTypeBasic represents basic authentication.
SecuritySchemeTypeBasic SecuritySchemeType = "basic"
// SecuritySchemeTypeAPIKey represents API key authentication.
SecuritySchemeTypeAPIKey SecuritySchemeType = "apiKey"
// SecuritySchemeTypeOAuth2 represents OAuth2 authentication.
SecuritySchemeTypeOAuth2 SecuritySchemeType = "oauth2"
)
// SecuritySchemeIn represents the location of the API key.
type SecuritySchemeIn string
const (
// SecuritySchemeInQuery represents an API key in the query string.
SecuritySchemeInQuery SecuritySchemeIn = "query"
// SecuritySchemeInHeader represents an API key in the header.
SecuritySchemeInHeader SecuritySchemeIn = "header"
)
// OAuth2Flow represents the flow type for OAuth2.
type OAuth2Flow string
const (
// OAuth2FlowImplicit represents the implicit flow.
OAuth2FlowImplicit OAuth2Flow = "implicit"
// OAuth2FlowPassword represents the password flow.
OAuth2FlowPassword OAuth2Flow = "password"
// OAuth2FlowApplication represents the application flow.
OAuth2FlowApplication OAuth2Flow = "application"
// OAuth2FlowAccessCode represents the access code flow.
OAuth2FlowAccessCode OAuth2Flow = "accessCode"
)
// SecurityScheme defines a security scheme that can be used by the operations.
type SecurityScheme struct {
marshaller.Model[core.SecurityScheme]
// Type is the type of the security scheme. Valid values are "basic", "apiKey" or "oauth2".
Type SecuritySchemeType
// Description is a short description for security scheme.
Description *string
// Name is the name of the header or query parameter to be used (apiKey only).
Name *string
// In is the location of the API key. Valid values are "query" or "header" (apiKey only).
In *SecuritySchemeIn
// Flow is the flow used by the OAuth2 security scheme. Valid values are "implicit", "password", "application" or "accessCode" (oauth2 only).
Flow *OAuth2Flow
// AuthorizationURL is the authorization URL to be used for this flow (oauth2 "implicit" and "accessCode" only).
AuthorizationURL *string
// TokenURL is the token URL to be used for this flow (oauth2 "password", "application" and "accessCode" only).
TokenURL *string
// Scopes lists the available scopes for the OAuth2 security scheme (oauth2 only).
Scopes *sequencedmap.Map[string, string]
// Extensions provides a list of extensions to the SecurityScheme object.
Extensions *extensions.Extensions
}
var _ interfaces.Model[core.SecurityScheme] = (*SecurityScheme)(nil)
// GetType returns the value of the Type field.
func (s *SecurityScheme) GetType() SecuritySchemeType {
if s == nil {
return ""
}
return s.Type
}
// GetDescription returns the value of the Description field. Returns empty string if not set.
func (s *SecurityScheme) GetDescription() string {
if s == nil || s.Description == nil {
return ""
}
return *s.Description
}
// GetName returns the value of the Name field. Returns empty string if not set.
func (s *SecurityScheme) GetName() string {
if s == nil || s.Name == nil {
return ""
}
return *s.Name
}
// GetIn returns the value of the In field. Returns empty string if not set.
func (s *SecurityScheme) GetIn() SecuritySchemeIn {
if s == nil || s.In == nil {
return ""
}
return *s.In
}
// GetFlow returns the value of the Flow field. Returns empty string if not set.
func (s *SecurityScheme) GetFlow() OAuth2Flow {
if s == nil || s.Flow == nil {
return ""
}
return *s.Flow
}
// GetAuthorizationURL returns the value of the AuthorizationURL field. Returns empty string if not set.
func (s *SecurityScheme) GetAuthorizationURL() string {
if s == nil || s.AuthorizationURL == nil {
return ""
}
return *s.AuthorizationURL
}
// GetTokenURL returns the value of the TokenURL field. Returns empty string if not set.
func (s *SecurityScheme) GetTokenURL() string {
if s == nil || s.TokenURL == nil {
return ""
}
return *s.TokenURL
}
// GetScopes returns the value of the Scopes field. Returns nil if not set.
func (s *SecurityScheme) GetScopes() *sequencedmap.Map[string, string] {
if s == nil {
return nil
}
return s.Scopes
}
// GetExtensions returns the value of the Extensions field. Returns an empty extensions map if not set.
func (s *SecurityScheme) GetExtensions() *extensions.Extensions {
if s == nil || s.Extensions == nil {
return extensions.New()
}
return s.Extensions
}
// Validate validates the SecurityScheme object against the Swagger Specification.
func (s *SecurityScheme) Validate(ctx context.Context, opts ...validation.Option) []error {
c := s.GetCore()
errs := []error{}
if c.Type.Present && s.Type == "" {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, errors.New("`securityScheme.type` is required"), c, c.Type))
} else {
validTypes := []SecuritySchemeType{SecuritySchemeTypeBasic, SecuritySchemeTypeAPIKey, SecuritySchemeTypeOAuth2}
valid := false
for _, t := range validTypes {
if s.Type == t {
valid = true
break
}
}
if !valid {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationAllowedValues, fmt.Errorf("securityScheme.type must be one of [`%s`]", strings.Join([]string{string(SecuritySchemeTypeBasic), string(SecuritySchemeTypeAPIKey), string(SecuritySchemeTypeOAuth2)}, ", ")), c, c.Type))
}
}
// Validate apiKey specific fields
if s.Type == SecuritySchemeTypeAPIKey {
if !c.Name.Present || s.Name == nil || *s.Name == "" {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, errors.New("`securityScheme.name` is required for type=apiKey"), c, c.Name))
}
if !c.In.Present || s.In == nil {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, errors.New("`securityScheme.in` is required for type=apiKey"), c, c.In))
} else if *s.In != SecuritySchemeInQuery && *s.In != SecuritySchemeInHeader {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationAllowedValues, fmt.Errorf("securityScheme.in must be one of [`%s`] for type=apiKey", strings.Join([]string{string(SecuritySchemeInQuery), string(SecuritySchemeInHeader)}, ", ")), c, c.In))
}
}
// Validate oauth2 specific fields
if s.Type == SecuritySchemeTypeOAuth2 {
if !c.Flow.Present || s.Flow == nil {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, errors.New("`securityScheme.flow` is required for type=oauth2"), c, c.Flow))
} else {
validFlows := []OAuth2Flow{OAuth2FlowImplicit, OAuth2FlowPassword, OAuth2FlowApplication, OAuth2FlowAccessCode}
valid := false
for _, f := range validFlows {
if *s.Flow == f {
valid = true
break
}
}
if !valid {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationAllowedValues, fmt.Errorf("securityScheme.flow must be one of [`%s`] for type=oauth2", strings.Join([]string{string(OAuth2FlowImplicit), string(OAuth2FlowPassword), string(OAuth2FlowApplication), string(OAuth2FlowAccessCode)}, ", ")), c, c.Flow))
}
if s.Flow != nil {
// authorizationUrl required for implicit and accessCode flows
if (*s.Flow == OAuth2FlowImplicit || *s.Flow == OAuth2FlowAccessCode) && (!c.AuthorizationURL.Present || s.AuthorizationURL == nil || *s.AuthorizationURL == "") {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, fmt.Errorf("securityScheme.authorizationUrl is required for flow=%s", *s.Flow), c, c.AuthorizationURL))
}
// tokenUrl required for password, application and accessCode flows
if (*s.Flow == OAuth2FlowPassword || *s.Flow == OAuth2FlowApplication || *s.Flow == OAuth2FlowAccessCode) && (!c.TokenURL.Present || s.TokenURL == nil || *s.TokenURL == "") {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, fmt.Errorf("securityScheme.tokenUrl is required for flow=%s", *s.Flow), c, c.TokenURL))
}
}
}
if !c.Scopes.Present {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationRequiredField, errors.New("`securityScheme.scopes` is required for type=oauth2"), c, c.Scopes))
}
}
// Validate URLs
if c.AuthorizationURL.Present && s.AuthorizationURL != nil && *s.AuthorizationURL != "" {
if _, err := url.Parse(*s.AuthorizationURL); err != nil {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationInvalidFormat, fmt.Errorf("securityScheme.authorizationUrl is not a valid uri: %w", err), c, c.AuthorizationURL))
}
}
if c.TokenURL.Present && s.TokenURL != nil && *s.TokenURL != "" {
if _, err := url.Parse(*s.TokenURL); err != nil {
errs = append(errs, validation.NewValueError(validation.SeverityError, validation.RuleValidationInvalidFormat, fmt.Errorf("securityScheme.tokenUrl is not a valid uri: %w", err), c, c.TokenURL))
}
}
s.Valid = len(errs) == 0 && c.GetValid()
return errs
}
// SecurityRequirement lists the required security schemes to execute an operation.
// The keys are the names of security schemes and the values are lists of scope names.
// For non-oauth2 security schemes, the array MUST be empty.
type SecurityRequirement struct {
marshaller.Model[core.SecurityRequirement]
*sequencedmap.Map[string, []string]
}
var _ interfaces.Model[core.SecurityRequirement] = (*SecurityRequirement)(nil)
// NewSecurityRequirement creates a new SecurityRequirement with an initialized map.
func NewSecurityRequirement() *SecurityRequirement {
return &SecurityRequirement{
Map: sequencedmap.New[string, []string](),
}
}
// Validate validates the SecurityRequirement object against the Swagger Specification.
func (s *SecurityRequirement) Validate(ctx context.Context, opts ...validation.Option) []error {
c := s.GetCore()
errs := []error{}
// Get Swagger context to access security definitions
validationOpts := validation.NewOptions(opts...)
swagger := validation.GetContextObject[Swagger](validationOpts)
if swagger == nil || s.Map == nil {
s.Valid = c.GetValid()
return errs
}
// Validate each security requirement
for name := range s.Keys() {
scopes, _ := s.Get(name)
// Check that the security scheme name exists in securityDefinitions
secScheme, exists := swagger.SecurityDefinitions.Get(name)
if !exists {
errs = append(errs, validation.NewValidationError(
validation.SeverityError,
validation.RuleValidationSchemeNotFound,
fmt.Errorf("security requirement `%s` does not match any security scheme in securityDefinitions", name),
c.RootNode))
continue
}
// For non-oauth2 security schemes, the array MUST be empty
if secScheme.Type != SecuritySchemeTypeOAuth2 {
if len(scopes) > 0 {
errs = append(errs, validation.NewValidationError(
validation.SeverityError,
validation.RuleValidationAllowedValues,
fmt.Errorf("security requirement `%s` must have empty scopes array for non-oauth2 security scheme (type=`%s`)", name, secScheme.Type),
c.RootNode))
}
}
}
s.Valid = len(errs) == 0 && c.GetValid()
return errs
}