security_scheme.go 2.8 KB

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