security_scheme.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. "k8s.io/kube-openapi/pkg/validation/spec"
  17. "github.com/go-openapi/swag"
  18. )
  19. // SecurityScheme defines reusable Security Scheme Object, more at https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#securitySchemeObject
  20. type SecurityScheme struct {
  21. spec.Refable
  22. SecuritySchemeProps
  23. spec.VendorExtensible
  24. }
  25. // MarshalJSON is a custom marshal function that knows how to encode SecurityScheme as JSON
  26. func (s *SecurityScheme) MarshalJSON() ([]byte, error) {
  27. b1, err := json.Marshal(s.SecuritySchemeProps)
  28. if err != nil {
  29. return nil, err
  30. }
  31. b2, err := json.Marshal(s.VendorExtensible)
  32. if err != nil {
  33. return nil, err
  34. }
  35. b3, err := json.Marshal(s.Refable)
  36. if err != nil {
  37. return nil, err
  38. }
  39. return swag.ConcatJSON(b1, b2, b3), nil
  40. }
  41. // UnmarshalJSON hydrates this items instance with the data from JSON
  42. func (s *SecurityScheme) UnmarshalJSON(data []byte) error {
  43. if err := json.Unmarshal(data, &s.SecuritySchemeProps); err != nil {
  44. return err
  45. }
  46. if err := json.Unmarshal(data, &s.VendorExtensible); err != nil {
  47. return err
  48. }
  49. return json.Unmarshal(data, &s.Refable)
  50. }
  51. // SecuritySchemeProps defines a security scheme that can be used by the operations
  52. type SecuritySchemeProps struct {
  53. // Type of the security scheme
  54. Type string `json:"type,omitempty"`
  55. // Description holds a short description for security scheme
  56. Description string `json:"description,omitempty"`
  57. // Name holds the name of the header, query or cookie parameter to be used
  58. Name string `json:"name,omitempty"`
  59. // In holds the location of the API key
  60. In string `json:"in,omitempty"`
  61. // Scheme holds the name of the HTTP Authorization scheme to be used in the Authorization header
  62. Scheme string `json:"scheme,omitempty"`
  63. // BearerFormat holds a hint to the client to identify how the bearer token is formatted
  64. BearerFormat string `json:"bearerFormat,omitempty"`
  65. // Flows contains configuration information for the flow types supported.
  66. Flows map[string]*OAuthFlow `json:"flows,omitempty"`
  67. // OpenIdConnectUrl holds an url to discover OAuth2 configuration values from
  68. OpenIdConnectUrl string `json:"openIdConnectUrl,omitempty"`
  69. }
  70. // OAuthFlow contains configuration information for the flow types supported.
  71. type OAuthFlow struct {
  72. OAuthFlowProps
  73. spec.VendorExtensible
  74. }
  75. // MarshalJSON is a custom marshal function that knows how to encode OAuthFlow as JSON
  76. func (o *OAuthFlow) MarshalJSON() ([]byte, error) {
  77. b1, err := json.Marshal(o.OAuthFlowProps)
  78. if err != nil {
  79. return nil, err
  80. }
  81. b2, err := json.Marshal(o.VendorExtensible)
  82. if err != nil {
  83. return nil, err
  84. }
  85. return swag.ConcatJSON(b1, b2), nil
  86. }
  87. // UnmarshalJSON hydrates this items instance with the data from JSON
  88. func (o *OAuthFlow) UnmarshalJSON(data []byte) error {
  89. if err := json.Unmarshal(data, &o.OAuthFlowProps); err != nil {
  90. return err
  91. }
  92. return json.Unmarshal(data, &o.VendorExtensible)
  93. }
  94. // OAuthFlowProps holds configuration details for a supported OAuth Flow
  95. type OAuthFlowProps struct {
  96. // AuthorizationUrl hold the authorization URL to be used for this flow
  97. AuthorizationUrl string `json:"authorizationUrl,omitempty"`
  98. // TokenUrl holds the token URL to be used for this flow
  99. TokenUrl string `json:"tokenUrl,omitempty"`
  100. // RefreshUrl holds the URL to be used for obtaining refresh tokens
  101. RefreshUrl string `json:"refreshUrl,omitempty"`
  102. // Scopes holds the available scopes for the OAuth2 security scheme
  103. Scopes map[string]string `json:"scopes,omitempty"`
  104. }