improve cb32 handling

This commit is contained in:
Yusur 2025-07-06 22:52:49 +02:00
parent f0a109983b
commit c7a41a70df
5 changed files with 18 additions and 5 deletions

View file

@ -1,5 +1,13 @@
# Changelog
## 0.4.0
👀
## 0.3.5
- Fixed cb32 handling. Now leading zeros in SIQ's are stripped, and `.from_cb32()` was implemented.
## 0.3.4
- Bug fixes in `.flask_restx` regarding error handling

View file

@ -27,7 +27,7 @@ from .itertools import makelist, kwargs_prefix, ltuple, rtuple, additem
from .i18n import I18n, JsonI18n, TomlI18n
from .snowflake import Snowflake, SnowflakeGen
__version__ = "0.3.4"
__version__ = "0.3.5"
__all__ = (
'Siq', 'SiqCache', 'SiqType', 'SiqGen', 'StringCase',

View file

@ -40,7 +40,7 @@ class FlaskAuthSrc(AuthSrc):
def get_signature(self) -> bytes:
sig = request.headers.get('authorization-signature', None)
return want_bytes(sig) if sig else None
def invalid_exc(self, msg: str = 'validation failed') -> Never:
def invalid_exc(self, msg: str = 'Validation failed') -> Never:
abort(400, msg)
def required_exc(self):
abort(401, 'Login required')

View file

@ -41,7 +41,7 @@ from typing import Iterable, override
import warnings
from .functools import deprecated
from .codecs import b32lencode, b64encode, cb32encode
from .codecs import b32lencode, b64encode, cb32decode, cb32encode, want_str
class SiqType(enum.Enum):
@ -235,9 +235,14 @@ class Siq(int):
def to_base64(self, length: int = 15, *, strip: bool = True) -> str:
return b64encode(self.to_bytes(length), strip=strip)
def to_cb32(self) -> str:
return cb32encode(self.to_bytes(15, 'big'))
return cb32encode(self.to_bytes(15, 'big')).lstrip('0')
to_crockford = to_cb32
@classmethod
def from_cb32(cls, val: str | bytes):
return cls.from_bytes(cb32decode(want_str(val).zfill(24)))
def to_hex(self) -> str:
return f'{self:x}'
def to_oct(self) -> str:

View file

@ -278,7 +278,7 @@ def require_auth_base(cls: type[DeclarativeBase], *, src: AuthSrc, column: str |
raise ValueError(msg)
invalid_exc = src.invalid_exc or _default_invalid
required_exc = src.required_exc or (lambda: _default_invalid())
required_exc = src.required_exc or (lambda: _default_invalid('Login required'))
def decorator(func: Callable):
@wraps(func)