An example implementation of authentication via handshake.
| 573 | |
| 574 | |
| 575 | class TokenServerAuthHandler(ServerAuthHandler): |
| 576 | """An example implementation of authentication via handshake.""" |
| 577 | |
| 578 | def __init__(self, creds): |
| 579 | super().__init__() |
| 580 | self.creds = creds |
| 581 | |
| 582 | def authenticate(self, outgoing, incoming): |
| 583 | username = incoming.read() |
| 584 | password = incoming.read() |
| 585 | if username in self.creds and self.creds[username] == password: |
| 586 | outgoing.write(base64.b64encode(b'secret:' + username)) |
| 587 | else: |
| 588 | raise flight.FlightUnauthenticatedError( |
| 589 | "invalid username/password") |
| 590 | |
| 591 | def is_valid(self, token): |
| 592 | token = base64.b64decode(token) |
| 593 | if not token.startswith(b'secret:'): |
| 594 | raise flight.FlightUnauthenticatedError("invalid token") |
| 595 | return token[7:] |
| 596 | |
| 597 | |
| 598 | class TokenClientAuthHandler(ClientAuthHandler): |