| 1 |
/* |
| 2 |
Copyright 2021 The Kubernetes Authors. |
| 3 |
|
| 4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
you may not use this file except in compliance with the License. |
| 6 |
You may obtain a copy of the License at |
| 7 |
|
| 8 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
|
| 10 |
Unless required by applicable law or agreed to in writing, software |
| 11 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
See the License for the specific language governing permissions and |
| 14 |
limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package spec3 |
| 18 |
|
| 19 |
import ( |
| 20 |
"encoding/json" |
| 21 |
|
| 22 |
"github.com/go-openapi/swag" |
| 23 |
"k8s.io/kube-openapi/pkg/internal" |
| 24 |
jsonv2 "k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json" |
| 25 |
"k8s.io/kube-openapi/pkg/internal/third_party/go-json-experiment/json/jsontext" |
| 26 |
"k8s.io/kube-openapi/pkg/validation/spec" |
| 27 |
) |
| 28 |
|
| 29 |
// SecurityScheme defines reusable Security Scheme Object, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#securitySchemeObject |
| 30 |
type SecurityScheme struct { |
| 31 |
spec.Refable |
| 32 |
SecuritySchemeProps |
| 33 |
spec.VendorExtensible |
| 34 |
} |
| 35 |
|
| 36 |
// MarshalJSON is a custom marshal function that knows how to encode SecurityScheme as JSON |
| 37 |
func (s *SecurityScheme) MarshalJSON() ([]byte, error) { |
| 38 |
if internal.UseOptimizedJSONMarshalingV3 { |
| 39 |
return internal.DeterministicMarshal(s) |
| 40 |
} |
| 41 |
b1, err := json.Marshal(s.SecuritySchemeProps) |
| 42 |
if err != nil { |
| 43 |
return nil, err |
| 44 |
} |
| 45 |
b2, err := json.Marshal(s.VendorExtensible) |
| 46 |
if err != nil { |
| 47 |
return nil, err |
| 48 |
} |
| 49 |
b3, err := json.Marshal(s.Refable) |
| 50 |
if err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
return swag.ConcatJSON(b1, b2, b3), nil |
| 54 |
} |
| 55 |
|
| 56 |
func (s *SecurityScheme) MarshalJSONTo(enc *jsontext.Encoder) error { |
| 57 |
var x struct { |
| 58 |
Ref string `json:"$ref,omitempty"` |
| 59 |
SecuritySchemeProps `json:",inline"` |
| 60 |
Extensions spec.Extensions `json:",inline"` |
| 61 |
} |
| 62 |
x.Ref = s.Refable.Ref.String() |
| 63 |
x.Extensions = internal.SanitizeExtensions(s.Extensions) |
| 64 |
x.SecuritySchemeProps = s.SecuritySchemeProps |
| 65 |
return jsonv2.MarshalEncode(enc, x) |
| 66 |
} |
| 67 |
|
| 68 |
// UnmarshalJSON hydrates this items instance with the data from JSON |
| 69 |
func (s *SecurityScheme) UnmarshalJSON(data []byte) error { |
| 70 |
if err := json.Unmarshal(data, &s.SecuritySchemeProps); err != nil { |
| 71 |
return err |
| 72 |
} |
| 73 |
if err := json.Unmarshal(data, &s.VendorExtensible); err != nil { |
| 74 |
return err |
| 75 |
} |
| 76 |
return json.Unmarshal(data, &s.Refable) |
| 77 |
} |
| 78 |
|
| 79 |
// SecuritySchemeProps defines a security scheme that can be used by the operations |
| 80 |
type SecuritySchemeProps struct { |
| 81 |
// Type of the security scheme |
| 82 |
Type string `json:"type,omitempty"` |
| 83 |
// Description holds a short description for security scheme |
| 84 |
Description string `json:"description,omitempty"` |
| 85 |
// Name holds the name of the header, query or cookie parameter to be used |
| 86 |
Name string `json:"name,omitempty"` |
| 87 |
// In holds the location of the API key |
| 88 |
In string `json:"in,omitempty"` |
| 89 |
// Scheme holds the name of the HTTP Authorization scheme to be used in the Authorization header |
| 90 |
Scheme string `json:"scheme,omitempty"` |
| 91 |
// BearerFormat holds a hint to the client to identify how the bearer token is formatted |
| 92 |
BearerFormat string `json:"bearerFormat,omitempty"` |
| 93 |
// Flows contains configuration information for the flow types supported. |
| 94 |
Flows map[string]*OAuthFlow `json:"flows,omitempty"` |
| 95 |
// OpenIdConnectUrl holds an url to discover OAuth2 configuration values from |
| 96 |
OpenIdConnectUrl string `json:"openIdConnectUrl,omitempty"` |
| 97 |
} |
| 98 |
|
| 99 |
// OAuthFlow contains configuration information for the flow types supported. |
| 100 |
type OAuthFlow struct { |
| 101 |
OAuthFlowProps |
| 102 |
spec.VendorExtensible |
| 103 |
} |
| 104 |
|
| 105 |
// MarshalJSON is a custom marshal function that knows how to encode OAuthFlow as JSON |
| 106 |
func (o *OAuthFlow) MarshalJSON() ([]byte, error) { |
| 107 |
b1, err := json.Marshal(o.OAuthFlowProps) |
| 108 |
if err != nil { |
| 109 |
return nil, err |
| 110 |
} |
| 111 |
b2, err := json.Marshal(o.VendorExtensible) |
| 112 |
if err != nil { |
| 113 |
return nil, err |
| 114 |
} |
| 115 |
return swag.ConcatJSON(b1, b2), nil |
| 116 |
} |
| 117 |
|
| 118 |
// UnmarshalJSON hydrates this items instance with the data from JSON |
| 119 |
func (o *OAuthFlow) UnmarshalJSON(data []byte) error { |
| 120 |
if err := json.Unmarshal(data, &o.OAuthFlowProps); err != nil { |
| 121 |
return err |
| 122 |
} |
| 123 |
return json.Unmarshal(data, &o.VendorExtensible) |
| 124 |
} |
| 125 |
|
| 126 |
// OAuthFlowProps holds configuration details for a supported OAuth Flow |
| 127 |
type OAuthFlowProps struct { |
| 128 |
// AuthorizationUrl hold the authorization URL to be used for this flow |
| 129 |
AuthorizationUrl string `json:"authorizationUrl,omitempty"` |
| 130 |
// TokenUrl holds the token URL to be used for this flow |
| 131 |
TokenUrl string `json:"tokenUrl,omitempty"` |
| 132 |
// RefreshUrl holds the URL to be used for obtaining refresh tokens |
| 133 |
RefreshUrl string `json:"refreshUrl,omitempty"` |
| 134 |
// Scopes holds the available scopes for the OAuth2 security scheme |
| 135 |
Scopes map[string]string `json:"scopes,omitempty"` |
| 136 |
} |