123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- package spec
- import (
- "encoding/json"
- "github.com/go-openapi/jsonpointer"
- "github.com/go-openapi/swag"
- )
- const (
- basic = "basic"
- apiKey = "apiKey"
- oauth2 = "oauth2"
- implicit = "implicit"
- password = "password"
- application = "application"
- accessCode = "accessCode"
- )
- func BasicAuth() *SecurityScheme {
- return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{Type: basic}}
- }
- func APIKeyAuth(fieldName, valueSource string) *SecurityScheme {
- return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{Type: apiKey, Name: fieldName, In: valueSource}}
- }
- func OAuth2Implicit(authorizationURL string) *SecurityScheme {
- return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
- Type: oauth2,
- Flow: implicit,
- AuthorizationURL: authorizationURL,
- }}
- }
- func OAuth2Password(tokenURL string) *SecurityScheme {
- return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
- Type: oauth2,
- Flow: password,
- TokenURL: tokenURL,
- }}
- }
- func OAuth2Application(tokenURL string) *SecurityScheme {
- return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
- Type: oauth2,
- Flow: application,
- TokenURL: tokenURL,
- }}
- }
- func OAuth2AccessToken(authorizationURL, tokenURL string) *SecurityScheme {
- return &SecurityScheme{SecuritySchemeProps: SecuritySchemeProps{
- Type: oauth2,
- Flow: accessCode,
- AuthorizationURL: authorizationURL,
- TokenURL: tokenURL,
- }}
- }
- type SecuritySchemeProps struct {
- Description string `json:"description,omitempty"`
- Type string `json:"type"`
- Name string `json:"name,omitempty"`
- In string `json:"in,omitempty"`
- Flow string `json:"flow,omitempty"`
- AuthorizationURL string `json:"authorizationUrl"`
- TokenURL string `json:"tokenUrl,omitempty"`
- Scopes map[string]string `json:"scopes,omitempty"`
- }
- func (s *SecuritySchemeProps) AddScope(scope, description string) {
- if s.Scopes == nil {
- s.Scopes = make(map[string]string)
- }
- s.Scopes[scope] = description
- }
- type SecurityScheme struct {
- VendorExtensible
- SecuritySchemeProps
- }
- func (s SecurityScheme) JSONLookup(token string) (interface{}, error) {
- if ex, ok := s.Extensions[token]; ok {
- return &ex, nil
- }
- r, _, err := jsonpointer.GetForToken(s.SecuritySchemeProps, token)
- return r, err
- }
- func (s SecurityScheme) MarshalJSON() ([]byte, error) {
- var (
- b1 []byte
- err error
- )
- if s.Type == oauth2 && (s.Flow == "implicit" || s.Flow == "accessCode") {
-
- b1, err = json.Marshal(s.SecuritySchemeProps)
- } else {
-
- b1, err = json.Marshal(struct {
- Description string `json:"description,omitempty"`
- Type string `json:"type"`
- Name string `json:"name,omitempty"`
- In string `json:"in,omitempty"`
- Flow string `json:"flow,omitempty"`
- AuthorizationURL string `json:"authorizationUrl,omitempty"`
- TokenURL string `json:"tokenUrl,omitempty"`
- Scopes map[string]string `json:"scopes,omitempty"`
- }{
- Description: s.Description,
- Type: s.Type,
- Name: s.Name,
- In: s.In,
- Flow: s.Flow,
- AuthorizationURL: s.AuthorizationURL,
- TokenURL: s.TokenURL,
- Scopes: s.Scopes,
- })
- }
- if err != nil {
- return nil, err
- }
- b2, err := json.Marshal(s.VendorExtensible)
- if err != nil {
- return nil, err
- }
- return swag.ConcatJSON(b1, b2), nil
- }
- func (s *SecurityScheme) UnmarshalJSON(data []byte) error {
- if err := json.Unmarshal(data, &s.SecuritySchemeProps); err != nil {
- return err
- }
- return json.Unmarshal(data, &s.VendorExtensible)
- }
|