Base class for all authentication schemes. To implement a custom authentication scheme, subclass `Auth` and override the `.auth_flow()` method. If the authentication scheme does I/O such as disk access or network calls, or uses synchronization primitives such as locks, you sho
| 20 | |
| 21 | |
| 22 | class Auth: |
| 23 | """ |
| 24 | Base class for all authentication schemes. |
| 25 | |
| 26 | To implement a custom authentication scheme, subclass `Auth` and override |
| 27 | the `.auth_flow()` method. |
| 28 | |
| 29 | If the authentication scheme does I/O such as disk access or network calls, or uses |
| 30 | synchronization primitives such as locks, you should override `.sync_auth_flow()` |
| 31 | and/or `.async_auth_flow()` instead of `.auth_flow()` to provide specialized |
| 32 | implementations that will be used by `Client` and `AsyncClient` respectively. |
| 33 | """ |
| 34 | |
| 35 | requires_request_body = False |
| 36 | requires_response_body = False |
| 37 | |
| 38 | def auth_flow(self, request: Request) -> typing.Generator[Request, Response, None]: |
| 39 | """ |
| 40 | Execute the authentication flow. |
| 41 | |
| 42 | To dispatch a request, `yield` it: |
| 43 | |
| 44 | ``` |
| 45 | yield request |
| 46 | ``` |
| 47 | |
| 48 | The client will `.send()` the response back into the flow generator. You can |
| 49 | access it like so: |
| 50 | |
| 51 | ``` |
| 52 | response = yield request |
| 53 | ``` |
| 54 | |
| 55 | A `return` (or reaching the end of the generator) will result in the |
| 56 | client returning the last response obtained from the server. |
| 57 | |
| 58 | You can dispatch as many requests as is necessary. |
| 59 | """ |
| 60 | yield request |
| 61 | |
| 62 | def sync_auth_flow( |
| 63 | self, request: Request |
| 64 | ) -> typing.Generator[Request, Response, None]: |
| 65 | """ |
| 66 | Execute the authentication flow synchronously. |
| 67 | |
| 68 | By default, this defers to `.auth_flow()`. You should override this method |
| 69 | when the authentication scheme does I/O and/or uses concurrency primitives. |
| 70 | """ |
| 71 | if self.requires_request_body: |
| 72 | request.read() |
| 73 | |
| 74 | flow = self.auth_flow(request) |
| 75 | request = next(flow) |
| 76 | |
| 77 | while True: |
| 78 | response = yield request |
| 79 | if self.requires_response_body: |