| 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 |
"k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext" |
| 24 |
) |
| 25 |
|
| 26 |
// SecuritySchemeProps describes a swagger security scheme in the securityDefinitions section |
| 27 |
type SecuritySchemeProps struct { |
| 28 |
Description string `json:"description,omitempty"` |
| 29 |
Type string `json:"type"` |
| 30 |
Name string `json:"name,omitempty"` // api key |
| 31 |
In string `json:"in,omitempty"` // api key |
| 32 |
Flow string `json:"flow,omitempty"` // oauth2 |
| 33 |
AuthorizationURL string `json:"authorizationUrl,omitempty"` // oauth2 |
| 34 |
TokenURL string `json:"tokenUrl,omitempty"` // oauth2 |
| 35 |
Scopes map[string]string `json:"scopes,omitempty"` // oauth2 |
| 36 |
} |
| 37 |
|
| 38 |
// SecurityScheme allows the definition of a security scheme that can be used by the operations. |
| 39 |
// Supported schemes are basic authentication, an API key (either as a header or as a query parameter) |
| 40 |
// and OAuth2's common flows (implicit, password, application and access code). |
| 41 |
// |
| 42 |
// For more information: http://goo.gl/8us55a#securitySchemeObject |
| 43 |
type SecurityScheme struct { |
| 44 |
VendorExtensible |
| 45 |
SecuritySchemeProps |
| 46 |
} |
| 47 |
|
| 48 |
// MarshalJSON marshal this to JSON |
| 49 |
func (s SecurityScheme) MarshalJSON() ([]byte, error) { |
| 50 |
if internal.UseOptimizedJSONMarshaling { |
| 51 |
return internal.DeterministicMarshal(s) |
| 52 |
} |
| 53 |
b1, err := json.Marshal(s.SecuritySchemeProps) |
| 54 |
if err != nil { |
| 55 |
return nil, err |
| 56 |
} |
| 57 |
b2, err := json.Marshal(s.VendorExtensible) |
| 58 |
if err != nil { |
| 59 |
return nil, err |
| 60 |
} |
| 61 |
return swag.ConcatJSON(b1, b2), nil |
| 62 |
} |
| 63 |
|
| 64 |
func (s SecurityScheme) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 65 |
var x struct { |
| 66 |
Extensions Extensions `json:",inline"` |
| 67 |
SecuritySchemeProps |
| 68 |
} |
| 69 |
x.Extensions = internal.SanitizeExtensions(s.Extensions) |
| 70 |
x.SecuritySchemeProps = s.SecuritySchemeProps |
| 71 |
return jsonv2.MarshalEncode(enc, x) |
| 72 |
} |
| 73 |
|
| 74 |
// UnmarshalJSON marshal this from JSON |
| 75 |
func (s *SecurityScheme) UnmarshalJSON(data []byte) error { |
| 76 |
if err := json.Unmarshal(data, &s.SecuritySchemeProps); err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
return json.Unmarshal(data, &s.VendorExtensible) |
| 80 |
} |
| 81 |
|
| 82 |
func (s *SecurityScheme) UnmarshalJSONFrom(dec *jsontext.Decoder) error { |
| 83 |
var x struct { |
| 84 |
Extensions Extensions `json:",inline"` |
| 85 |
SecuritySchemeProps |
| 86 |
} |
| 87 |
if err := jsonv2.UnmarshalDecode(dec, &x); err != nil { |
| 88 |
return err |
| 89 |
} |
| 90 |
s.Extensions = internal.SanitizeExtensions(x.Extensions) |
| 91 |
s.SecuritySchemeProps = x.SecuritySchemeProps |
| 92 |
return nil |
| 93 |
} |