-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsecurity_scheme.go
79 lines (68 loc) · 2.32 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
package openapi
// codebeat:disable[TOO_MANY_IVARS]
// SecurityScheme Object
type SecurityScheme struct {
Type SecuritySchemeType
Description string
Name string
In InType
Scheme string
BearerFormat string `yaml:"bearerFormat"`
Flows *OAuthFlows
OpenIDConnectURL string `yaml:"openIdConnectUrl"`
Ref string `yaml:"$ref"`
}
// SecuritySchemeType represents a securityScheme.type value.
type SecuritySchemeType string
// SecuritySchemeTypes
const (
APIKeyType SecuritySchemeType = "apiKey"
HTTPType SecuritySchemeType = "http"
OAuth2Type SecuritySchemeType = "oauth2"
OpenIDConnectType SecuritySchemeType = "openIdConnect"
)
// SecuritySchemeTypeList is a list of valid values of securityScheme.Type.
var SecuritySchemeTypeList = []string{string(APIKeyType), string(HTTPType), string(OAuth2Type), string(OpenIDConnectType)}
// Validate the values of SecurityScheme object.
func (secScheme SecurityScheme) Validate() error {
switch secScheme.Type {
case "":
return ErrRequired{Target: "securityScheme.type"}
case APIKeyType:
return secScheme.validateFieldForAPIKey()
case HTTPType:
return secScheme.validateFieldForHTTP()
case OAuth2Type:
return secScheme.validateFieldForOAuth2()
case OpenIDConnectType:
return secScheme.validateFieldForOpenIDConnect()
}
return ErrMustOneOf{Object: "securityScheme.type", ValidValues: SecuritySchemeTypeList}
}
func (secScheme SecurityScheme) validateFieldForAPIKey() error {
if secScheme.Name == "" {
return ErrRequired{"securityScheme.name"}
}
if secScheme.In == "" {
return ErrRequired{"securityScheme.in"}
}
if secScheme.In != InQuery && secScheme.In != InHeader && secScheme.In != InCookie {
return ErrMustOneOf{Object: "securityScheme.in", ValidValues: SecuritySchemeInList}
}
return nil
}
func (secScheme SecurityScheme) validateFieldForHTTP() error {
if secScheme.Scheme == "" {
return ErrRequired{Target: "securityScheme.scheme"}
}
return nil
}
func (secScheme SecurityScheme) validateFieldForOAuth2() error {
if secScheme.Flows == nil {
return ErrRequired{Target: "securityScheme.flows"}
}
return secScheme.Flows.Validate()
}
func (secScheme SecurityScheme) validateFieldForOpenIDConnect() error {
return mustURL("securityScheme.openIdConnectUrl", secScheme.OpenIDConnectURL)
}