forked from sanic-org/sanic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add credentials property to Request objects (sanic-org#2357)
- Loading branch information
1 parent
8f18389
commit f1a695f
Showing
5 changed files
with
171 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import annotations | ||
|
||
from base64 import b64decode | ||
from dataclasses import dataclass, field | ||
from typing import Optional | ||
|
||
|
||
@dataclass() | ||
class Credentials: | ||
auth_type: Optional[str] | ||
token: Optional[str] | ||
_username: Optional[str] = field(default=None) | ||
_password: Optional[str] = field(default=None) | ||
|
||
def __post_init__(self): | ||
if self._auth_is_basic: | ||
self._username, self._password = ( | ||
b64decode(self.token.encode("utf-8")).decode().split(":") | ||
) | ||
|
||
@property | ||
def username(self): | ||
if not self._auth_is_basic: | ||
raise AttributeError("Username is available for Basic Auth only") | ||
return self._username | ||
|
||
@property | ||
def password(self): | ||
if not self._auth_is_basic: | ||
raise AttributeError("Password is available for Basic Auth only") | ||
return self._password | ||
|
||
@property | ||
def _auth_is_basic(self) -> bool: | ||
return self.auth_type == "Basic" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters