security_scheme.go 4.6 KB

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