1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package spec
16
17 import (
18 "encoding/json"
19
20 "github.com/go-openapi/jsonpointer"
21 "github.com/go-openapi/swag"
22 )
23
24 const (
25 basic = "basic"
26 apiKey = "apiKey"
27 oauth2 = "oauth2"
28 implicit = "implicit"
29 password = "password"
30 application = "application"
31 accessCode = "accessCode"
32 )
33
34
35 func BasicAuth() *SecurityScheme {
36 return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{Type: basic}}
37 }
38
39
40 func APIKeyAuth(fieldName, valueSource string) *SecurityScheme {
41 return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{Type: apiKey, Name: fieldName, In: valueSource}}
42 }
43
44
45 func OAuth2Implicit(authorizationURL string) *SecurityScheme {
46 return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
47 Type: oauth2,
48 Flow: implicit,
49 AuthorizationURL: authorizationURL,
50 }}
51 }
52
53
54 func OAuth2Password(tokenURL string) *SecurityScheme {
55 return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
56 Type: oauth2,
57 Flow: password,
58 TokenURL: tokenURL,
59 }}
60 }
61
62
63 func OAuth2Application(tokenURL string) *SecurityScheme {
64 return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
65 Type: oauth2,
66 Flow: application,
67 TokenURL: tokenURL,
68 }}
69 }
70
71
72 func OAuth2AccessToken(authorizationURL, tokenURL string) *SecurityScheme {
73 return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
74 Type: oauth2,
75 Flow: accessCode,
76 AuthorizationURL: authorizationURL,
77 TokenURL: tokenURL,
78 }}
79 }
80
81
82 type SecuritySchemeProps struct {
83 Description string `json:"description,omitempty"`
84 Type string `json:"type"`
85 Name string `json:"name,omitempty"`
86 In string `json:"in,omitempty"`
87 Flow string `json:"flow,omitempty"`
88 AuthorizationURL string `json:"authorizationUrl"`
89 TokenURL string `json:"tokenUrl,omitempty"`
90 Scopes map[string]string `json:"scopes,omitempty"`
91 }
92
93
94 func (s *SecuritySchemeProps) AddScope(scope, description string) {
95 if s.Scopes == nil {
96 s.Scopes = make(map[string]string)
97 }
98 s.Scopes[scope] = description
99 }
100
101
102
103
104
105
106 type SecurityScheme struct {
107 VendorExtensible
108 SecuritySchemeProps
109 }
110
111
112 func (s SecurityScheme) JSONLookup(token string) (interface{}, error) {
113 if ex, ok := s.Extensions[token]; ok {
114 return &ex, nil
115 }
116
117 r, _, err := jsonpointer.GetForToken(s.SecuritySchemeProps, token)
118 return r, err
119 }
120
121
122 func (s SecurityScheme) MarshalJSON() ([]byte, error) {
123 var (
124 b1 []byte
125 err error
126 )
127
128 if s.Type == oauth2 && (s.Flow == "implicit" || s.Flow == "accessCode") {
129
130 b1, err = json.Marshal(s.SecuritySchemeProps)
131 } else {
132
133 b1, err = json.Marshal(struct {
134 Description string `json:"description,omitempty"`
135 Type string `json:"type"`
136 Name string `json:"name,omitempty"`
137 In string `json:"in,omitempty"`
138 Flow string `json:"flow,omitempty"`
139 AuthorizationURL string `json:"authorizationUrl,omitempty"`
140 TokenURL string `json:"tokenUrl,omitempty"`
141 Scopes map[string]string `json:"scopes,omitempty"`
142 }{
143 Description: s.Description,
144 Type: s.Type,
145 Name: s.Name,
146 In: s.In,
147 Flow: s.Flow,
148 AuthorizationURL: s.AuthorizationURL,
149 TokenURL: s.TokenURL,
150 Scopes: s.Scopes,
151 })
152 }
153 if err != nil {
154 return nil, err
155 }
156
157 b2, err := json.Marshal(s.VendorExtensible)
158 if err != nil {
159 return nil, err
160 }
161 return swag.ConcatJSON(b1, b2), nil
162 }
163
164
165 func (s *SecurityScheme) UnmarshalJSON(data []byte) error {
166 if err := json.Unmarshal(data, &s.SecuritySchemeProps); err != nil {
167 return err
168 }
169 return json.Unmarshal(data, &s.VendorExtensible)
170 }
171
View as plain text