-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29f2eb1
commit a3dd928
Showing
6 changed files
with
108 additions
and
10 deletions.
There are no files selected for viewing
Binary file not shown.
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,36 @@ | ||
""" | ||
create_user.py | ||
------------- | ||
A convenience script to create a user. | ||
""" | ||
|
||
from getpass import getpass | ||
|
||
from sqlmodel import SQLModel, Session, create_engine | ||
|
||
from schemas import User | ||
|
||
|
||
engine = create_engine( | ||
"sqlite:///carsharing.db", | ||
connect_args={"check_same_thread": False}, # Needed for SQLite | ||
echo=True # Log generated SQL | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
print("Creating tables (if necessary)") | ||
SQLModel.metadata.create_all(engine) | ||
|
||
print("--------") | ||
|
||
print("This script will create a user and save it in the database.") | ||
|
||
username = input("Please enter username\n") | ||
pwd = getpass("Please enter password\n") | ||
|
||
with Session(engine) as session: | ||
user = User(username=username) | ||
user.set_password(pwd) | ||
session.add(user) | ||
session.commit() |
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,36 @@ | ||
from fastapi import Depends, HTTPException, APIRouter | ||
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm | ||
from sqlmodel import Session, select | ||
from starlette import status | ||
|
||
from db import get_session | ||
from schemas import UserOutput, User | ||
|
||
URL_PREFIX="/auth" | ||
router = APIRouter(prefix=URL_PREFIX) | ||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl=f"{URL_PREFIX}/token") | ||
|
||
|
||
def get_current_user(token: str = Depends(oauth2_scheme), | ||
session: Session = Depends(get_session)) -> UserOutput: | ||
query = select(User).where(User.username == token) | ||
user = session.exec(query).first() | ||
if user: | ||
return UserOutput.from_orm(user) | ||
else: | ||
raise HTTPException( | ||
status_code=status.HTTP_401_UNAUTHORIZED, | ||
detail="Username or password incorrect", | ||
headers={"WWW-Authenticate": "Bearer"}, | ||
) | ||
|
||
|
||
@router.post("/token") | ||
async def login(form_data: OAuth2PasswordRequestForm = Depends(), | ||
session: Session = Depends(get_session)): | ||
query = select(User).where(User.username == form_data.username) | ||
user = session.exec(query).first() | ||
if user and user.verify_password(form_data.password): | ||
return {"access_token": user.username, "token_type": "bearer"} | ||
else: | ||
raise HTTPException(status_code=400, detail="Incorrect username or password") |
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