Initial commit
This commit is contained in:
commit
b8216f6ade
25 changed files with 1539 additions and 0 deletions
src/util
23
src/util/websocket_auth_backend.py
Normal file
23
src/util/websocket_auth_backend.py
Normal file
|
@ -0,0 +1,23 @@
|
|||
import base64
|
||||
import binascii
|
||||
from starlette.authentication import (
|
||||
AuthCredentials, AuthenticationBackend, AuthenticationError, SimpleUser
|
||||
)
|
||||
|
||||
class BasicAuthBackend(AuthenticationBackend):
|
||||
async def authenticate(self, conn):
|
||||
if "Authorization" not in conn.headers:
|
||||
raise AuthenticationError('No Authorization header provided')
|
||||
|
||||
auth = conn.headers["Authorization"]
|
||||
try:
|
||||
scheme, credentials = auth.split()
|
||||
if scheme.lower() != 'basic':
|
||||
raise AuthenticationError('Invalid authorization scheme')
|
||||
decoded = base64.b64decode(credentials).decode("ascii")
|
||||
except (ValueError, UnicodeDecodeError, binascii.Error) as exc:
|
||||
raise AuthenticationError('Invalid basic auth credentials')
|
||||
|
||||
username, _, password = decoded.partition(":")
|
||||
# TODO: You'd want to verify the username and password here.
|
||||
return AuthCredentials(["authenticated"]), SimpleUser(username)
|
Loading…
Add table
Add a link
Reference in a new issue