当前位置: 首页>>代码示例>>Python>>正文


Python security.OAuth2PasswordRequestForm方法代码示例

本文整理汇总了Python中fastapi.security.OAuth2PasswordRequestForm方法的典型用法代码示例。如果您正苦于以下问题:Python security.OAuth2PasswordRequestForm方法的具体用法?Python security.OAuth2PasswordRequestForm怎么用?Python security.OAuth2PasswordRequestForm使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在fastapi.security的用法示例。


在下文中一共展示了security.OAuth2PasswordRequestForm方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: login

# 需要导入模块: from fastapi import security [as 别名]
# 或者: from fastapi.security import OAuth2PasswordRequestForm [as 别名]
def login(form_data: OAuth2PasswordRequestForm = Depends()):
    """
    OAuth2 compatible token login, get an access token for future requests.
    """
    bucket = get_default_bucket()
    user = crud.user.authenticate(
        bucket, username=form_data.username, password=form_data.password
    )
    if not user:
        raise HTTPException(status_code=400, detail="Incorrect email or password")
    elif not crud.user.is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")
    access_token_expires = timedelta(minutes=config.ACCESS_TOKEN_EXPIRE_MINUTES)
    return {
        "access_token": create_access_token(
            data={"username": user.username}, expires_delta=access_token_expires
        ),
        "token_type": "bearer",
    } 
开发者ID:tiangolo,项目名称:full-stack-fastapi-couchbase,代码行数:21,代码来源:login.py

示例2: login_access_token

# 需要导入模块: from fastapi import security [as 别名]
# 或者: from fastapi.security import OAuth2PasswordRequestForm [as 别名]
def login_access_token(
    db: Session = Depends(deps.get_db), form_data: OAuth2PasswordRequestForm = Depends()
) -> Any:
    """
    OAuth2 compatible token login, get an access token for future requests
    """
    user = crud.user.authenticate(
        db, email=form_data.username, password=form_data.password
    )
    if not user:
        raise HTTPException(status_code=400, detail="Incorrect email or password")
    elif not crud.user.is_active(user):
        raise HTTPException(status_code=400, detail="Inactive user")
    access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
    return {
        "access_token": security.create_access_token(
            user.id, expires_delta=access_token_expires
        ),
        "token_type": "bearer",
    } 
开发者ID:tiangolo,项目名称:full-stack-fastapi-postgresql,代码行数:22,代码来源:login.py

示例3: login_for_access_token

# 需要导入模块: from fastapi import security [as 别名]
# 或者: from fastapi.security import OAuth2PasswordRequestForm [as 别名]
def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends()):
    user = authenticate_user(fake_users_db, form_data.username, form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
    access_token = create_access_token(
        data={"sub": user.username}, expires_delta=access_token_expires
    )
    return {"access_token": access_token, "token_type": "bearer"} 
开发者ID:tiangolo,项目名称:fastapi,代码行数:15,代码来源:tutorial004.py

示例4: login

# 需要导入模块: from fastapi import security [as 别名]
# 或者: from fastapi.security import OAuth2PasswordRequestForm [as 别名]
def login(form_data: OAuth2PasswordRequestForm = Depends()):
    user_dict = fake_users_db.get(form_data.username)
    if not user_dict:
        raise HTTPException(status_code=400, detail="Incorrect username or password")
    user = UserInDB(**user_dict)
    hashed_password = fake_hash_password(form_data.password)
    if not hashed_password == user.hashed_password:
        raise HTTPException(status_code=400, detail="Incorrect username or password")

    return {"access_token": user.username, "token_type": "bearer"} 
开发者ID:tiangolo,项目名称:fastapi,代码行数:12,代码来源:tutorial003.py

示例5: login

# 需要导入模块: from fastapi import security [as 别名]
# 或者: from fastapi.security import OAuth2PasswordRequestForm [as 别名]
def login(data: OAuth2PasswordRequestForm = Depends()):
    user_identifier = data.username
    password = data.password

    user = load_user(user_identifier)
    if not user:
        raise InvalidCredentialsException
    elif password != user['password']:
        raise InvalidCredentialsException

    access_token = lm.create_access_token(
        data=dict(sub=user_identifier)
    )

    return {'access_token': access_token, 'token_type': 'bearer'} 
开发者ID:MushroomMaula,项目名称:fastapi_login,代码行数:17,代码来源:test_login_manager.py

示例6: create_oauth2_password_request_form

# 需要导入模块: from fastapi import security [as 别名]
# 或者: from fastapi.security import OAuth2PasswordRequestForm [as 别名]
def create_oauth2_password_request_form():
    def _create_oauth2_password_request_form(username, password):
        return OAuth2PasswordRequestForm(username=username, password=password, scope="")

    return _create_oauth2_password_request_form 
开发者ID:frankie567,项目名称:fastapi-users,代码行数:7,代码来源:test_db_base.py

示例7: get_auth_router

# 需要导入模块: from fastapi import security [as 别名]
# 或者: from fastapi.security import OAuth2PasswordRequestForm [as 别名]
def get_auth_router(
    backend: BaseAuthentication,
    user_db: BaseUserDatabase[models.BaseUserDB],
    authenticator: Authenticator,
) -> APIRouter:
    """Generate a router with login/logout routes for an authentication backend."""
    router = APIRouter()

    @router.post("/login")
    async def login(
        response: Response, credentials: OAuth2PasswordRequestForm = Depends()
    ):
        user = await user_db.authenticate(credentials)

        if user is None or not user.is_active:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ErrorCode.LOGIN_BAD_CREDENTIALS,
            )

        return await backend.get_login_response(user, response)

    if backend.logout:

        @router.post("/logout")
        async def logout(
            response: Response, user=Depends(authenticator.get_current_active_user)
        ):
            return await backend.get_logout_response(user, response)

    return router 
开发者ID:frankie567,项目名称:fastapi-users,代码行数:33,代码来源:auth.py


注:本文中的fastapi.security.OAuth2PasswordRequestForm方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。