Ejemplo n.º 1
0
async def join(
    game: GameBaseJoin,
    session: Session = Depends(get_session),
    user: UserBaseSession = Depends(JWTBearer())
) -> Message:
    """Join game request"""
    TokenValidator.check_token(session, user.id)
    # Check if user is in another game
    db_game = ControllerGame.get_by_username(session, user.username)
    if db_game is not None:
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User is in another game.")
    # Fetch game object
    db_game = ControllerGame.get_by_id(session, game.id)
    # Check if given game id is valid
    if db_game is None:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Invalid game id.")
    # Check if game has open place for joining
    if db_game.second_user_id is not None:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Game is full.")
    # Check if game has password -> Verify password
    if db_game.password is not None:
        if game.password is None or Security.verify_pwd(game.password, db_game.password) is False:
            raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Invalid password.")
    ControllerGame.join(session, game.id, user.id)
    return Message(detail="Joining is successful.")
Ejemplo n.º 2
0
 def create(cls, session: Session, user: UserBaseCreate) -> Users:
     db_user = Users(**user.dict())
     db_user.password = Security.get_pwd_hash(user.password)
     session.add(db_user)
     session.commit()
     session.refresh(db_user)
     return db_user
Ejemplo n.º 3
0
 async def __call__(self, request: Request) -> UserBaseSession:
     credentials: Optional[HTTPAuthorizationCredentials] = await super(
         JWTBearer, self).__call__(request)
     if credentials is not None:
         if not credentials.scheme == "Bearer":
             raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                                 detail="Invalid authentication scheme.")
         return Security.decode_token(credentials.credentials)
     else:
         raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
                             detail="Invalid authorization code.")
Ejemplo n.º 4
0
async def login(
    user: UserBaseLogin, session: Session = Depends(get_session)
) -> UserBaseLoginResponse:
    """Login request"""
    db_user = ControllerUser.authenticate(session, user.username,
                                          user.password)
    if db_user is None:
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
                            detail="Invalid credentials.")
    # Check token
    db_token = ControllerToken.get_by_user_id(session, db_user.id)
    if db_token is None:
        # User is not logged in before
        access_token = Security.create_token(db_user)
        ControllerToken.save_token(session, access_token, db_user.id)
    elif db_token.valid is True:
        # User is already logged in
        access_token = db_token.token
    else:
        # User is logged out -> Give new token
        access_token = Security.create_token(db_user)
        ControllerToken.update_token(session, db_token.id, access_token)
    return UserBaseLoginResponse(token=access_token)
Ejemplo n.º 5
0
async def create(
    game: GameBaseCreate,
    session: Session = Depends(get_session),
    user: UserBaseSession = Depends(JWTBearer())
) -> GameBaseCreateResponse:
    """Create game request"""
    TokenValidator.check_token(session, user.id)
    # Check if user is in another game
    db_game = ControllerGame.get_by_username(session, user.username)
    if db_game is not None:
        raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="User is in another game.")
    if game.name is None:
        game.name = f"{user.username}'s Game"
    if game.password is not None:
        game.password = Security.get_pwd_hash(game.password)
    db_game = ControllerGame.create(session, game, user.id)
    return GameBaseCreateResponse(id=db_game.id)
Ejemplo n.º 6
0
 def authorize_socket(token: WebsocketToken) -> UserBaseSession:
     """Authorize token coming"""
     return Security.decode_token(token.token)
Ejemplo n.º 7
0
 def authenticate(cls, session: Session, username: str, password: str) -> Optional[Users]:
     user = cls.get_by_username(session, username)
     if user is not None and Security.verify_pwd(password, user.password) is True:
         return user
     else:
         return None