async def refresh_token(email: str = Depends(valid_credentials)): """ Endpoint that creates a new web token. As the funciton "updates" creating a new token, it has the PUT method. Need to be logged in to use. """ if not email: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Incorrect password", headers={"WWW-Authenticate": "Bearer"}, ) try: with db_session: username: str = db.get( "select username from DB_User where email=$email") access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) access_token = create_access_token( data={"email": email, "username": username}, expires_delta=access_token_expires, ) return {"access_token": access_token, "token_type": "bearer"} except: raise HTTPException( status_code=405, detail="Something went wrong" )
async def check_email_status(email: str): with db_session: try: email_confirmed = db.get( "select email_confirmed from DB_User where email = $email" ) return email_confirmed except BaseException: return None
def authenticate_user(mail: str, password: str): """ Function that autenthicates the user by checking his password """ keys = ('username', 'email', 'hashed_password', 'email_confirmed', 'icon', 'creation_date') try: user_tuple = db.get("select * from DB_User where email = $mail") except: raise HTTPException(status_code=400, detail="Incorrect mail address") user = dict(zip(keys, user_tuple)) if not verify_password(password, user['hashed_password']): return False return user
async def get_current_user(email: str = Depends(valid_credentials)): """ Function that return a dict with all the users data from the database """ if email is None: raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Bearer"}) keys = ('username', 'email', 'hashed_password', 'email_confirmed', 'icon', 'creation_date') with db_session: try: user_tuple = db.get("select * from DB_User where email = $email") except: raise HTTPException(status_code=400, detail="Incorrect email or password") user = dict(zip(keys, user_tuple)) return user
async def validate_user(email: str, code: str): try: with db_session: data = db.get( "select email,code from Validation_Tuple where email=$email") if data[1] != code: raise HTTPException( status_code=409, detail="Invalid validation code") user = db.DB_User.get(email=email) user.set(email_confirmed=True) commit() html = """ <!DOCTYPE html> <html> <head> <title>Secret voldemort</title> </head> <body style="background-color:black; text-align: center;"> <h1 style="color: goldenrod; padding-top: 60px; text-shadow:1px 1px 2px darkgoldenrod;" >Email Verified!</h1> <h5 style="color: goldenrod;text-shadow:1px 1px 2px darkgoldenrod;" > You can start playing now! </h5> <div> <img src="https://images-ext-2.discordapp.net/external/TKE5N1VRYV4jDNc2EFBou31abWc9yuAi3J5zP3gztAc/https/1000logos.net/wp-content/uploads/2018/08/Hogwarts-Logo.jpg"; style="width: 800px;height: 400px;"> </img> </div> </body> </html> """ return HTMLResponse(html) except: raise HTTPException(status_code=404, detail="Email not found")