github.com/shogo82148/goa-v1@v1.6.2/security.go (about)

     1  package goa
     2  
     3  import "context"
     4  
     5  // Location is the enum defining where the value of key based security schemes should be read:
     6  // either a HTTP request header or a URL querystring value
     7  type Location string
     8  
     9  // LocHeader indicates the secret value should be loaded from the request headers.
    10  const LocHeader Location = "header"
    11  
    12  // LocQuery indicates the secret value should be loaded from the request URL querystring.
    13  const LocQuery Location = "query"
    14  
    15  // ContextRequiredScopes extracts the security scopes from the given context.
    16  // This should be used in auth handlers to validate that the required scopes are present in the
    17  // JWT or OAuth2 token.
    18  func ContextRequiredScopes(ctx context.Context) []string {
    19  	if s := ctx.Value(securityScopesKey); s != nil {
    20  		return s.([]string)
    21  	}
    22  	return nil
    23  }
    24  
    25  // WithRequiredScopes builds a context containing the given required scopes.
    26  func WithRequiredScopes(ctx context.Context, scopes []string) context.Context {
    27  	return context.WithValue(ctx, securityScopesKey, scopes)
    28  }
    29  
    30  // OAuth2Security represents the `oauth2` security scheme. It is instantiated by the generated code
    31  // accordingly to the use of the different `*Security()` DSL functions and `Security()` in the
    32  // design.
    33  type OAuth2Security struct {
    34  	// Description of the security scheme
    35  	Description string
    36  	// Flow defines the OAuth2 flow type. See http://swagger.io/specification/#securitySchemeObject
    37  	Flow string
    38  	// TokenURL defines the OAuth2 tokenUrl.  See http://swagger.io/specification/#securitySchemeObject
    39  	TokenURL string
    40  	// AuthorizationURL defines the OAuth2 authorizationUrl.  See http://swagger.io/specification/#securitySchemeObject
    41  	AuthorizationURL string
    42  	// Scopes defines a list of scopes for the security scheme, along with their description.
    43  	Scopes map[string]string
    44  }
    45  
    46  // BasicAuthSecurity represents the `Basic` security scheme, which consists of a simple login/pass,
    47  // accessible through Request.BasicAuth().
    48  type BasicAuthSecurity struct {
    49  	// Description of the security scheme
    50  	Description string
    51  }
    52  
    53  // APIKeySecurity represents the `apiKey` security scheme. It handles a key that can be in the
    54  // headers or in the query parameters, and does authentication based on that.  The Name field
    55  // represents the key of either the query string parameter or the header, depending on the In field.
    56  type APIKeySecurity struct {
    57  	// Description of the security scheme
    58  	Description string
    59  	// In represents where to check for some data, `query` or `header`
    60  	In Location
    61  	// Name is the name of the `header` or `query` parameter to check for data.
    62  	Name string
    63  }
    64  
    65  // JWTSecurity represents an api key based scheme, with support for scopes and a token URL.
    66  type JWTSecurity struct {
    67  	// Description of the security scheme
    68  	Description string
    69  	// In represents where to check for the JWT, `query` or `header`
    70  	In Location
    71  	// Name is the name of the `header` or `query` parameter to check for data.
    72  	Name string
    73  	// TokenURL defines the URL where you'd get the JWT tokens.
    74  	TokenURL string
    75  	// Scopes defines a list of scopes for the security scheme, along with their description.
    76  	Scopes map[string]string
    77  }