api_key.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. from typing import Optional
  2. from fastapi.openapi.models import APIKey, APIKeyIn
  3. from fastapi.security.base import SecurityBase
  4. from starlette.exceptions import HTTPException
  5. from starlette.requests import Request
  6. from starlette.status import HTTP_403_FORBIDDEN
  7. from typing_extensions import Annotated, Doc
  8. class APIKeyBase(SecurityBase):
  9. @staticmethod
  10. def check_api_key(api_key: Optional[str], auto_error: bool) -> Optional[str]:
  11. if not api_key:
  12. if auto_error:
  13. raise HTTPException(
  14. status_code=HTTP_403_FORBIDDEN, detail="Not authenticated"
  15. )
  16. return None
  17. return api_key
  18. class APIKeyQuery(APIKeyBase):
  19. """
  20. API key authentication using a query parameter.
  21. This defines the name of the query parameter that should be provided in the request
  22. with the API key and integrates that into the OpenAPI documentation. It extracts
  23. the key value sent in the query parameter automatically and provides it as the
  24. dependency result. But it doesn't define how to send that API key to the client.
  25. ## Usage
  26. Create an instance object and use that object as the dependency in `Depends()`.
  27. The dependency result will be a string containing the key value.
  28. ## Example
  29. ```python
  30. from fastapi import Depends, FastAPI
  31. from fastapi.security import APIKeyQuery
  32. app = FastAPI()
  33. query_scheme = APIKeyQuery(name="api_key")
  34. @app.get("/items/")
  35. async def read_items(api_key: str = Depends(query_scheme)):
  36. return {"api_key": api_key}
  37. ```
  38. """
  39. def __init__(
  40. self,
  41. *,
  42. name: Annotated[
  43. str,
  44. Doc("Query parameter name."),
  45. ],
  46. scheme_name: Annotated[
  47. Optional[str],
  48. Doc(
  49. """
  50. Security scheme name.
  51. It will be included in the generated OpenAPI (e.g. visible at `/docs`).
  52. """
  53. ),
  54. ] = None,
  55. description: Annotated[
  56. Optional[str],
  57. Doc(
  58. """
  59. Security scheme description.
  60. It will be included in the generated OpenAPI (e.g. visible at `/docs`).
  61. """
  62. ),
  63. ] = None,
  64. auto_error: Annotated[
  65. bool,
  66. Doc(
  67. """
  68. By default, if the query parameter is not provided, `APIKeyQuery` will
  69. automatically cancel the request and send the client an error.
  70. If `auto_error` is set to `False`, when the query parameter is not
  71. available, instead of erroring out, the dependency result will be
  72. `None`.
  73. This is useful when you want to have optional authentication.
  74. It is also useful when you want to have authentication that can be
  75. provided in one of multiple optional ways (for example, in a query
  76. parameter or in an HTTP Bearer token).
  77. """
  78. ),
  79. ] = True,
  80. ):
  81. self.model: APIKey = APIKey(
  82. **{"in": APIKeyIn.query}, # type: ignore[arg-type]
  83. name=name,
  84. description=description,
  85. )
  86. self.scheme_name = scheme_name or self.__class__.__name__
  87. self.auto_error = auto_error
  88. async def __call__(self, request: Request) -> Optional[str]:
  89. api_key = request.query_params.get(self.model.name)
  90. return self.check_api_key(api_key, self.auto_error)
  91. class APIKeyHeader(APIKeyBase):
  92. """
  93. API key authentication using a header.
  94. This defines the name of the header that should be provided in the request with
  95. the API key and integrates that into the OpenAPI documentation. It extracts
  96. the key value sent in the header automatically and provides it as the dependency
  97. result. But it doesn't define how to send that key to the client.
  98. ## Usage
  99. Create an instance object and use that object as the dependency in `Depends()`.
  100. The dependency result will be a string containing the key value.
  101. ## Example
  102. ```python
  103. from fastapi import Depends, FastAPI
  104. from fastapi.security import APIKeyHeader
  105. app = FastAPI()
  106. header_scheme = APIKeyHeader(name="x-key")
  107. @app.get("/items/")
  108. async def read_items(key: str = Depends(header_scheme)):
  109. return {"key": key}
  110. ```
  111. """
  112. def __init__(
  113. self,
  114. *,
  115. name: Annotated[str, Doc("Header name.")],
  116. scheme_name: Annotated[
  117. Optional[str],
  118. Doc(
  119. """
  120. Security scheme name.
  121. It will be included in the generated OpenAPI (e.g. visible at `/docs`).
  122. """
  123. ),
  124. ] = None,
  125. description: Annotated[
  126. Optional[str],
  127. Doc(
  128. """
  129. Security scheme description.
  130. It will be included in the generated OpenAPI (e.g. visible at `/docs`).
  131. """
  132. ),
  133. ] = None,
  134. auto_error: Annotated[
  135. bool,
  136. Doc(
  137. """
  138. By default, if the header is not provided, `APIKeyHeader` will
  139. automatically cancel the request and send the client an error.
  140. If `auto_error` is set to `False`, when the header is not available,
  141. instead of erroring out, the dependency result will be `None`.
  142. This is useful when you want to have optional authentication.
  143. It is also useful when you want to have authentication that can be
  144. provided in one of multiple optional ways (for example, in a header or
  145. in an HTTP Bearer token).
  146. """
  147. ),
  148. ] = True,
  149. ):
  150. self.model: APIKey = APIKey(
  151. **{"in": APIKeyIn.header}, # type: ignore[arg-type]
  152. name=name,
  153. description=description,
  154. )
  155. self.scheme_name = scheme_name or self.__class__.__name__
  156. self.auto_error = auto_error
  157. async def __call__(self, request: Request) -> Optional[str]:
  158. api_key = request.headers.get(self.model.name)
  159. return self.check_api_key(api_key, self.auto_error)
  160. class APIKeyCookie(APIKeyBase):
  161. """
  162. API key authentication using a cookie.
  163. This defines the name of the cookie that should be provided in the request with
  164. the API key and integrates that into the OpenAPI documentation. It extracts
  165. the key value sent in the cookie automatically and provides it as the dependency
  166. result. But it doesn't define how to set that cookie.
  167. ## Usage
  168. Create an instance object and use that object as the dependency in `Depends()`.
  169. The dependency result will be a string containing the key value.
  170. ## Example
  171. ```python
  172. from fastapi import Depends, FastAPI
  173. from fastapi.security import APIKeyCookie
  174. app = FastAPI()
  175. cookie_scheme = APIKeyCookie(name="session")
  176. @app.get("/items/")
  177. async def read_items(session: str = Depends(cookie_scheme)):
  178. return {"session": session}
  179. ```
  180. """
  181. def __init__(
  182. self,
  183. *,
  184. name: Annotated[str, Doc("Cookie name.")],
  185. scheme_name: Annotated[
  186. Optional[str],
  187. Doc(
  188. """
  189. Security scheme name.
  190. It will be included in the generated OpenAPI (e.g. visible at `/docs`).
  191. """
  192. ),
  193. ] = None,
  194. description: Annotated[
  195. Optional[str],
  196. Doc(
  197. """
  198. Security scheme description.
  199. It will be included in the generated OpenAPI (e.g. visible at `/docs`).
  200. """
  201. ),
  202. ] = None,
  203. auto_error: Annotated[
  204. bool,
  205. Doc(
  206. """
  207. By default, if the cookie is not provided, `APIKeyCookie` will
  208. automatically cancel the request and send the client an error.
  209. If `auto_error` is set to `False`, when the cookie is not available,
  210. instead of erroring out, the dependency result will be `None`.
  211. This is useful when you want to have optional authentication.
  212. It is also useful when you want to have authentication that can be
  213. provided in one of multiple optional ways (for example, in a cookie or
  214. in an HTTP Bearer token).
  215. """
  216. ),
  217. ] = True,
  218. ):
  219. self.model: APIKey = APIKey(
  220. **{"in": APIKeyIn.cookie}, # type: ignore[arg-type]
  221. name=name,
  222. description=description,
  223. )
  224. self.scheme_name = scheme_name or self.__class__.__name__
  225. self.auto_error = auto_error
  226. async def __call__(self, request: Request) -> Optional[str]:
  227. api_key = request.cookies.get(self.model.name)
  228. return self.check_api_key(api_key, self.auto_error)