classAPIKeyCookieAuth(_BaseAPIKeyAuth):"""Flask-HTTPAuth's HTTPTokenAuth with some modifications to implement cookie APIKey authentication. - Add an authentication error handler that returns JSON response. - Expose the `auth.current_user` as a property. - Add a `description` attribute for OpenAPI Spec. - Add a `name` attribute for OpenAPI Spec. - Add a `param_name` attribute for API Key paramter. - Add the `get_security_scheme` method for OpenAPI Spec. Examples: ```python from apiflask import APIFlask, APIKeyCookieAuth app = APIFlask(__name__) auth = APIKeyCookieAuth() ``` """# noqa: E501def__init__(self,name:str='ApiKeyAuth',scheme:str='ApiKey',realm:str|None=None,param_name:str|None='X-API-Key',description:str|None=None,security_scheme_name:str|None=None,)->None:"""Initialize a `APIKeyCookieAuth` object. Arguments: name: The security scheme name, default to `ApiKeyAuth`. scheme: The authentication scheme used in the `WWW-Authenticate` header. defaults to `'ApiKey'`. realm: The realm used in the `WWW-Authenticate` header to indicate a scope of protection, defaults to use `'Authentication Required'`. param_name: The name of API Key paramter. defaults to `'ApiKey'`. description: The description of the OpenAPI security scheme. security_scheme_name: The name of the OpenAPI security scheme, defaults to `ApiKeyAuth`. *Version added: 3.0.0* """super().__init__(name=name,scheme=scheme,realm=realm,param_name=param_name,description=description,security_scheme_name=security_scheme_name,)self._location='cookie'defis_compatible_auth(self,headers):returnself.param_nameinrequest.cookiesdefget_auth(self)->Authorization:auth=Authorization('apiKey')auth.token=request.cookies.get(self.param_name,'')# type: ignorereturnauth
def__init__(self,name:str='ApiKeyAuth',scheme:str='ApiKey',realm:str|None=None,param_name:str|None='X-API-Key',description:str|None=None,security_scheme_name:str|None=None,)->None:"""Initialize a `APIKeyCookieAuth` object. Arguments: name: The security scheme name, default to `ApiKeyAuth`. scheme: The authentication scheme used in the `WWW-Authenticate` header. defaults to `'ApiKey'`. realm: The realm used in the `WWW-Authenticate` header to indicate a scope of protection, defaults to use `'Authentication Required'`. param_name: The name of API Key paramter. defaults to `'ApiKey'`. description: The description of the OpenAPI security scheme. security_scheme_name: The name of the OpenAPI security scheme, defaults to `ApiKeyAuth`. *Version added: 3.0.0* """super().__init__(name=name,scheme=scheme,realm=realm,param_name=param_name,description=description,security_scheme_name=security_scheme_name,)self._location='cookie'
classAPIKeyHeaderAuth(_BaseAPIKeyAuth):"""Flask-HTTPAuth's HTTPTokenAuth with some modifications to implement header APIKey authentication. - Add an authentication error handler that returns JSON response. - Expose the `auth.current_user` as a property. - Add a `description` attribute for OpenAPI Spec. - Add a `name` attribute for OpenAPI Spec. - Add a `param_name` attribute for API Key paramter. - Add the `get_security_scheme` method for OpenAPI Spec. Examples: ```python from apiflask import APIFlask, APIKeyHeaderAuth app = APIFlask(__name__) auth = APIKeyHeaderAuth() ``` """# noqa: E501def__init__(self,name:str='ApiKeyAuth',scheme:str='ApiKey',realm:str|None=None,param_name:str|None='X-API-Key',description:str|None=None,security_scheme_name:str|None=None,)->None:"""Initialize a `APIKeyHeaderAuth` object. Arguments: name: The security scheme name, default to `ApiKeyAuth`. scheme: The authentication scheme used in the `WWW-Authenticate` header. defaults to `'ApiKey'`. realm: The realm used in the `WWW-Authenticate` header to indicate a scope of protection, defaults to use `'Authentication Required'`. param_name: The name of API Key paramter. defaults to `'Authorization'`. description: The description of the OpenAPI security scheme. security_scheme_name: The name of the OpenAPI security scheme, defaults to `ApiKeyAuth`. *Version added: 3.0.0* """super().__init__(name=name,scheme=scheme,realm=realm,param_name=param_name,description=description,security_scheme_name=security_scheme_name,)
def__init__(self,name:str='ApiKeyAuth',scheme:str='ApiKey',realm:str|None=None,param_name:str|None='X-API-Key',description:str|None=None,security_scheme_name:str|None=None,)->None:"""Initialize a `APIKeyHeaderAuth` object. Arguments: name: The security scheme name, default to `ApiKeyAuth`. scheme: The authentication scheme used in the `WWW-Authenticate` header. defaults to `'ApiKey'`. realm: The realm used in the `WWW-Authenticate` header to indicate a scope of protection, defaults to use `'Authentication Required'`. param_name: The name of API Key paramter. defaults to `'Authorization'`. description: The description of the OpenAPI security scheme. security_scheme_name: The name of the OpenAPI security scheme, defaults to `ApiKeyAuth`. *Version added: 3.0.0* """super().__init__(name=name,scheme=scheme,realm=realm,param_name=param_name,description=description,security_scheme_name=security_scheme_name,)
classAPIKeyQueryAuth(_BaseAPIKeyAuth):"""Flask-HTTPAuth's HTTPTokenAuth with some modifications to implement query parameter APIKey authentication. - Add an authentication error handler that returns JSON response. - Expose the `auth.current_user` as a property. - Add a `description` attribute for OpenAPI Spec. - Add a `name` attribute for OpenAPI Spec. - Add a `param_name` attribute for API Key paramter. - Add the `get_security_scheme` method for OpenAPI Spec. Examples: ```python from apiflask import APIFlask, APIKeyQueryAuth app = APIFlask(__name__) auth = APIKeyQueryAuth() ``` """# noqa: E501def__init__(self,name:str='ApiKeyAuth',scheme:str='ApiKey',realm:str|None=None,param_name:str|None='X-API-Key',description:str|None=None,security_scheme_name:str|None=None,)->None:"""Initialize a `APIKeyQueryAuth` object. Arguments: name: The security scheme name, default to `ApiKeyAuth`. scheme: The authentication scheme used in the `WWW-Authenticate` header. defaults to `'ApiKey'`. realm: The realm used in the `WWW-Authenticate` header to indicate a scope of protection, defaults to use `'Authentication Required'`. param_name: The name of API Key paramter. defaults to `'ApiKey'`. description: The description of the OpenAPI security scheme. security_scheme_name: The name of the OpenAPI security scheme, defaults to `ApiKeyAuth`. *Version added: 3.0.0* """super().__init__(name=name,scheme=scheme,realm=realm,param_name=param_name,description=description,security_scheme_name=security_scheme_name,)self._location='query'defis_compatible_auth(self,headers):returnself.param_nameinrequest.argsdefget_auth(self)->Authorization:auth=Authorization('apiKey')auth.token=request.args.get(self.param_name,'')# type: ignorereturnauth
def__init__(self,name:str='ApiKeyAuth',scheme:str='ApiKey',realm:str|None=None,param_name:str|None='X-API-Key',description:str|None=None,security_scheme_name:str|None=None,)->None:"""Initialize a `APIKeyQueryAuth` object. Arguments: name: The security scheme name, default to `ApiKeyAuth`. scheme: The authentication scheme used in the `WWW-Authenticate` header. defaults to `'ApiKey'`. realm: The realm used in the `WWW-Authenticate` header to indicate a scope of protection, defaults to use `'Authentication Required'`. param_name: The name of API Key paramter. defaults to `'ApiKey'`. description: The description of the OpenAPI security scheme. security_scheme_name: The name of the OpenAPI security scheme, defaults to `ApiKeyAuth`. *Version added: 3.0.0* """super().__init__(name=name,scheme=scheme,realm=realm,param_name=param_name,description=description,security_scheme_name=security_scheme_name,)self._location='query'
classHTTPBasicAuth(_AuthBase,BaseHTTPBasicAuth,SecurityScheme):"""Flask-HTTPAuth's HTTPBasicAuth with some modifications. - Add an authentication error handler that returns JSON response. - Expose the `auth.current_user` as a property. - Add a `description` attribute for OpenAPI Spec. - Add a `name` attribute for OpenAPI Spec. - Add the `get_security_scheme` method for OpenAPI Spec. Examples: ```python from apiflask import APIFlask, HTTPBasicAuth app = APIFlask(__name__) auth = HTTPBasicAuth() ``` *Version changed: 3.0.0* - Add parameter `name`. *Version changed: 1.3.0* - Add `security_scheme_name` parameter. """def__init__(self,name:str='BasicAuth',scheme:str='Basic',realm:str|None=None,description:str|None=None,security_scheme_name:str|None=None,)->None:"""Initialize an `HTTPBasicAuth` object. Arguments: name: The security scheme name, default to `BasicAuth`. scheme: The authentication scheme used in the `WWW-Authenticate` header. Defaults to `'Basic'`. realm: The realm used in the `WWW-Authenticate` header to indicate a scope of protection, defaults to use `'Authentication Required'`. description: The description of the OpenAPI security scheme. security_scheme_name: The name of the OpenAPI security scheme, default to `BasicAuth`. """BaseHTTPBasicAuth.__init__(self,scheme=scheme,realm=realm)super().__init__(description=description,security_scheme_name=security_scheme_name)self.name=security_scheme_nameornamedefget_security_scheme(self)->dict[str,t.Any]:security_scheme={'type':'http','scheme':'basic',}ifself.descriptionisnotNone:security_scheme['description']=self.descriptionreturnsecurity_scheme
def__init__(self,name:str='BasicAuth',scheme:str='Basic',realm:str|None=None,description:str|None=None,security_scheme_name:str|None=None,)->None:"""Initialize an `HTTPBasicAuth` object. Arguments: name: The security scheme name, default to `BasicAuth`. scheme: The authentication scheme used in the `WWW-Authenticate` header. Defaults to `'Basic'`. realm: The realm used in the `WWW-Authenticate` header to indicate a scope of protection, defaults to use `'Authentication Required'`. description: The description of the OpenAPI security scheme. security_scheme_name: The name of the OpenAPI security scheme, default to `BasicAuth`. """BaseHTTPBasicAuth.__init__(self,scheme=scheme,realm=realm)super().__init__(description=description,security_scheme_name=security_scheme_name)self.name=security_scheme_nameorname
classHTTPTokenAuth(_AuthBase,BaseHTTPTokenAuth,SecurityScheme):"""Flask-HTTPAuth's HTTPTokenAuth with some modifications. - Add an authentication error handler that returns JSON response. - Expose the `auth.current_user` as a property. - Add a `description` attribute for OpenAPI Spec. - Add a `name` attribute for OpenAPI Spec. - Add the `get_security_scheme` method for OpenAPI Spec. Examples: ```python from apiflask import APIFlask, HTTPTokenAuth app = APIFlask(__name__) auth = HTTPTokenAuth() ``` """def__init__(self,scheme:str='Bearer',name:str='BearerAuth',realm:str|None=None,header:str|None=None,description:str|None=None,security_scheme_name:str|None=None,)->None:"""Initialize a `HTTPTokenAuth` object. Arguments: scheme: The authentication scheme used in the `WWW-Authenticate` header. One of `'Bearer'` and `'ApiKey'`, defaults to `'Bearer'`. name: The security scheme name, default to `BearerAuth`. realm: The realm used in the `WWW-Authenticate` header to indicate a scope of protection, defaults to use `'Authentication Required'`. header: The custom header where to obtain the token (instead of from `Authorization` header). If a custom header is used, the scheme should not be included. Example: ``` X-API-Key: this-is-my-token ``` description: The description of the OpenAPI security scheme. security_scheme_name: The name of the OpenAPI security scheme, defaults to `BearerAuth` or `ApiKeyAuth`. *Version changed: 3.0.0* - Add parameter `name`. *Version changed: 1.3.0* - Add `security_scheme_name` parameter. """BaseHTTPTokenAuth.__init__(self,scheme=scheme,realm=realm,header=header)super().__init__(description=description,security_scheme_name=security_scheme_name)ifnotself.scheme.lower()=='bearer'orself.headerisnotNone:name='ApiKeyAuth'warnings.warn('The API key authorization by HTTPTokenAuth is deprecated and will be removed in ''APIFlask 4.0.0. Use ''APIKeyHeaderAuth, APIKeyCookieAuth, or APIKeyQueryAuth instead.',DeprecationWarning,stacklevel=2,)self.name=security_scheme_nameornamedefget_security_scheme(self)->dict[str,t.Any]:ifnotself.scheme.lower()=='bearer'orself.headerisnotNone:security_scheme={'type':'apiKey','name':self.header,'in':'header',}else:security_scheme={'type':'http','scheme':'bearer',}ifself.descriptionisnotNone:security_scheme['description']=self.descriptionreturnsecurity_scheme
The authentication scheme used in the WWW-Authenticate
header. One of 'Bearer' and 'ApiKey', defaults to 'Bearer'.
'Bearer'
name
str
The security scheme name, default to BearerAuth.
'BearerAuth'
realm
str | None
The realm used in the WWW-Authenticate header to indicate
a scope of protection, defaults to use 'Authentication Required'.
None
header
str | None
The custom header where to obtain the token (instead
of from Authorization header). If a custom header is used,
the scheme should not be included. Example:
X-API-Key: this-is-my-token
None
description
str | None
The description of the OpenAPI security scheme.
None
security_scheme_name
str | None
The name of the OpenAPI security scheme,
defaults to BearerAuth or ApiKeyAuth.
def__init__(self,scheme:str='Bearer',name:str='BearerAuth',realm:str|None=None,header:str|None=None,description:str|None=None,security_scheme_name:str|None=None,)->None:"""Initialize a `HTTPTokenAuth` object. Arguments: scheme: The authentication scheme used in the `WWW-Authenticate` header. One of `'Bearer'` and `'ApiKey'`, defaults to `'Bearer'`. name: The security scheme name, default to `BearerAuth`. realm: The realm used in the `WWW-Authenticate` header to indicate a scope of protection, defaults to use `'Authentication Required'`. header: The custom header where to obtain the token (instead of from `Authorization` header). If a custom header is used, the scheme should not be included. Example: ``` X-API-Key: this-is-my-token ``` description: The description of the OpenAPI security scheme. security_scheme_name: The name of the OpenAPI security scheme, defaults to `BearerAuth` or `ApiKeyAuth`. *Version changed: 3.0.0* - Add parameter `name`. *Version changed: 1.3.0* - Add `security_scheme_name` parameter. """BaseHTTPTokenAuth.__init__(self,scheme=scheme,realm=realm,header=header)super().__init__(description=description,security_scheme_name=security_scheme_name)ifnotself.scheme.lower()=='bearer'orself.headerisnotNone:name='ApiKeyAuth'warnings.warn('The API key authorization by HTTPTokenAuth is deprecated and will be removed in ''APIFlask 4.0.0. Use ''APIKeyHeaderAuth, APIKeyCookieAuth, or APIKeyQueryAuth instead.',DeprecationWarning,stacklevel=2,)self.name=security_scheme_nameorname
def__init__(self,main_auth:HTTPAuthType,*additional_auth:tuple[HTTPAuthType]):"""Initialize a `HTTPMultiAuth` object. Arguments: main_auth: The main authentication object. additional_auth: The additional additional objects. """super().__init__(main_auth,*additional_auth)