before level 3 initial commit
This commit is contained in:
+30
@@ -0,0 +1,30 @@
|
||||
"""Basic is only for creating a session. Every other route accepts only Bearer."""
|
||||
import base64
|
||||
import binascii
|
||||
from typing import Optional, Tuple
|
||||
|
||||
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
||||
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
||||
|
||||
|
||||
def parse_basic(header: str) -> Optional[Tuple[str, str]]:
|
||||
"""username:password. The password may itself contain a colon."""
|
||||
scheme, separator, param = header.partition(' ')
|
||||
if separator != ' ' or scheme.lower() != 'basic' or not param or param != param.strip():
|
||||
return None
|
||||
try:
|
||||
decoded = base64.b64decode(param, validate=True)
|
||||
text = decoded.decode('utf-8')
|
||||
except (binascii.Error, UnicodeDecodeError, ValueError):
|
||||
return None
|
||||
username, separator, password = text.partition(':')
|
||||
if separator != ':':
|
||||
return None
|
||||
return username, password
|
||||
|
||||
|
||||
def parse_bearer(header: str) -> Optional[str]:
|
||||
scheme, separator, token = header.partition(' ')
|
||||
if separator != ' ' or scheme.lower() != 'bearer' or not token or token != token.strip() or ' ' in token:
|
||||
return None
|
||||
return token
|
||||
Reference in New Issue
Block a user