本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.6KB

  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. // SecuritySchemeProps describes a swagger security scheme in the securityDefinitions section
  71. type SecuritySchemeProps struct {
  72. Description string `json:"description,omitempty"`
  73. Type string `json:"type"`
  74. Name string `json:"name,omitempty"` // api key
  75. In string `json:"in,omitempty"` // api key
  76. Flow string `json:"flow,omitempty"` // oauth2
  77. AuthorizationURL string `json:"authorizationUrl,omitempty"` // oauth2
  78. TokenURL string `json:"tokenUrl,omitempty"` // oauth2
  79. Scopes map[string]string `json:"scopes,omitempty"` // oauth2
  80. }
  81. // AddScope adds a scope to this security scheme
  82. func (s *SecuritySchemeProps) AddScope(scope, description string) {
  83. if s.Scopes == nil {
  84. s.Scopes = make(map[string]string)
  85. }
  86. s.Scopes[scope] = description
  87. }
  88. // SecurityScheme allows the definition of a security scheme that can be used by the operations.
  89. // Supported schemes are basic authentication, an API key (either as a header or as a query parameter)
  90. // and OAuth2's common flows (implicit, password, application and access code).
  91. //
  92. // For more information: http://goo.gl/8us55a#securitySchemeObject
  93. type SecurityScheme struct {
  94. VendorExtensible
  95. SecuritySchemeProps
  96. }
  97. // JSONLookup implements an interface to customize json pointer lookup
  98. func (s SecurityScheme) JSONLookup(token string) (interface{}, error) {
  99. if ex, ok := s.Extensions[token]; ok {
  100. return &ex, nil
  101. }
  102. r, _, err := jsonpointer.GetForToken(s.SecuritySchemeProps, token)
  103. return r, err
  104. }
  105. // MarshalJSON marshal this to JSON
  106. func (s SecurityScheme) MarshalJSON() ([]byte, error) {
  107. b1, err := json.Marshal(s.SecuritySchemeProps)
  108. if err != nil {
  109. return nil, err
  110. }
  111. b2, err := json.Marshal(s.VendorExtensible)
  112. if err != nil {
  113. return nil, err
  114. }
  115. return swag.ConcatJSON(b1, b2), nil
  116. }
  117. // UnmarshalJSON marshal this from JSON
  118. func (s *SecurityScheme) UnmarshalJSON(data []byte) error {
  119. if err := json.Unmarshal(data, &s.SecuritySchemeProps); err != nil {
  120. return err
  121. }
  122. return json.Unmarshal(data, &s.VendorExtensible)
  123. }
上海开阖软件有限公司 沪ICP备12045867号-1