-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Clean some internals of the Pluggable * Expose Controller * Add requires for generic types * Add tests for requires with Security * Add docs about Requires and Security
- Loading branch information
Showing
24 changed files
with
1,029 additions
and
35 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
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,53 @@ | ||
from esmerald import Request, Security, HTTPException, get, Inject, Injects, Esmerald, Gateway | ||
from lilya import status | ||
from typing import cast, Any | ||
from pydantic import BaseModel | ||
|
||
|
||
class MyCustomSecurity: | ||
def __init__(self, name: str, **kwargs: Any) -> None: | ||
self.name = name | ||
self.__auto_error__ = kwargs.pop("auto_error", True) | ||
|
||
async def __call__(self, request: Request) -> dict[str, None]: | ||
api_key = request.query_params.get(self.name, None) | ||
if api_key: | ||
return cast(str, api_key) | ||
|
||
if self.__auto_error__: | ||
raise HTTPException( | ||
status_code=status.HTTP_403_FORBIDDEN, | ||
detail="Not authenticated", | ||
) | ||
return None | ||
|
||
|
||
# Instantiate the custom security scheme | ||
api_key = MyCustomSecurity(name="key") | ||
|
||
# Use the custom security scheme | ||
security = Security(api_key) | ||
|
||
|
||
class User(BaseModel): | ||
username: str | ||
|
||
|
||
def get_current_user(oauth_header: str = Security(api_key)): | ||
user = User(username=oauth_header) | ||
return user | ||
|
||
|
||
@get( | ||
"/users/me", | ||
security=[api_key], | ||
dependencies={"current_user": Inject(get_current_user)}, | ||
) | ||
def read_current_user(current_user: User = Injects()) -> Any: | ||
return current_user | ||
|
||
|
||
# Start the application | ||
app = Esmerald( | ||
routes=[Gateway(handler=read_current_user)], | ||
) |
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,26 @@ | ||
from typing import Any | ||
|
||
from esmerald import Gateway, Inject, Injects, JSONResponse, Requires, get, Esmerald | ||
|
||
|
||
async def get_user(): | ||
return {"id": 1, "name": "Alice"} | ||
|
||
|
||
async def get_current_user(user: Any = Requires(get_user)): | ||
return user | ||
|
||
|
||
@get( | ||
"/items", | ||
dependencies={"current_user": Inject(get_current_user)}, | ||
) | ||
async def get_items(current_user: Any = Injects()) -> JSONResponse: | ||
return JSONResponse({"message": "Hello", "user": current_user}) | ||
|
||
|
||
app = Esmerald( | ||
routes=[ | ||
Gateway(handler=get_items), | ||
] | ||
) |
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,26 @@ | ||
from typing import Dict, Any | ||
from esmerald import Gateway, Requires, get, Esmerald | ||
|
||
|
||
async def query_params(q: str | None = None, skip: int = 0, limit: int = 20): | ||
return {"q": q, "skip": skip, "limit": limit} | ||
|
||
|
||
async def get_user() -> Dict[str, Any]: | ||
return {"username": "admin"} | ||
|
||
|
||
async def get_user( | ||
user: Dict[str, Any] = Requires(get_user), params: Dict[str, Any] = Requires(query_params) | ||
): | ||
return {"user": user, "params": params} | ||
|
||
|
||
@get("/info") | ||
async def get_info(info: Dict[str, Any] = Requires(get_user)) -> Any: | ||
return info | ||
|
||
|
||
app = Esmerald( | ||
routes=[Gateway(handler=get_info)], | ||
) |
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,30 @@ | ||
from typing import Any | ||
|
||
from lilya.middleware import DefineMiddleware | ||
from lilya.middleware.request_context import RequestContextMiddleware | ||
from pydantic import BaseModel | ||
|
||
from esmerald import Gateway, Requires, Security, get, Esmerald | ||
from esmerald.security.api_key import APIKeyInCookie | ||
|
||
api_key = APIKeyInCookie(name="key") | ||
|
||
|
||
class User(BaseModel): | ||
username: str | ||
|
||
|
||
def get_current_user(oauth_header: str = Security(api_key)): | ||
user = User(username=oauth_header) | ||
return user | ||
|
||
|
||
@get("/users/me", security=[api_key]) | ||
def read_current_user(current_user: User = Requires(get_current_user)) -> Any: | ||
return current_user | ||
|
||
|
||
app = Esmerald( | ||
routes=[Gateway(handler=read_current_user)], | ||
middleware=[DefineMiddleware(RequestContextMiddleware)], | ||
) |
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,38 @@ | ||
from typing import Any | ||
|
||
from lilya.middleware import DefineMiddleware | ||
from lilya.middleware.request_context import RequestContextMiddleware | ||
from pydantic import BaseModel | ||
|
||
from esmerald import Gateway, Requires, Security, get, Esmerald, Inject, Injects | ||
from esmerald.security.api_key import APIKeyInCookie | ||
|
||
api_key = APIKeyInCookie(name="key") | ||
|
||
|
||
class User(BaseModel): | ||
username: str | ||
|
||
|
||
def get_current_user(oauth_header: str = Security(api_key)): | ||
user = User(username=oauth_header) | ||
return user | ||
|
||
|
||
def get_user(user: User = Requires(get_current_user)) -> User: | ||
return user | ||
|
||
|
||
@get( | ||
"/users/me", | ||
security=[api_key], | ||
dependencies={"current_user": Inject(get_user)}, | ||
) | ||
def read_current_user(current_user: User = Injects()) -> Any: | ||
return current_user | ||
|
||
|
||
app = Esmerald( | ||
routes=[Gateway(handler=read_current_user)], | ||
middleware=[DefineMiddleware(RequestContextMiddleware)], | ||
) |
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,17 @@ | ||
from typing import Any, Dict | ||
|
||
from esmerald import Gateway, Requires, get, Esmerald | ||
|
||
|
||
async def query_params(q: str | None = None, skip: int = 0, limit: int = 20): | ||
return {"q": q, "skip": skip, "limit": limit} | ||
|
||
|
||
@get("/items") | ||
async def get_params(params: Dict[str, Any] = Requires(query_params)) -> Any: | ||
return params | ||
|
||
|
||
app = Esmerald( | ||
routes=[Gateway(handler=get_params)], | ||
) |
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
Oops, something went wrong.