k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/spec3/security_scheme.go (about) 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/validation/spec" 26 ) 27 28 // SecurityScheme defines reusable Security Scheme Object, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#securitySchemeObject 29 type SecurityScheme struct { 30 spec.Refable 31 SecuritySchemeProps 32 spec.VendorExtensible 33 } 34 35 // MarshalJSON is a custom marshal function that knows how to encode SecurityScheme as JSON 36 func (s *SecurityScheme) MarshalJSON() ([]byte, error) { 37 if internal.UseOptimizedJSONMarshalingV3 { 38 return internal.DeterministicMarshal(s) 39 } 40 b1, err := json.Marshal(s.SecuritySchemeProps) 41 if err != nil { 42 return nil, err 43 } 44 b2, err := json.Marshal(s.VendorExtensible) 45 if err != nil { 46 return nil, err 47 } 48 b3, err := json.Marshal(s.Refable) 49 if err != nil { 50 return nil, err 51 } 52 return swag.ConcatJSON(b1, b2, b3), nil 53 } 54 55 func (s *SecurityScheme) MarshalNextJSON(opts jsonv2.MarshalOptions, enc *jsonv2.Encoder) error { 56 var x struct { 57 Ref string `json:"$ref,omitempty"` 58 SecuritySchemeProps `json:",inline"` 59 spec.Extensions 60 } 61 x.Ref = s.Refable.Ref.String() 62 x.Extensions = internal.SanitizeExtensions(s.Extensions) 63 x.SecuritySchemeProps = s.SecuritySchemeProps 64 return opts.MarshalNext(enc, x) 65 } 66 67 // UnmarshalJSON hydrates this items instance with the data from JSON 68 func (s *SecurityScheme) UnmarshalJSON(data []byte) error { 69 if err := json.Unmarshal(data, &s.SecuritySchemeProps); err != nil { 70 return err 71 } 72 if err := json.Unmarshal(data, &s.VendorExtensible); err != nil { 73 return err 74 } 75 return json.Unmarshal(data, &s.Refable) 76 } 77 78 // SecuritySchemeProps defines a security scheme that can be used by the operations 79 type SecuritySchemeProps struct { 80 // Type of the security scheme 81 Type string `json:"type,omitempty"` 82 // Description holds a short description for security scheme 83 Description string `json:"description,omitempty"` 84 // Name holds the name of the header, query or cookie parameter to be used 85 Name string `json:"name,omitempty"` 86 // In holds the location of the API key 87 In string `json:"in,omitempty"` 88 // Scheme holds the name of the HTTP Authorization scheme to be used in the Authorization header 89 Scheme string `json:"scheme,omitempty"` 90 // BearerFormat holds a hint to the client to identify how the bearer token is formatted 91 BearerFormat string `json:"bearerFormat,omitempty"` 92 // Flows contains configuration information for the flow types supported. 93 Flows map[string]*OAuthFlow `json:"flows,omitempty"` 94 // OpenIdConnectUrl holds an url to discover OAuth2 configuration values from 95 OpenIdConnectUrl string `json:"openIdConnectUrl,omitempty"` 96 } 97 98 // OAuthFlow contains configuration information for the flow types supported. 99 type OAuthFlow struct { 100 OAuthFlowProps 101 spec.VendorExtensible 102 } 103 104 // MarshalJSON is a custom marshal function that knows how to encode OAuthFlow as JSON 105 func (o *OAuthFlow) MarshalJSON() ([]byte, error) { 106 b1, err := json.Marshal(o.OAuthFlowProps) 107 if err != nil { 108 return nil, err 109 } 110 b2, err := json.Marshal(o.VendorExtensible) 111 if err != nil { 112 return nil, err 113 } 114 return swag.ConcatJSON(b1, b2), nil 115 } 116 117 // UnmarshalJSON hydrates this items instance with the data from JSON 118 func (o *OAuthFlow) UnmarshalJSON(data []byte) error { 119 if err := json.Unmarshal(data, &o.OAuthFlowProps); err != nil { 120 return err 121 } 122 return json.Unmarshal(data, &o.VendorExtensible) 123 } 124 125 // OAuthFlowProps holds configuration details for a supported OAuth Flow 126 type OAuthFlowProps struct { 127 // AuthorizationUrl hold the authorization URL to be used for this flow 128 AuthorizationUrl string `json:"authorizationUrl,omitempty"` 129 // TokenUrl holds the token URL to be used for this flow 130 TokenUrl string `json:"tokenUrl,omitempty"` 131 // RefreshUrl holds the URL to be used for obtaining refresh tokens 132 RefreshUrl string `json:"refreshUrl,omitempty"` 133 // Scopes holds the available scopes for the OAuth2 security scheme 134 Scopes map[string]string `json:"scopes,omitempty"` 135 }