security_scheme.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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/jsonpointer"
  18. "github.com/go-openapi/swag"
  19. )
  20. const (
  21. basic = "basic"
  22. apiKey = "apiKey"
  23. oauth2 = "oauth2"
  24. implicit = "implicit"
  25. password = "password"
  26. application = "application"
  27. accessCode = "accessCode"
  28. )
  29. // BasicAuth creates a basic auth security scheme
  30. func BasicAuth() *SecurityScheme {
  31. return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{Type: basic}}
  32. }
  33. // APIKeyAuth creates an api key auth security scheme
  34. func APIKeyAuth(fieldName, valueSource string) *SecurityScheme {
  35. return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{Type: apiKey, Name: fieldName, In: valueSource}}
  36. }
  37. // OAuth2Implicit creates an implicit flow oauth2 security scheme
  38. func OAuth2Implicit(authorizationURL string) *SecurityScheme {
  39. return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
  40. Type: oauth2,
  41. Flow: implicit,
  42. AuthorizationURL: authorizationURL,
  43. }}
  44. }
  45. // OAuth2Password creates a password flow oauth2 security scheme
  46. func OAuth2Password(tokenURL string) *SecurityScheme {
  47. return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
  48. Type: oauth2,
  49. Flow: password,
  50. TokenURL: tokenURL,
  51. }}
  52. }
  53. // OAuth2Application creates an application flow oauth2 security scheme
  54. func OAuth2Application(tokenURL string) *SecurityScheme {
  55. return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
  56. Type: oauth2,
  57. Flow: application,
  58. TokenURL: tokenURL,
  59. }}
  60. }
  61. // OAuth2AccessToken creates an access token flow oauth2 security scheme
  62. func OAuth2AccessToken(authorizationURL, tokenURL string) *SecurityScheme {
  63. return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
  64. Type: oauth2,
  65. Flow: accessCode,
  66. AuthorizationURL: authorizationURL,
  67. TokenURL: tokenURL,
  68. }}
  69. }
  70. type SecuritySchemeProps struct {
  71. Description string `json:"description,omitempty"`
  72. Type string `json:"type"`
  73. Name string `json:"name,omitempty"` // api key
  74. In string `json:"in,omitempty"` // api key
  75. Flow string `json:"flow,omitempty"` // oauth2
  76. AuthorizationURL string `json:"authorizationUrl,omitempty"` // oauth2
  77. TokenURL string `json:"tokenUrl,omitempty"` // oauth2
  78. Scopes map[string]string `json:"scopes,omitempty"` // oauth2
  79. }
  80. // AddScope adds a scope to this security scheme
  81. func (s *SecuritySchemeProps) AddScope(scope, description string) {
  82. if s.Scopes == nil {
  83. s.Scopes = make(map[string]string)
  84. }
  85. s.Scopes[scope] = description
  86. }
  87. // SecurityScheme allows the definition of a security scheme that can be used by the operations.
  88. // Supported schemes are basic authentication, an API key (either as a header or as a query parameter)
  89. // and OAuth2's common flows (implicit, password, application and access code).
  90. //
  91. // For more information: http://goo.gl/8us55a#securitySchemeObject
  92. type SecurityScheme struct {
  93. VendorExtensible
  94. SecuritySchemeProps
  95. }
  96. // JSONLookup implements an interface to customize json pointer lookup
  97. func (s SecurityScheme) JSONLookup(token string) (interface{}, error) {
  98. if ex, ok := s.Extensions[token]; ok {
  99. return &ex, nil
  100. }
  101. r, _, err := jsonpointer.GetForToken(s.SecuritySchemeProps, token)
  102. return r, err
  103. }
  104. // MarshalJSON marshal this to JSON
  105. func (s SecurityScheme) MarshalJSON() ([]byte, error) {
  106. b1, err := json.Marshal(s.SecuritySchemeProps)
  107. if err != nil {
  108. return nil, err
  109. }
  110. b2, err := json.Marshal(s.VendorExtensible)
  111. if err != nil {
  112. return nil, err
  113. }
  114. return swag.ConcatJSON(b1, b2), nil
  115. }
  116. // UnmarshalJSON marshal this from JSON
  117. func (s *SecurityScheme) UnmarshalJSON(data []byte) error {
  118. if err := json.Unmarshal(data, &s.SecuritySchemeProps); err != nil {
  119. return err
  120. }
  121. if err := json.Unmarshal(data, &s.VendorExtensible); err != nil {
  122. return err
  123. }
  124. return nil
  125. }