-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add extraction for access token (#31)
- Loading branch information
Showing
24 changed files
with
893 additions
and
253 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[flake8] | ||
max-line-length = 88 | ||
select = C,E,F,W,B,B9 | ||
ignore = E203, E501, W503 | ||
exclude = __init__.py |
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,44 @@ | ||
import os | ||
from pydantic import BaseModel | ||
from fastapi import FastAPI, Depends | ||
from fastapi_cloudauth.auth0 import Auth0, Auth0CurrentUser, Auth0Claims | ||
|
||
tags_metadata = [ | ||
{ | ||
"name": "Auth0", | ||
"description": "Operations with access/ID token, provided by Auth0.", | ||
} | ||
] | ||
|
||
app = FastAPI( | ||
title="FastAPI CloudAuth Project", | ||
description="Simple integration between FastAPI and cloud authentication services (AWS Cognito, Auth0, Firebase Authentication).", | ||
openapi_tags=tags_metadata, | ||
) | ||
|
||
auth = Auth0(domain=os.environ["AUTH0_DOMAIN"]) | ||
|
||
|
||
@app.get("/", dependencies=[Depends(auth.scope("read:users"))], tags=["Auth0"]) | ||
def secure(): | ||
# access token is valid | ||
return "Hello" | ||
|
||
|
||
class AccessUser(BaseModel): | ||
sub: str | ||
|
||
|
||
@app.get("/access/", tags=["Auth0"]) | ||
def secure_access(current_user: AccessUser = Depends(auth.claim(AccessUser))): | ||
# access token is valid and getting user info from access token | ||
return f"Hello", {current_user.sub} | ||
|
||
|
||
get_current_user = Auth0CurrentUser(domain=os.environ["AUTH0_DOMAIN"]) | ||
|
||
|
||
@app.get("/user/", tags=["Auth0"]) | ||
def secure_user(current_user: Auth0Claims = Depends(get_current_user)): | ||
# ID token is valid and getting user info from ID token | ||
return f"Hello, {current_user.username}" |
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,48 @@ | ||
import os | ||
from pydantic import BaseModel | ||
from fastapi import FastAPI, Depends | ||
from fastapi_cloudauth.cognito import Cognito, CognitoCurrentUser, CognitoClaims | ||
|
||
tags_metadata = [ | ||
{ | ||
"name": "Cognito", | ||
"description": "Operations with access/ID token, provided by AWS Cognito.", | ||
} | ||
] | ||
|
||
app = FastAPI( | ||
title="FastAPI CloudAuth Project", | ||
description="Simple integration between FastAPI and cloud authentication services (AWS Cognito, Auth0, Firebase Authentication).", | ||
openapi_tags=tags_metadata, | ||
) | ||
|
||
auth = Cognito( | ||
region=os.environ["COGNITO_REGION"], userPoolId=os.environ["COGNITO_USERPOOLID"] | ||
) | ||
|
||
|
||
@app.get("/", dependencies=[Depends(auth.scope("read:users"))], tags=["Cognito"]) | ||
def secure(): | ||
# access token is valid | ||
return "Hello" | ||
|
||
|
||
class AccessUser(BaseModel): | ||
sub: str | ||
|
||
|
||
@app.get("/access/", tags=["Cognito"]) | ||
def secure_access(current_user: AccessUser = Depends(auth.claim(AccessUser))): | ||
# access token is valid and getting user info from access token | ||
return f"Hello", {current_user.sub} | ||
|
||
|
||
get_current_user = CognitoCurrentUser( | ||
region=os.environ["COGNITO_REGION"], userPoolId=os.environ["COGNITO_USERPOOLID"] | ||
) | ||
|
||
|
||
@app.get("/user/", tags=["Cognito"]) | ||
def secure_user(current_user: CognitoClaims = Depends(get_current_user)): | ||
# ID token is valid and getting user info from ID token | ||
return f"Hello, {current_user.username}" |
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,23 @@ | ||
from fastapi import FastAPI, Depends | ||
from fastapi_cloudauth.firebase import FirebaseCurrentUser, FirebaseClaims | ||
|
||
tags_metadata = [ | ||
{ | ||
"name": "Firebase", | ||
"description": "Operations with access/ID token, provided by Firebase Authentication.", | ||
} | ||
] | ||
|
||
app = FastAPI( | ||
title="FastAPI CloudAuth Project", | ||
description="Simple integration between FastAPI and cloud authentication services (AWS Cognito, Auth0, Firebase Authentication).", | ||
openapi_tags=tags_metadata, | ||
) | ||
|
||
get_current_user = FirebaseCurrentUser() | ||
|
||
|
||
@app.get("/user/", tags=["Firebase"]) | ||
def secure_user(current_user: FirebaseClaims = Depends(get_current_user)): | ||
# ID token is valid and getting user info from ID token | ||
return f"Hello, {current_user.user_id}" |
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,33 +1,46 @@ | ||
from typing import Any, Optional | ||
|
||
from pydantic import BaseModel, Field | ||
from .base import TokenVerifier, TokenUserInfoGetter, JWKS | ||
|
||
from .base import ScopedAuth, UserInfoAuth | ||
from .verification import JWKS | ||
|
||
|
||
class Auth0(TokenVerifier): | ||
class Auth0(ScopedAuth): | ||
""" | ||
Verify access token of auth0 | ||
""" | ||
|
||
scope_key = "permissions" | ||
user_info = None | ||
|
||
def __init__(self, domain: str, *args, **kwargs): | ||
def __init__( | ||
self, | ||
domain: str, | ||
scope_key: Optional[str] = "permissions", | ||
auto_error: bool = True, | ||
): | ||
url = f"https://{domain}/.well-known/jwks.json" | ||
jwks = JWKS.fromurl(url) | ||
super().__init__(jwks, *args, **kwargs) | ||
super().__init__( | ||
jwks, scope_key=scope_key, auto_error=auto_error, | ||
) | ||
|
||
|
||
class Auth0Claims(BaseModel): | ||
username: str = Field(alias="name") | ||
email: str = Field(None, alias="email") | ||
|
||
|
||
class Auth0CurrentUser(TokenUserInfoGetter): | ||
class Auth0CurrentUser(UserInfoAuth): | ||
""" | ||
Verify ID token and get user info of Auth0 | ||
""" | ||
|
||
user_info = Auth0Claims | ||
|
||
def __init__(self, domain: str, *args, **kwargs): | ||
def __init__( | ||
self, domain: str, *args: Any, **kwargs: Any, | ||
): | ||
url = f"https://{domain}/.well-known/jwks.json" | ||
jwks = JWKS.fromurl(url) | ||
super().__init__(jwks, *args, **kwargs) | ||
super().__init__(jwks, *args, user_info=self.user_info, **kwargs) |
Oops, something went wrong.