security_scheme.go 3.1 KB

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