| 1 | // Copyright 2015 go-swagger maintainers |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package spec |
| 16 | |
| 17 | import ( |
| 18 | "encoding/json" |
| 19 | |
| 20 | "github.com/go-openapi/swag" |
| 21 | "k8s.io/kube-openapi/pkg/internal" |
| 22 | jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json" |
| 23 | ) |
| 24 | |
| 25 | // SecuritySchemeProps describes a swagger security scheme in the securityDefinitions section |
| 26 | type SecuritySchemeProps struct { |
| 27 | Description string `json:"description,omitempty"` |
| 28 | Type string `json:"type"` |
| 29 | Name string `json:"name,omitempty"` // api key |
| 30 | In string `json:"in,omitempty"` // api key |
| 31 | Flow string `json:"flow,omitempty"` // oauth2 |
| 32 | AuthorizationURL string `json:"authorizationUrl,omitempty"` // oauth2 |
| 33 | TokenURL string `json:"tokenUrl,omitempty"` // oauth2 |
| 34 | Scopes map[string]string `json:"scopes,omitempty"` // oauth2 |
| 35 | } |
| 36 | |
| 37 | // SecurityScheme allows the definition of a security scheme that can be used by the operations. |
| 38 | // Supported schemes are basic authentication, an API key (either as a header or as a query parameter) |
| 39 | // and OAuth2's common flows (implicit, password, application and access code). |
| 40 | // |
| 41 | // For more information: http://goo.gl/8us55a#securitySchemeObject |
| 42 | type SecurityScheme struct { |
| 43 | VendorExtensible |
| 44 | SecuritySchemeProps |
| 45 | } |
| 46 | |
| 47 | // MarshalJSON marshal this to JSON |
| 48 | func (s SecurityScheme) MarshalJSON() ([]byte, error) { |
| 49 | if internal.UseOptimizedJSONMarshaling { |
| 50 | return internal.DeterministicMarshal(s) |
| 51 | } |
| 52 | b1, err := json.Marshal(s.SecuritySchemeProps) |
| 53 | if err != nil { |
| 54 | return nil, err |
| 55 | } |
| 56 | b2, err := json.Marshal(s.VendorExtensible) |
| 57 | if err != nil { |
| 58 | return nil, err |
| 59 | } |
| 60 | return swag.ConcatJSON(b1, b2), nil |
| 61 | } |
| 62 | |
| 63 | func (s SecurityScheme) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error { |
| 64 | var x struct { |
| 65 | Extensions |
| 66 | SecuritySchemeProps |
| 67 | } |
| 68 | x.Extensions = internal.SanitizeExtensions(s.Extensions) |
| 69 | x.SecuritySchemeProps = s.SecuritySchemeProps |
| 70 | return opts.MarshalNext(enc, x) |
| 71 | } |
| 72 | |
| 73 | // UnmarshalJSON marshal this from JSON |
| 74 | func (s *SecurityScheme) UnmarshalJSON(data []byte) error { |
| 75 | if err := json.Unmarshal(data, &s.SecuritySchemeProps); err != nil { |
| 76 | return err |
| 77 | } |
| 78 | return json.Unmarshal(data, &s.VendorExtensible) |
| 79 | } |
| 80 | |
| 81 | func (s *SecurityScheme) UnmarshalNextJSON(opts jsonv2.UnmarshalOptions, dec *jsonv2.Decoder) error { |
| 82 | var x struct { |
| 83 | Extensions |
| 84 | SecuritySchemeProps |
| 85 | } |
| 86 | if err := opts.UnmarshalNext(dec, &x); err != nil { |
| 87 | return err |
| 88 | } |
| 89 | s.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 90 | s.SecuritySchemeProps = x.SecuritySchemeProps |
| 91 | return nil |
| 92 | } |