add token loader, .flask_sqlalchemy, jsonencode(), base2048
This commit is contained in:
parent
dfa4309216
commit
9f75d983ba
11 changed files with 462 additions and 42 deletions
|
|
@ -14,15 +14,17 @@ This software is distributed on an "AS IS" BASIS,
|
|||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
"""
|
||||
|
||||
from abc import ABC
|
||||
from base64 import b64decode
|
||||
from typing import Any, Callable
|
||||
from itsdangerous import TimestampSigner
|
||||
|
||||
from suou.iding import Siq
|
||||
from .codecs import want_str
|
||||
from .iding import Siq
|
||||
|
||||
class UserSigner(TimestampSigner):
|
||||
"""
|
||||
Instance itsdangerous.TimestampSigner() from a user ID.
|
||||
|
||||
XXX UNTESTED!!!
|
||||
itsdangerous.TimestampSigner() instanced from a user ID, with token generation and validation capabilities.
|
||||
"""
|
||||
user_id: int
|
||||
def __init__(self, master_secret: bytes, user_id: int, user_secret: bytes, **kwargs):
|
||||
|
|
@ -30,4 +32,21 @@ class UserSigner(TimestampSigner):
|
|||
self.user_id = user_id
|
||||
def token(self) -> str:
|
||||
return self.sign(Siq(self.user_id).to_base64()).decode('ascii')
|
||||
@classmethod
|
||||
def split_token(cls, /, token: str | bytes) :
|
||||
a, b, c = want_str(token).rsplit('.', 2)
|
||||
return b64decode(a), b, b64decode(c)
|
||||
|
||||
|
||||
class HasSigner(ABC):
|
||||
'''
|
||||
Abstract base class for INTERNAL USE.
|
||||
'''
|
||||
signer: Callable[Any, UserSigner]
|
||||
|
||||
@classmethod
|
||||
def __instancehook__(cls, obj) -> bool:
|
||||
return callable(getattr(obj, 'signer', None))
|
||||
|
||||
|
||||
__all__ = ('UserSigner', )
|
||||
Loading…
Add table
Add a link
Reference in a new issue