-
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.
* Fix image uploading to S3 * image refactoring stuff * fix test * fix more mock
1 parent
dbaf6fd
commit b89547d
Showing
16 changed files
with
164 additions
and
139 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
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
Empty file.
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,100 @@ | ||
"""Defines the API endpoint for authenticating with Github.""" | ||
|
||
import logging | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Depends, Response | ||
from httpx import AsyncClient, Response as HttpxResponse | ||
from pydantic.main import BaseModel | ||
|
||
from store.app.db import Crud | ||
from store.app.model import UserPermissions | ||
from store.settings import settings | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
github_auth_router = APIRouter() | ||
|
||
|
||
@github_auth_router.get("/login") | ||
async def github_login() -> str: | ||
"""Gives the user a redirect url to login with github. | ||
Returns: | ||
Github oauth redirect url. | ||
""" | ||
return f"https://github.com/login/oauth/authorize?scope=user:email&client_id={settings.oauth.github_client_id}" | ||
|
||
|
||
async def github_access_token_req(params: dict[str, str], headers: dict[str, str]) -> HttpxResponse: | ||
async with AsyncClient() as client: | ||
return await client.post( | ||
url="https://github.com/login/oauth/access_token", | ||
params=params, | ||
headers=headers, | ||
) | ||
|
||
|
||
async def github_req(headers: dict[str, str]) -> HttpxResponse: | ||
async with AsyncClient() as client: | ||
return await client.get("https://api.github.com/user", headers=headers) | ||
|
||
|
||
async def github_email_req(headers: dict[str, str]) -> HttpxResponse: | ||
async with AsyncClient() as client: | ||
return await client.get("https://api.github.com/user/emails", headers=headers) | ||
|
||
|
||
class UserInfoResponse(BaseModel): | ||
id: str | ||
permissions: UserPermissions | ||
|
||
|
||
@github_auth_router.get("/code/{code}", response_model=UserInfoResponse) | ||
async def github_code( | ||
code: str, | ||
crud: Annotated[Crud, Depends(Crud.get)], | ||
response: Response, | ||
) -> UserInfoResponse: | ||
"""Gives the user a session token upon successful github authentication and creation of user. | ||
Args: | ||
code: Github code returned from the successful authentication. | ||
crud: The CRUD object. | ||
response: The response object. | ||
Returns: | ||
UserInfoResponse. | ||
""" | ||
params = { | ||
"client_id": settings.oauth.github_client_id, | ||
"client_secret": settings.oauth.github_client_secret, | ||
"code": code, | ||
} | ||
headers = {"Accept": "application/json"} | ||
oauth_response = await github_access_token_req(params, headers) | ||
response_json = oauth_response.json() | ||
|
||
# access token is used to retrieve user oauth details | ||
access_token = response_json["access_token"] | ||
headers.update({"Authorization": f"Bearer {access_token}"}) | ||
oauth_response = await github_req(headers) | ||
oauth_email_response = await github_email_req(headers) | ||
|
||
github_id = oauth_response.json()["html_url"] | ||
email = next(entry["email"] for entry in oauth_email_response.json() if entry["primary"]) | ||
|
||
user = await crud.get_user_from_github_token(github_id) | ||
|
||
# We create a new user if the user does not exist yet. | ||
if user is None: | ||
user = await crud.create_user_from_github_token( | ||
email=email, | ||
github_id=github_id, | ||
) | ||
|
||
api_key = await crud.add_api_key(user.id) | ||
|
||
response.set_cookie(key="session_token", value=api_key.id, httponly=True, samesite="lax") | ||
|
||
return UserInfoResponse(id=user.id, permissions=user.permissions) |
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
crypto: | ||
jwt_secret: fakeJwtSecret | ||
s3: | ||
bucket: images | ||
prefix: "" | ||
site: | ||
homepage: http://127.0.0.1:3000 | ||
image_url: http://127.0.0.1:4566/images | ||
image_base_url: http://127.0.0.1:4566/images |
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
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