An example implementation of HTTP basic authentication.
| 532 | |
| 533 | |
| 534 | class HttpBasicServerAuthHandler(ServerAuthHandler): |
| 535 | """An example implementation of HTTP basic authentication.""" |
| 536 | |
| 537 | def __init__(self, creds): |
| 538 | super().__init__() |
| 539 | self.creds = creds |
| 540 | |
| 541 | def authenticate(self, outgoing, incoming): |
| 542 | buf = incoming.read() |
| 543 | auth = flight.BasicAuth.deserialize(buf) |
| 544 | if auth.username not in self.creds: |
| 545 | raise flight.FlightUnauthenticatedError("unknown user") |
| 546 | if self.creds[auth.username] != auth.password: |
| 547 | raise flight.FlightUnauthenticatedError("wrong password") |
| 548 | outgoing.write(tobytes(auth.username)) |
| 549 | |
| 550 | def is_valid(self, token): |
| 551 | if not token: |
| 552 | raise flight.FlightUnauthenticatedError("token not provided") |
| 553 | if token not in self.creds: |
| 554 | raise flight.FlightUnauthenticatedError("unknown user") |
| 555 | return token |
| 556 | |
| 557 | |
| 558 | class HttpBasicClientAuthHandler(ClientAuthHandler): |