Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard 401 response when using HTTP auth in flask

In flask, I'm using the following snippet to enable HTTP auth:

def authenticate():     return Response('<Why access is denied string goes here...>', 401, {'WWW-Authenticate':'Basic realm="Login Required"'}) 

Now, in my past experience with Flask, if someone's credentials are incorrect and I want to let them know I can just call:

abort(401) 

This gives you the basic apache 401 response. Does anyone know how I can implement that with the snippet above?

Thanks

like image 879
ingh.am Avatar asked Oct 24 '11 14:10

ingh.am


People also ask

How do I send my 401 response to a flask?

Create a function whose only argument is the HTTP error status code, make it return a flask. Response instance, and decorate it with @app. errorhandler. You can then use abort(401) to your heart's content.

How do flask handle errors?

Step 1 — Using The Flask Debugger. In this step, you'll create an application that has a few errors and run it without debug mode to see how the application responds. Then you'll run it with debug mode on and use the debugger to troubleshoot application errors.

How to add HTTP Basic Access Authentication to a flask application?

A Flask extension for adding HTTP basic access authentication to the application. app – a Flask instance. Defaults to None. If no application is provided on creation, then it can be provided later on via init_app (). Check the request for HTTP basic access authentication header and try to authenticate the user.

How to implement custom 401 error response in flask?

This gives you the basic apache 401 response. Does anyone know how I can implement that with the snippet above? Show activity on this post. Custom error responses are really quite easy in Flask. Create a function whose only argument is the HTTP error status code, make it return a flask.Response instance, and decorate it with @app.errorhandler.

What is HTTP_AUTH_required in flask?

Basic Auth is supported in Flask-Security, using the @http_auth_required () decorator. If a request for an endpoint protected with @http_auth_required is received, and the request doesn’t contain the appropriate HTTP Headers, a 401 is returned along with the required WWW-Authenticate header.

What is the use of a 401 in an HTTP request?

It is a callable object, that raises various predefined HTTP exceptions (subclasses of HTTPException) on demand. Check out the code here for details. The predefined Unauthorized (which is mapped to 401) only defines the code and a message, but not the WWW-Authenticate header, which as you know is required to trigger the login-popup with browsers.


1 Answers

Custom error responses are really quite easy in Flask. Create a function whose only argument is the HTTP error status code, make it return a flask.Response instance, and decorate it with @app.errorhandler.

@app.errorhandler(401) def custom_401(error):     return Response('<Why access is denied string goes here...>', 401, {'WWW-Authenticate':'Basic realm="Login Required"'}) 

You can then use abort(401) to your heart's content.

like image 133
ʇsәɹoɈ Avatar answered Oct 08 '22 13:10

ʇsәɹoɈ