-
Notifications
You must be signed in to change notification settings - Fork 2
/
oauth_flow.go
324 lines (290 loc) · 8.24 KB
/
oauth_flow.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
package openapi
import (
"encoding/json"
"github.com/chanced/transcode"
"gopkg.in/yaml.v3"
)
// OAuthFlow configuration details for a supported OAuth Flow
type OAuthFlow struct {
Extensions `json:"-"`
Location `json:"-"`
// The authorization URL to be used for this flow. This MUST be in the form
// of a URL. The OAuth2 standard requires the use of TLS.
//
// Applies to: OAuth2 ("implicit", "authorizationCode")
//
// *required*
AuthorizationURL Text `json:"authorizationUrl,omitempty"`
// The token URL to be used for this flow. This MUST be in the form of a
// URL. The OAuth2 standard requires the use of TLS.
//
// Applies to: OAuth2Flow ("password", "clientCredentials", "authorizationCode")
//
// *required*
TokenURL Text `json:"tokenUrl,omitempty"`
// The URL to be used for obtaining refresh tokens. This MUST be in the form
// of a URL. The OAuth2 standard requires the use of TLS.
RefreshURL Text `json:"refreshUrl,omitempty"`
// The available scopes for the OAuth2 security scheme. A map between the
// scope name and a short description for it. The map MAY be empty.
//
// *required*
Scopes *Scopes `json:"scopes"`
}
func (f *OAuthFlow) Refs() []Ref {
if f == nil {
return nil
}
return f.Scopes.Refs()
}
func (f *OAuthFlow) Anchors() (*Anchors, error) {
if f == nil {
return nil, nil
}
return f.Scopes.Anchors()
}
// func (f *OAuthFlow) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return f.resolveNodeByPointer(ptr)
// }
// func (f *OAuthFlow) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return f, nil
// }
// nxt, tok, _ := ptr.Next()
// switch tok {
// case "scopes":
// if f.Scopes == nil {
// return nil, newErrNotFound(f.Location.AbsoluteLocation(), tok)
// }
// return f.Scopes.resolveNodeByPointer(nxt)
// default:
// return nil, newErrNotResolvable(f.Location.AbsoluteLocation(), tok)
// }
// }
func (r *OAuthFlow) Nodes() []Node {
if r == nil {
return nil
}
return downcastNodes(r.nodes())
}
func (r *OAuthFlow) nodes() []node {
if r == nil {
return nil
}
return appendEdges(nil, r.Scopes)
}
func (*OAuthFlow) Kind() Kind { return KindOAuthFlow }
func (*OAuthFlow) mapKind() Kind { return KindUndefined }
func (*OAuthFlow) sliceKind() Kind { return KindUndefined }
func (o *OAuthFlow) setLocation(loc Location) error {
if o == nil {
return nil
}
o.Location = loc
o.Scopes.setLocation(loc.AppendLocation("scopes"))
return nil
}
// MarshalJSON marshals json
func (o OAuthFlow) MarshalJSON() ([]byte, error) {
type oauthflow OAuthFlow
return marshalExtendedJSON(oauthflow(o))
}
// UnmarshalJSON unmarshals json
func (o *OAuthFlow) UnmarshalJSON(data []byte) error {
type oauthflow OAuthFlow
var v oauthflow
if err := unmarshalExtendedJSON(data, &v); err != nil {
return err
}
*o = OAuthFlow(v)
return nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
func (o OAuthFlow) MarshalYAML() (interface{}, error) {
j, err := o.MarshalJSON()
if err != nil {
return nil, err
}
var v interface{}
err = json.Unmarshal(j, &v)
if err != nil {
return nil, err
}
return v, nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Unmarshaler interface
func (o *OAuthFlow) UnmarshalYAML(value *yaml.Node) error {
v, err := yaml.Marshal(value)
if err != nil {
return err
}
j, err := transcode.JSONFromYAML(v)
if err != nil {
return err
}
return json.Unmarshal(j, o)
}
func (o *OAuthFlow) isNil() bool { return o == nil }
// OAuthFlows allows configuration of the supported OAuth Flows.
type OAuthFlows struct {
Extensions `json:"-"`
Location `json:"-"`
// Configuration for the OAuth Implicit flow
Implicit *OAuthFlow `json:"implicit,omitempty"`
// Configuration for the OAuth Resource Owner Password flow
Password *OAuthFlow `json:"password,omitempty"`
// Configuration for the OAuth Client Credentials flow. Previously called
// application in OpenAPI 2.0.
ClientCredentials *OAuthFlow `json:"clientCredentials,omitempty"`
// Configuration for the OAuth Authorization Code flow. Previously called accessCode in OpenAPI 2.0.
AuthorizationCode *OAuthFlow `json:"authorizationCode,omitempty"`
}
func (f *OAuthFlows) Nodes() []Node {
if f == nil {
return nil
}
return downcastNodes(f.nodes())
}
func (f *OAuthFlows) nodes() []node {
if f == nil {
return nil
}
return appendEdges(nil, f.Implicit, f.Password, f.ClientCredentials, f.AuthorizationCode)
}
func (f *OAuthFlows) Refs() []Ref {
if f == nil {
return nil
}
var refs []Ref
refs = append(refs, f.Implicit.Refs()...)
refs = append(refs, f.Password.Refs()...)
refs = append(refs, f.ClientCredentials.Refs()...)
refs = append(refs, f.AuthorizationCode.Refs()...)
return refs
}
func (f *OAuthFlows) isNil() bool { return f == nil }
func (f *OAuthFlows) Anchors() (*Anchors, error) {
if f == nil {
return nil, nil
}
var anchors *Anchors
var err error
if anchors, err = anchors.merge(f.Implicit.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(f.Password.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(f.ClientCredentials.Anchors()); err != nil {
return nil, err
}
if anchors, err = anchors.merge(f.AuthorizationCode.Anchors()); err != nil {
return nil, err
}
return anchors, nil
}
// func (f *OAuthFlows) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return f.resolveNodeByPointer(ptr)
// }
// func (f *OAuthFlows) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return f, nil
// }
// nxt, tok, _ := ptr.Next()
// switch tok {
// case "implicit":
// if f.Implicit == nil {
// return nil, newErrNotFound(f.Location.AbsoluteLocation(), tok)
// }
// return f.Implicit.resolveNodeByPointer(nxt)
// case "password":
// if f.Password == nil {
// return nil, newErrNotFound(f.Location.AbsoluteLocation(), tok)
// }
// return f.Password.resolveNodeByPointer(nxt)
// case "clientCredentials":
// if f.ClientCredentials == nil {
// return nil, newErrNotFound(f.Location.AbsoluteLocation(), tok)
// }
// return f.ClientCredentials.resolveNodeByPointer(nxt)
// case "authorizationCode":
// if f.AuthorizationCode == nil {
// return nil, newErrNotFound(f.Location.AbsoluteLocation(), tok)
// }
// return f.AuthorizationCode.resolveNodeByPointer(nxt)
// default:
// return nil, newErrNotResolvable(f.Location.AbsoluteLocation(), tok)
// }
// }
func (*OAuthFlows) Kind() Kind { return KindOAuthFlows }
func (*OAuthFlows) mapKind() Kind { return KindUndefined }
func (*OAuthFlows) sliceKind() Kind { return KindUndefined }
func (o *OAuthFlows) setLocation(loc Location) error {
if o == nil {
return nil
}
o.Location = loc
if err := o.Implicit.setLocation(loc.AppendLocation("implicit")); err != nil {
return err
}
if err := o.Password.setLocation(loc.AppendLocation("password")); err != nil {
return err
}
if err := o.ClientCredentials.setLocation(loc.AppendLocation("clientCredentials")); err != nil {
return err
}
if err := o.AuthorizationCode.setLocation(loc.AppendLocation("authorizationCode")); err != nil {
return err
}
return nil
}
// MarshalJSON marshals json
func (o OAuthFlows) MarshalJSON() ([]byte, error) {
type oauthflows OAuthFlows
return marshalExtendedJSON(oauthflows(o))
}
// UnmarshalJSON unmarshals json
func (f *OAuthFlows) UnmarshalJSON(data []byte) error {
type oauthflows OAuthFlows
var v oauthflows
if err := unmarshalExtendedJSON(data, &v); err != nil {
return err
}
*f = OAuthFlows(v)
return nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
func (f OAuthFlows) MarshalYAML() (interface{}, error) {
j, err := f.MarshalJSON()
if err != nil {
return nil, err
}
var v interface{}
err = json.Unmarshal(j, &v)
if err != nil {
return nil, err
}
return v, nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Unmarshaler interface
func (f *OAuthFlows) UnmarshalYAML(value *yaml.Node) error {
v, err := yaml.Marshal(value)
if err != nil {
return err
}
j, err := transcode.JSONFromYAML(v)
if err != nil {
return err
}
return json.Unmarshal(j, f)
}
var (
_ node = (*OAuthFlow)(nil)
_ node = (*OAuthFlows)(nil)
)