Skip to content

Commit

Permalink
✨ Add FastAPI dependencies for token operations in route logic (#566)
Browse files Browse the repository at this point in the history
* 🔧 Add methods for setting subject getter and token blocklist callbacks

* ✨ Add FastAPI dependencies for token operations in route logic
  • Loading branch information
yezz123 committed Apr 4, 2024
1 parent 476746b commit 88260e3
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
8 changes: 8 additions & 0 deletions authx/_internal/_callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ def set_callback_token_blocklist(self, callback: TokenCallback) -> None:
"""Set callback for token"""
self.callback_is_token_in_blocklist = callback

def set_subject_getter(self, callback: ModelCallback[T]) -> None:
"""Set the callback to run for subject retrieval and serialization"""
self.set_callback_get_model_instance(callback)

def set_token_blocklist(self, callback: TokenCallback) -> None:
"""Set the callback to run for validation of revoked tokens"""
self.set_callback_token_blocklist(callback)

def _get_current_subject(self, uid: str, **kwargs) -> T:
"""Get current model instance from callback"""
self._check_model_callback_is_set()
Expand Down
50 changes: 49 additions & 1 deletion authx/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import contextlib
from typing import Any, Callable, Coroutine, Dict, Literal, Optional, overload

from fastapi import Request, Response
from fastapi import Depends, Request, Response

from authx._internal._callback import _CallbackHandler
from authx._internal._error import _ErrorHandler
Expand Down Expand Up @@ -501,6 +501,54 @@ def unset_cookies(self, response: Response) -> None:
self.unset_access_cookies(response)
self.unset_refresh_cookies(response)

# Notes:
# The AuthXDeps is a utility class, to enable quick token operations
# within the route logic. It provides methods to avoid addtional code
# in your route that would be outside of the route logic

# Such methods includes setting and unsetting cookies without the need
# to generate a response object beforhand.

@property
def DEPENDENCY(self) -> AuthXDependency:
"""FastAPI Dependency to return an AuthX sub-object within the route context"""
return Depends(self.get_dependency)

@property
def BUNDLE(self) -> AuthXDependency:
"""FastAPI Dependency to return a AuthX sub-object within the route context"""
return self.DEPENDENCY

@property
def FRESH_REQUIRED(self) -> TokenPayload:
"""FastAPI Dependency to enforce valid token availability in request"""
return Depends(self.fresh_token_required)

@property
def ACCESS_REQUIRED(self) -> TokenPayload:
"""FastAPI Dependency to enforce presence of an `access` token in request"""
return Depends(self.access_token_required)

@property
def REFRESH_REQUIRED(self) -> TokenPayload:
"""FastAPI Dependency to enforce presence of a `refresh` token in request"""
return Depends(self.refresh_token_required)

@property
def ACCESS_TOKEN(self) -> RequestToken:
"""FastAPI Dependency to retrieve access token from request"""
return Depends(self.get_token_from_request(type="access"))

@property
def REFRESH_TOKEN(self) -> RequestToken:
"""FastAPI Dependency to retrieve refresh token from request"""
return Depends(self.get_token_from_request(type="refresh"))

@property
def CURRENT_SUBJECT(self) -> T:
"""FastAPI Dependency to retrieve the current subject from request"""
return Depends(self.get_current_subject)

def get_dependency(self, request: Request, response: Response) -> AuthXDependency:
"""FastAPI Dependency to return a AuthX sub-object within the route context
Expand Down

0 comments on commit 88260e3

Please sign in to comment.