Challenge - 5 Problems
OAuth2 Password Flow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this FastAPI OAuth2 password flow token endpoint?
Consider this FastAPI endpoint using OAuth2 password flow. What will the response JSON contain after a successful login?
FastAPI
from fastapi import FastAPI, Depends from fastapi.security import OAuth2PasswordRequestForm from fastapi.responses import JSONResponse app = FastAPI() @app.post('/token') async def login(form_data: OAuth2PasswordRequestForm = Depends()): if form_data.username == 'user' and form_data.password == 'pass': return {'access_token': 'fake-token', 'token_type': 'bearer'} return JSONResponse(status_code=400, content={'error': 'Invalid credentials'})
Attempts:
2 left
💡 Hint
Check what the function returns when username and password match.
✗ Incorrect
The endpoint returns a JSON with access_token and token_type when credentials are correct.
❓ state_output
intermediate1:30remaining
What is the value of the token_type field in the OAuth2 password flow response?
Given a successful OAuth2 password flow token response in FastAPI, what is the value of the 'token_type' field?
FastAPI
from fastapi import FastAPI, Depends from fastapi.security import OAuth2PasswordRequestForm app = FastAPI() @app.post('/token') async def login(form_data: OAuth2PasswordRequestForm = Depends()): return {'access_token': 'token123', 'token_type': 'bearer'}
Attempts:
2 left
💡 Hint
OAuth2 standard uses a lowercase token type.
✗ Incorrect
The OAuth2 standard specifies the token type as 'bearer' in lowercase.
📝 Syntax
advanced2:00remaining
Which option correctly defines the OAuth2PasswordRequestForm dependency in FastAPI?
Select the correct way to use OAuth2PasswordRequestForm in a FastAPI endpoint for password flow authentication.
FastAPI
from fastapi import FastAPI, Depends from fastapi.security import OAuth2PasswordRequestForm app = FastAPI() @app.post('/token') async def login(???): pass
Attempts:
2 left
💡 Hint
Use Depends() to declare dependencies in FastAPI function parameters.
✗ Incorrect
FastAPI requires dependencies to be declared with Depends(). Option A is correct.
🔧 Debug
advanced2:30remaining
What error will this FastAPI OAuth2 password flow code raise?
Identify the error raised by this code snippet when the /token endpoint is called with valid credentials.
FastAPI
from fastapi import FastAPI, Depends from fastapi.security import OAuth2PasswordRequestForm app = FastAPI() @app.post('/token') async def login(form_data: OAuth2PasswordRequestForm): if form_data.username == 'user' and form_data.password == 'pass': return {'access_token': 'token', 'token_type': 'bearer'} return {'error': 'Invalid credentials'}
Attempts:
2 left
💡 Hint
Check how dependencies must be declared with Depends() in FastAPI.
✗ Incorrect
The function parameter lacks Depends(), so FastAPI cannot inject the dependency, causing a TypeError.
🧠 Conceptual
expert3:00remaining
Which statement best describes the OAuth2 password flow in FastAPI?
Choose the most accurate description of how OAuth2 password flow works in FastAPI applications.
Attempts:
2 left
💡 Hint
Password flow involves direct username and password exchange for tokens.
✗ Incorrect
OAuth2 password flow involves sending user credentials directly to the token endpoint to get an access token.