0
0
Microservicessystem_design~10 mins

Authentication at gateway level in Microservices - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the incoming request has an Authorization header.

Microservices
if not request.headers.get([1]):
    return 'Unauthorized', 401
Drag options to blanks, or click blank then click option'
AUser-Agent
BAuthorization
CContent-Type
DAccept
Attempts:
3 left
💡 Hint
Common Mistakes
Checking wrong headers like Content-Type or User-Agent.
2fill in blank
medium

Complete the code to extract the token from the Authorization header.

Microservices
token = request.headers.get('Authorization').[1](' ')[1]
Drag options to blanks, or click blank then click option'
Asplit
Breplace
Cstrip
Djoin
Attempts:
3 left
💡 Hint
Common Mistakes
Using replace or strip which do not split the string.
3fill in blank
hard

Fix the error in the code to validate the token using the gateway's auth service.

Microservices
is_valid = auth_service.[1](token)
Drag options to blanks, or click blank then click option'
Avalidate_token
Bverify
Ccheck_token
Dauthenticate
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic method names that do not exist in the auth service.
4fill in blank
hard

Fill both blanks to reject requests without valid tokens and allow valid ones.

Microservices
if not [1]:
    return [2], 401
Drag options to blanks, or click blank then click option'
Ais_valid
B'Unauthorized'
C'Forbidden'
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Returning 'Forbidden' which is for authorization failure, not authentication.
5fill in blank
hard

Fill all three blanks to forward the authenticated request with user info to microservices.

Microservices
request.headers['X-User-ID'] = [1]
request.headers['X-User-Role'] = [2]
response = microservice_client.[3](request)
Drag options to blanks, or click blank then click option'
Auser_id
Buser_role
Csend_request
Dtoken
Attempts:
3 left
💡 Hint
Common Mistakes
Using the token directly instead of user info headers.
Using wrong method to forward request.