-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* made user login and new user forms * Update new user/login form with UX tweaks * Move account items to user menu in navbar * Whoop typo * Fix form popup animation * Organize form imports * adding db backend routing for creating users * Refactor stuff, add migration for users table * Whoops update get_user_info to use pennkey * Remove unnecessary package-lock files * Remove salt from DB (it's stored in hash) * Add OAuth2 + JWT authentication to backend * Add frontend main process auth logic * Get frontend pipeline working * Update backend to record user pennkey on asset POST * Add auth token to frontend file download call --------- Co-authored-by: Debby Lin <[email protected]>
- Loading branch information
1 parent
e3bae0c
commit 86ccc92
Showing
30 changed files
with
1,118 additions
and
312 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 |
---|---|---|
|
@@ -2,3 +2,4 @@ DATABASE_URL="postgresql://postgres:[email protected]:5432/postgres" | |
AWS_ENDPOINT_URL="http://localhost:4566" | ||
AWS_ACCESS_KEY_ID="test_AWS_ACCESS_KEY_ID" | ||
AWS_SECRET_ACCESS_KEY="test_AWS_SECRET_ACCESS_KEY" | ||
SECRET_KEY="4fc57ea9cd4d54e9e43b534a3b88722ba32cf4f0a2e1b76a2069cde25ff4203f" |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
37 changes: 37 additions & 0 deletions
37
backend/migrations/versions/33e6aa9a48d7_add_user_table.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"""Add user table | ||
Revision ID: 33e6aa9a48d7 | ||
Revises: 4b80b9859b79 | ||
Create Date: 2024-04-23 01:57:35.665974 | ||
""" | ||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = '33e6aa9a48d7' | ||
down_revision: Union[str, None] = '4b80b9859b79' | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table('users', | ||
sa.Column('pennkey', sa.String(), nullable=False), | ||
sa.Column('hashed_password', sa.LargeBinary(), nullable=False), | ||
sa.Column('first_name', sa.String(), nullable=False), | ||
sa.Column('last_name', sa.String(), nullable=False), | ||
sa.Column('school', sa.Enum('sas', 'seas', 'wharton', name='school', create_constraint=True), nullable=False), | ||
sa.PrimaryKeyConstraint('pennkey') | ||
) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade() -> None: | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_table('users') | ||
# ### end Alembic commands ### |
This file was deleted.
Oops, something went wrong.
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,9 +1,11 @@ | ||
from fastapi import APIRouter | ||
|
||
from .assets import router as assets_router | ||
from .users import router as users_router | ||
|
||
router = APIRouter( | ||
prefix="/api/v1", | ||
) | ||
|
||
router.include_router(assets_router) | ||
router.include_router(users_router) |
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,108 @@ | ||
from datetime import timedelta | ||
from typing import Annotated, Sequence | ||
from fastapi import ( | ||
APIRouter, | ||
Depends, | ||
HTTPException, | ||
) | ||
from fastapi.security import OAuth2PasswordRequestForm | ||
from sqlalchemy.orm import Session | ||
|
||
from util.auth import create_access_token, get_current_user | ||
from util.crud.users import ( | ||
authenticate_user, | ||
create_user, | ||
read_user_exists, | ||
read_user, | ||
read_users, | ||
) | ||
from database.connection import get_db | ||
from schemas.models import UserCreate, User, Token | ||
|
||
|
||
ACCESS_TOKEN_EXPIRE_MINUTES = 30 | ||
|
||
|
||
router = APIRouter( | ||
prefix="/users", | ||
tags=["users"], | ||
responses={404: {"description": "Not found"}}, | ||
) | ||
|
||
|
||
@router.get( | ||
"/", | ||
summary="Get a list of users", | ||
description="Fetches a list of users from the database. Optionally, add a search parameter to filter results.", | ||
) | ||
def get_users( | ||
db: Annotated[Session, Depends(get_db)], | ||
query: str | None = None, | ||
offset: int = 0, | ||
) -> Sequence[User]: | ||
return read_users(db, query=query, offset=offset) | ||
|
||
|
||
@router.post( | ||
"/", | ||
summary="Create a new user in the database", | ||
) | ||
async def new_user(user: UserCreate, db: Annotated[Session, Depends(get_db)]) -> User: | ||
# make sure user doesn't already exist | ||
if read_user_exists(db, user.pennkey): | ||
raise HTTPException(400, "User already exists") | ||
|
||
try: | ||
result = create_user(db, user) | ||
if result is None: | ||
raise HTTPException(status_code=400, detail="User could not be created") | ||
except Exception as e: | ||
print(e) | ||
raise HTTPException(status_code=500, detail="Trolma") | ||
|
||
return result | ||
|
||
|
||
@router.post( | ||
"/token", | ||
summary="Login with PennKey and password", | ||
) | ||
def login_for_access_token( | ||
form_data: Annotated[OAuth2PasswordRequestForm, Depends()], | ||
db: Annotated[Session, Depends(get_db)], | ||
) -> Token: | ||
# authenticate user | ||
user = authenticate_user(db, form_data.username, form_data.password) | ||
if user is None: | ||
raise HTTPException(status_code=401, detail="Invalid credentials") | ||
|
||
# create JWT access token | ||
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) | ||
access_token = create_access_token( | ||
data={"sub": user.pennkey}, expires_delta=access_token_expires | ||
) | ||
|
||
return Token(access_token=access_token, token_type="bearer") | ||
|
||
|
||
@router.get( | ||
"/me", | ||
summary="Get info about the current user", | ||
description="Based on the provided token, fetches information on the current user.", | ||
) | ||
def read_users_me( | ||
current_user: Annotated[User, Depends(get_current_user)], | ||
) -> User: | ||
return current_user | ||
|
||
|
||
@router.get( | ||
"/{pennkey}", | ||
summary="Get info about a specific user", | ||
description="Based on `pennkey`, fetches information on a specific user.", | ||
) | ||
def get_user_info(db: Annotated[Session, Depends(get_db)], pennkey: str) -> User: | ||
result = read_user(db, pennkey) | ||
if result is None: | ||
raise HTTPException(status_code=404, detail="User not found") | ||
return result |
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
Oops, something went wrong.