podman
170 строк · 5.7 Кб
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
15package spec
16
17import (
18"encoding/json"
19
20"github.com/go-openapi/jsonpointer"
21"github.com/go-openapi/swag"
22)
23
24const (
25basic = "basic"
26apiKey = "apiKey"
27oauth2 = "oauth2"
28implicit = "implicit"
29password = "password"
30application = "application"
31accessCode = "accessCode"
32)
33
34// BasicAuth creates a basic auth security scheme
35func BasicAuth() *SecurityScheme {
36return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{Type: basic}}
37}
38
39// APIKeyAuth creates an api key auth security scheme
40func APIKeyAuth(fieldName, valueSource string) *SecurityScheme {
41return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{Type: apiKey, Name: fieldName, In: valueSource}}
42}
43
44// OAuth2Implicit creates an implicit flow oauth2 security scheme
45func OAuth2Implicit(authorizationURL string) *SecurityScheme {
46return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
47Type: oauth2,
48Flow: implicit,
49AuthorizationURL: authorizationURL,
50}}
51}
52
53// OAuth2Password creates a password flow oauth2 security scheme
54func OAuth2Password(tokenURL string) *SecurityScheme {
55return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
56Type: oauth2,
57Flow: password,
58TokenURL: tokenURL,
59}}
60}
61
62// OAuth2Application creates an application flow oauth2 security scheme
63func OAuth2Application(tokenURL string) *SecurityScheme {
64return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
65Type: oauth2,
66Flow: application,
67TokenURL: tokenURL,
68}}
69}
70
71// OAuth2AccessToken creates an access token flow oauth2 security scheme
72func OAuth2AccessToken(authorizationURL, tokenURL string) *SecurityScheme {
73return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
74Type: oauth2,
75Flow: accessCode,
76AuthorizationURL: authorizationURL,
77TokenURL: tokenURL,
78}}
79}
80
81// SecuritySchemeProps describes a swagger security scheme in the securityDefinitions section
82type SecuritySchemeProps struct {
83Description string `json:"description,omitempty"`
84Type string `json:"type"`
85Name string `json:"name,omitempty"` // api key
86In string `json:"in,omitempty"` // api key
87Flow string `json:"flow,omitempty"` // oauth2
88AuthorizationURL string `json:"authorizationUrl"` // oauth2
89TokenURL string `json:"tokenUrl,omitempty"` // oauth2
90Scopes map[string]string `json:"scopes,omitempty"` // oauth2
91}
92
93// AddScope adds a scope to this security scheme
94func (s *SecuritySchemeProps) AddScope(scope, description string) {
95if s.Scopes == nil {
96s.Scopes = make(map[string]string)
97}
98s.Scopes[scope] = description
99}
100
101// SecurityScheme allows the definition of a security scheme that can be used by the operations.
102// Supported schemes are basic authentication, an API key (either as a header or as a query parameter)
103// and OAuth2's common flows (implicit, password, application and access code).
104//
105// For more information: http://goo.gl/8us55a#securitySchemeObject
106type SecurityScheme struct {
107VendorExtensible
108SecuritySchemeProps
109}
110
111// JSONLookup implements an interface to customize json pointer lookup
112func (s SecurityScheme) JSONLookup(token string) (interface{}, error) {
113if ex, ok := s.Extensions[token]; ok {
114return &ex, nil
115}
116
117r, _, err := jsonpointer.GetForToken(s.SecuritySchemeProps, token)
118return r, err
119}
120
121// MarshalJSON marshal this to JSON
122func (s SecurityScheme) MarshalJSON() ([]byte, error) {
123var (
124b1 []byte
125err error
126)
127
128if s.Type == oauth2 && (s.Flow == "implicit" || s.Flow == "accessCode") {
129// when oauth2 for implicit or accessCode flows, empty AuthorizationURL is added as empty string
130b1, err = json.Marshal(s.SecuritySchemeProps)
131} else {
132// when not oauth2, empty AuthorizationURL should be omitted
133b1, err = json.Marshal(struct {
134Description string `json:"description,omitempty"`
135Type string `json:"type"`
136Name string `json:"name,omitempty"` // api key
137In string `json:"in,omitempty"` // api key
138Flow string `json:"flow,omitempty"` // oauth2
139AuthorizationURL string `json:"authorizationUrl,omitempty"` // oauth2
140TokenURL string `json:"tokenUrl,omitempty"` // oauth2
141Scopes map[string]string `json:"scopes,omitempty"` // oauth2
142}{
143Description: s.Description,
144Type: s.Type,
145Name: s.Name,
146In: s.In,
147Flow: s.Flow,
148AuthorizationURL: s.AuthorizationURL,
149TokenURL: s.TokenURL,
150Scopes: s.Scopes,
151})
152}
153if err != nil {
154return nil, err
155}
156
157b2, err := json.Marshal(s.VendorExtensible)
158if err != nil {
159return nil, err
160}
161return swag.ConcatJSON(b1, b2), nil
162}
163
164// UnmarshalJSON marshal this from JSON
165func (s *SecurityScheme) UnmarshalJSON(data []byte) error {
166if err := json.Unmarshal(data, &s.SecuritySchemeProps); err != nil {
167return err
168}
169return json.Unmarshal(data, &s.VendorExtensible)
170}
171