mirror of
				https://github.com/fastapi-users/fastapi-users.git
				synced 2025-11-04 22:56:56 +08:00 
			
		
		
		
	* Add routes for user activation (#403) * Add routes for user activation Generate a token after creating the user in register route, passing to `activation_callback`, if `activation_callback` supplied Create new `/activate` route that will verify the token and activate the user Add new error codes to `fastapi_users/router/common.py` Update documentation Add tests Co-authored-by: Mark Todd <markpeter.todd@hotmail.co.uk> * Rework routes for user activation * Separate verification logic and token generation into `/fastapi_users/router/verify.py`, with per-route callbacks for custom behaviour * Return register router to original state * Added `is_verified` property to user models * Added `requires_verification` argument to `get_users_router`and `get_auth_router` * Additional dependencies added for verification in `fastapi_users/authentication/__init__.py` * Update tests for new behaviour * Update `README.md` to describe a workaround for possible problems during testing, by exceeding ulimit file descriptor limit Co-authored-by: Mark Todd <markpeter.todd@hotmail.co.uk> * Restored docs to original state. * All other modifications reqested added Kebab-case on request-verify-token SECRET now used as test string Other minor changes Co-authored-by: Mark Todd <markpeter.todd@hotmail.co.uk> * Embed token in body in verify route * Reorganize checks in verify route and add unit test * Ignore coverage on Protocol classes * Tweak verify_user function to take full user in parameter * Improve unit tests structure regarding parametrized test client * Make after_verification_request optional to be more consistent with other routers * Tweak status codes on verify routes * Write documentation for verification feature * Add not released warning on verify docs Co-authored-by: Edd Salkield <edd@salkield.uk> Co-authored-by: Mark Todd <markpeter.todd@hotmail.co.uk>
		
			
				
	
	
		
			128 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			128 lines
		
	
	
		
			4.0 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
from typing import Callable, Optional, Type, cast
 | 
						|
 | 
						|
import jwt
 | 
						|
from fastapi import APIRouter, Body, HTTPException, Request, status
 | 
						|
from pydantic import UUID4, EmailStr
 | 
						|
 | 
						|
from fastapi_users import models
 | 
						|
from fastapi_users.router.common import ErrorCode, run_handler
 | 
						|
from fastapi_users.user import (
 | 
						|
    GetUserProtocol,
 | 
						|
    UserAlreadyVerified,
 | 
						|
    UserNotExists,
 | 
						|
    VerifyUserProtocol,
 | 
						|
)
 | 
						|
from fastapi_users.utils import JWT_ALGORITHM, generate_jwt
 | 
						|
 | 
						|
VERIFY_USER_TOKEN_AUDIENCE = "fastapi-users:verify"
 | 
						|
 | 
						|
 | 
						|
def get_verify_router(
 | 
						|
    verify_user: VerifyUserProtocol,
 | 
						|
    get_user: GetUserProtocol,
 | 
						|
    user_model: Type[models.BaseUser],
 | 
						|
    verification_token_secret: str,
 | 
						|
    verification_token_lifetime_seconds: int = 3600,
 | 
						|
    after_verification_request: Optional[
 | 
						|
        Callable[[models.UD, str, Request], None]
 | 
						|
    ] = None,
 | 
						|
    after_verification: Optional[Callable[[models.UD, Request], None]] = None,
 | 
						|
):
 | 
						|
    router = APIRouter()
 | 
						|
 | 
						|
    @router.post("/request-verify-token", status_code=status.HTTP_202_ACCEPTED)
 | 
						|
    async def request_verify_token(
 | 
						|
        request: Request, email: EmailStr = Body(..., embed=True)
 | 
						|
    ):
 | 
						|
        try:
 | 
						|
            user = await get_user(email)
 | 
						|
            if user.is_verified:
 | 
						|
                raise HTTPException(
 | 
						|
                    status_code=status.HTTP_400_BAD_REQUEST,
 | 
						|
                    detail=ErrorCode.VERIFY_USER_ALREADY_VERIFIED,
 | 
						|
                )
 | 
						|
            elif user.is_active:
 | 
						|
                token_data = {
 | 
						|
                    "user_id": str(user.id),
 | 
						|
                    "email": email,
 | 
						|
                    "aud": VERIFY_USER_TOKEN_AUDIENCE,
 | 
						|
                }
 | 
						|
                token = generate_jwt(
 | 
						|
                    token_data,
 | 
						|
                    verification_token_lifetime_seconds,
 | 
						|
                    verification_token_secret,
 | 
						|
                )
 | 
						|
 | 
						|
                if after_verification_request:
 | 
						|
                    await run_handler(after_verification_request, user, token, request)
 | 
						|
        except UserNotExists:
 | 
						|
            pass
 | 
						|
 | 
						|
        return None
 | 
						|
 | 
						|
    @router.post("/verify", response_model=user_model)
 | 
						|
    async def verify(request: Request, token: str = Body(..., embed=True)):
 | 
						|
        try:
 | 
						|
            data = jwt.decode(
 | 
						|
                token,
 | 
						|
                verification_token_secret,
 | 
						|
                audience=VERIFY_USER_TOKEN_AUDIENCE,
 | 
						|
                algorithms=[JWT_ALGORITHM],
 | 
						|
            )
 | 
						|
        except jwt.exceptions.ExpiredSignatureError:
 | 
						|
            raise HTTPException(
 | 
						|
                status_code=status.HTTP_400_BAD_REQUEST,
 | 
						|
                detail=ErrorCode.VERIFY_USER_TOKEN_EXPIRED,
 | 
						|
            )
 | 
						|
        except jwt.PyJWTError:
 | 
						|
            raise HTTPException(
 | 
						|
                status_code=status.HTTP_400_BAD_REQUEST,
 | 
						|
                detail=ErrorCode.VERIFY_USER_BAD_TOKEN,
 | 
						|
            )
 | 
						|
 | 
						|
        user_id = data.get("user_id")
 | 
						|
        email = cast(EmailStr, data.get("email"))
 | 
						|
 | 
						|
        if user_id is None:
 | 
						|
            raise HTTPException(
 | 
						|
                status_code=status.HTTP_400_BAD_REQUEST,
 | 
						|
                detail=ErrorCode.VERIFY_USER_BAD_TOKEN,
 | 
						|
            )
 | 
						|
 | 
						|
        try:
 | 
						|
            user_check = await get_user(email)
 | 
						|
        except UserNotExists:
 | 
						|
            raise HTTPException(
 | 
						|
                status_code=status.HTTP_400_BAD_REQUEST,
 | 
						|
                detail=ErrorCode.VERIFY_USER_BAD_TOKEN,
 | 
						|
            )
 | 
						|
 | 
						|
        try:
 | 
						|
            user_uuid = UUID4(user_id)
 | 
						|
        except ValueError:
 | 
						|
            raise HTTPException(
 | 
						|
                status_code=status.HTTP_400_BAD_REQUEST,
 | 
						|
                detail=ErrorCode.VERIFY_USER_BAD_TOKEN,
 | 
						|
            )
 | 
						|
 | 
						|
        if user_check.id != user_uuid:
 | 
						|
            raise HTTPException(
 | 
						|
                status_code=status.HTTP_400_BAD_REQUEST,
 | 
						|
                detail=ErrorCode.VERIFY_USER_BAD_TOKEN,
 | 
						|
            )
 | 
						|
 | 
						|
        try:
 | 
						|
            user = await verify_user(user_check)
 | 
						|
        except UserAlreadyVerified:
 | 
						|
            raise HTTPException(
 | 
						|
                status_code=status.HTTP_400_BAD_REQUEST,
 | 
						|
                detail=ErrorCode.VERIFY_USER_ALREADY_VERIFIED,
 | 
						|
            )
 | 
						|
 | 
						|
        if after_verification:
 | 
						|
            await run_handler(after_verification, user, request)
 | 
						|
 | 
						|
        return user
 | 
						|
 | 
						|
    return router
 |