Django verify auth from request

Viewed 54

I am interested in having the user send a request in this fashion, using requests:

import requests
url = "https://postman-echo.com/basic-auth"
username = "postman"
password = "password"
response = requests.get(url, auth=(username, password))

Then in my Django model I need to be able to verify that those auth credentials are matching with the right user.

I cannot access request.auth and there are no auth fields in request.META.

What is the right way to access those credentials from the Django side?

1 Answers

I found the answer here.

auth_header = request.META['HTTP_AUTHORIZATION']
encoded_credentials = auth_header.split(' ')[1]  # Removes "Basic " to isolate credentials
decoded_credentials = base64.b64decode(encoded_credentials).decode("utf-8").split(':')
Related