-
-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Authorized Custom Feed (user-specific results) (#10)
- Loading branch information
Showing
3 changed files
with
51 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
atproto==0.0.35 | ||
atproto==0.0.37 | ||
peewee~=3.16.2 | ||
Flask~=2.3.2 | ||
python-dotenv~=1.0.0 |
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,41 @@ | ||
from atproto import DidInMemoryCache, IdResolver, verify_jwt | ||
from atproto.exceptions import TokenInvalidSignatureError | ||
from flask import Request | ||
|
||
|
||
_CACHE = DidInMemoryCache() | ||
_ID_RESOLVER = IdResolver(cache=_CACHE) | ||
|
||
_AUTHORIZATION_HEADER_NAME = 'Authorization' | ||
_AUTHORIZATION_HEADER_VALUE_PREFIX = 'Bearer ' | ||
|
||
|
||
class AuthorizationError(Exception): | ||
... | ||
|
||
|
||
def validate_auth(request: 'Request') -> str: | ||
"""Validate authorization header. | ||
Args: | ||
request: The request to validate. | ||
Returns: | ||
:obj:`str`: Requester DID. | ||
Raises: | ||
:obj:`AuthorizationError`: If the authorization header is invalid. | ||
""" | ||
auth_header = request.headers.get(_AUTHORIZATION_HEADER_NAME) | ||
if not auth_header: | ||
raise AuthorizationError('Authorization header is missing') | ||
|
||
if not auth_header.startswith(_AUTHORIZATION_HEADER_VALUE_PREFIX): | ||
raise AuthorizationError('Invalid authorization header') | ||
|
||
jwt = auth_header[len(_AUTHORIZATION_HEADER_VALUE_PREFIX) :].strip() | ||
|
||
try: | ||
return verify_jwt(jwt, _ID_RESOLVER.did.resolve_atproto_key).iss | ||
except TokenInvalidSignatureError as e: | ||
raise AuthorizationError('Invalid signature') from e |