MCPcopy Index your code
hub / github.com/fastapi-users/fastapi-users / verify

Method verify

fastapi_users/manager.py:306–356  ·  view source on GitHub ↗

Validate a verification request. Changes the is_verified flag of the user to True. Triggers the on_after_verify handler on success. :param token: The verification token generated by request_verify. :param request: Optional FastAPI request that trig

(self, token: str, request: Request | None = None)

Source from the content-addressed store, hash-verified

304 await self.on_after_request_verify(user, token, request)
305
306 async def verify(self, token: str, request: Request | None = None) -> models.UP:
307 """
308 Validate a verification request.
309
310 Changes the is_verified flag of the user to True.
311
312 Triggers the on_after_verify handler on success.
313
314 :param token: The verification token generated by request_verify.
315 :param request: Optional FastAPI request that
316 triggered the operation, defaults to None.
317 :raises InvalidVerifyToken: The token is invalid or expired.
318 :raises UserAlreadyVerified: The user is already verified.
319 :return: A verified user of type models.UP.
320 """
321 try:
322 data = decode_jwt(
323 token,
324 self.verification_token_secret,
325 [self.verification_token_audience],
326 )
327 except jwt.PyJWTError:
328 raise exceptions.InvalidVerifyToken()
329
330 try:
331 user_id = data["sub"]
332 email = data["email"]
333 except KeyError:
334 raise exceptions.InvalidVerifyToken()
335
336 try:
337 user = await self.get_by_email(email)
338 except exceptions.UserNotExists:
339 raise exceptions.InvalidVerifyToken()
340
341 try:
342 parsed_id = self.parse_id(user_id)
343 except exceptions.InvalidID:
344 raise exceptions.InvalidVerifyToken()
345
346 if parsed_id != user.id:
347 raise exceptions.InvalidVerifyToken()
348
349 if user.is_verified:
350 raise exceptions.UserAlreadyVerified()
351
352 verified_user = await self._update(user, {"is_verified": True})
353
354 await self.on_after_verify(verified_user, request)
355
356 return verified_user
357
358 async def forgot_password(
359 self, user: models.UP, request: Request | None = None

Callers 11

verifyFunction · 0.80
test_invalid_tokenMethod · 0.80
test_token_expiredMethod · 0.80
test_missing_user_idMethod · 0.80
test_invalid_user_idMethod · 0.80
test_invalid_emailMethod · 0.80
test_verified_userMethod · 0.80
test_inactive_userMethod · 0.80
test_active_userMethod · 0.80

Calls 5

get_by_emailMethod · 0.95
parse_idMethod · 0.95
_updateMethod · 0.95
on_after_verifyMethod · 0.95
decode_jwtFunction · 0.90

Tested by 10

test_invalid_tokenMethod · 0.64
test_token_expiredMethod · 0.64
test_missing_user_idMethod · 0.64
test_invalid_user_idMethod · 0.64
test_invalid_emailMethod · 0.64
test_verified_userMethod · 0.64
test_inactive_userMethod · 0.64
test_active_userMethod · 0.64