本文整理汇总了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",
}
示例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",
}
示例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"}
示例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"}
示例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'}
示例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
示例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