API Documentation¶
- class flask_httpauth.HTTPBasicAuth(scheme=None, realm=None)¶
Create a basic authentication object.
If the optional
schemeargument is provided, it will be used instead of the standard “Basic” scheme in theWWW-Authenticateresponse. A fairly common practice is to use a custom scheme to prevent browsers from prompting the user to login.The
realmargument can be used to provide an application defined realm with theWWW-Authenticateheader.- current_user()¶
The user object returned by the
verify_passwordcallback on successful authentication. If no user is returned by the callback, this is set to the username passed by the client. Example:@app.route('/') @auth.login_required def index(): user = auth.current_user() return "Hello, {}!".format(user.name)
- error_handler(f)¶
Decorator for a function that will be called by the framework when it is necessary to send an authentication error back to the client. The function can take one argument, the status code of the error, which can be 401 (incorrect credentials) or 403 (correct, but insufficient credentials). To preserve compatiiblity with older releases of this package, the function can also be defined without arguments. The return value from this function must by any accepted response type in Flask routes. If this callback isn’t provided a default error response is generated. Example:
@auth.error_handler def auth_error(status): return "Access Denied", status
- get_password(f)¶
Deprecated Decorator for a function that will be called by the framework to obtain the password for a given user. Example:
@auth.get_password def get_password(username): return db.get_user_password(username)
- get_user_roles(f)¶
Decorator for a function that will be called by the framework to obtain the roles assigned to a given user. The callback function takes a single argument, the user for which roles are requested. The user object passed to this function will be the one returned by the “verify” callback. If the verify callback returned
Trueinstead of a user object, then theAuthorizationobject provided by Flask will be passed to this function. The function should return the role or list of roles that belong to the user. Example:@auth.get_user_roles def get_user_roles(user): return user.get_roles()
- hash_password(f)¶
Deprecated Decorator for a function that will be called by the framework to apply a custom hashing algorithm to the password provided by the client. If this callback isn’t provided the password will be checked unchanged. The callback can take one or two arguments. The one argument version receives the password to hash, while the two argument version receives the username and the password in that order. Example single argument callback:
@auth.hash_password def hash_password(password): return md5(password).hexdigest()
Example two argument callback:
@auth.hash_password def hash_pw(username, password): salt = get_salt(username) return hash(password, salt)
- login_required(f=None, role=None, optional=None)¶
Decorator for a function that will be called when authentication is successful. This will typically be a Flask view function. Example:
@app.route('/private') @auth.login_required def private_page(): return "Only for authorized people!"
An optional
roleargument can be given to further restrict access by roles. Example:@app.route('/private') @auth.login_required(role='admin') def private_page(): return "Only for admins!"
An optional
optionalargument can be set toTrueto allow the route to execute also when authentication is not included with the request, in which caseauth.current_user()will be set toNone. Example:@app.route('/private') @auth.login_required(optional=True) def private_page(): user = auth.current_user() return "Hello {}!".format( user.name if user is not None else 'anonymous')
- username()¶
Deprecated A view function that is protected with this class can access the logged username through this method. Example:
@app.route('/') @auth.login_required def index(): return "Hello, {}!".format(auth.username())
- verify_password(f)¶
Decorator for a function that will be called by the framework to verify that the username and password combination provided by the client are valid. The callback function takes two arguments, the username and the password. It must return the user object if credentials are valid, or
Trueif a user object is not available. In case of failed authentication, it should returnNoneorFalse. Example usage:@auth.verify_password def verify_password(username, password): user = User.query.filter_by(username).first() if user and passlib.hash.sha256_crypt.verify( password, user.password_hash): return user
If this callback is defined, it is also invoked when the request does not have the
Authorizationheader with user credentials, and in this case both theusernameandpasswordarguments are set to empty strings. The application can opt to returnTruein this case and that will allow anonymous users access to the route. The callback function can indicate that the user is anonymous by writing a state variable toflask.gor by checking ifauth.current_user()isNone.Note that when a
verify_passwordcallback is provided theget_passwordandhash_passwordcallbacks are not used.
- class flask_httpauth.HTTPDigestAuth(scheme=None, realm=None, use_ha1_pw=False, qop='auth', algorithm='MD5')¶
Create a digest authentication object.
If the optional
schemeargument is provided, it will be used instead of the “Digest” scheme in theWWW-Authenticateresponse. A fairly common practice is to use a custom scheme to prevent browsers from prompting the user to login.The
realmargument can be used to provide an application defined realm with theWWW-Authenticateheader.If
use_ha1_pwis False, then theget_passwordcallback needs to return the plain text password for the given user. Ifuse_ha1_pwis True, theget_passwordcallback needs to return the HA1 value for the given user. The advantage of settinguse_ha1_pwtoTrueis that it allows the application to store the HA1 hash of the password in the user database.The
qopoption configures a list of accepted quality of protection extensions. This argument can be given as a comma-separated string, a list of strings, orNoneto disable. The default isauth. Theauth-intoption is currently not implemented.The
algorithmoption configures the hash generation algorithm to use. The default isMD5. The two algorithms that are implemented areMD5andMD5-Sess.- current_user()¶
The user object returned by the
verify_passwordcallback on successful authentication. If no user is returned by the callback, this is set to the username passed by the client. Example:@app.route('/') @auth.login_required def index(): user = auth.current_user() return "Hello, {}!".format(user.name)
- error_handler(f)¶
Decorator for a function that will be called by the framework when it is necessary to send an authentication error back to the client. The function can take one argument, the status code of the error, which can be 401 (incorrect credentials) or 403 (correct, but insufficient credentials). To preserve compatiiblity with older releases of this package, the function can also be defined without arguments. The return value from this function must by any accepted response type in Flask routes. If this callback isn’t provided a default error response is generated. Example:
@auth.error_handler def auth_error(status): return "Access Denied", status
- generate_ha1(username, password)¶
Generate the HA1 hash that can be stored in the user database when
use_ha1_pwis set to True in the constructor.
- generate_nonce(f)¶
If defined, this callback function will be called by the framework to generate a nonce. If this is defined,
verify_nonceshould also be defined.This can be used to use a state storage mechanism other than the session.
- generate_opaque(f)¶
Decorator for a function that will be called by the framework to generate an opaque value. If this is defined,
verify_opaqueshould also be defined.This can be used to use a state storage mechanism other than the session.
- get_password(f)¶
Deprecated Decorator for a function that will be called by the framework to obtain the password for a given user. Example:
@auth.get_password def get_password(username): return db.get_user_password(username)
- get_user_roles(f)¶
Decorator for a function that will be called by the framework to obtain the roles assigned to a given user. The callback function takes a single argument, the user for which roles are requested. The user object passed to this function will be the one returned by the “verify” callback. If the verify callback returned
Trueinstead of a user object, then theAuthorizationobject provided by Flask will be passed to this function. The function should return the role or list of roles that belong to the user. Example:@auth.get_user_roles def get_user_roles(user): return user.get_roles()
- login_required(f=None, role=None, optional=None)¶
Decorator for a function that will be called when authentication is successful. This will typically be a Flask view function. Example:
@app.route('/private') @auth.login_required def private_page(): return "Only for authorized people!"
An optional
roleargument can be given to further restrict access by roles. Example:@app.route('/private') @auth.login_required(role='admin') def private_page(): return "Only for admins!"
An optional
optionalargument can be set toTrueto allow the route to execute also when authentication is not included with the request, in which caseauth.current_user()will be set toNone. Example:@app.route('/private') @auth.login_required(optional=True) def private_page(): user = auth.current_user() return "Hello {}!".format( user.name if user is not None else 'anonymous')
- username()¶
Deprecated A view function that is protected with this class can access the logged username through this method. Example:
@app.route('/') @auth.login_required def index(): return "Hello, {}!".format(auth.username())
- verify_nonce(f)¶
Decorator for a function that will be called by the framework to verify that a nonce is valid. It will be called with a single argument: the nonce to be verified.
This can be used to use a state storage mechanism other than the session.
- verify_opaque(f)¶
Decorator for a function that will be called by the framework to verify that an opaque value is valid. It will be called with a single argument: the opaque value to be verified.
This can be used to use a state storage mechanism other than the session.
- class flask_httpauth.HTTPTokenAuth(scheme='Bearer', realm=None, header=None)¶
- current_user()¶
The user object returned by the
verify_passwordcallback on successful authentication. If no user is returned by the callback, this is set to the username passed by the client. Example:@app.route('/') @auth.login_required def index(): user = auth.current_user() return "Hello, {}!".format(user.name)
- error_handler(f)¶
Decorator for a function that will be called by the framework when it is necessary to send an authentication error back to the client. The function can take one argument, the status code of the error, which can be 401 (incorrect credentials) or 403 (correct, but insufficient credentials). To preserve compatiiblity with older releases of this package, the function can also be defined without arguments. The return value from this function must by any accepted response type in Flask routes. If this callback isn’t provided a default error response is generated. Example:
@auth.error_handler def auth_error(status): return "Access Denied", status
- get_password(f)¶
Deprecated Decorator for a function that will be called by the framework to obtain the password for a given user. Example:
@auth.get_password def get_password(username): return db.get_user_password(username)
- get_user_roles(f)¶
Decorator for a function that will be called by the framework to obtain the roles assigned to a given user. The callback function takes a single argument, the user for which roles are requested. The user object passed to this function will be the one returned by the “verify” callback. If the verify callback returned
Trueinstead of a user object, then theAuthorizationobject provided by Flask will be passed to this function. The function should return the role or list of roles that belong to the user. Example:@auth.get_user_roles def get_user_roles(user): return user.get_roles()
- login_required(f=None, role=None, optional=None)¶
Decorator for a function that will be called when authentication is successful. This will typically be a Flask view function. Example:
@app.route('/private') @auth.login_required def private_page(): return "Only for authorized people!"
An optional
roleargument can be given to further restrict access by roles. Example:@app.route('/private') @auth.login_required(role='admin') def private_page(): return "Only for admins!"
An optional
optionalargument can be set toTrueto allow the route to execute also when authentication is not included with the request, in which caseauth.current_user()will be set toNone. Example:@app.route('/private') @auth.login_required(optional=True) def private_page(): user = auth.current_user() return "Hello {}!".format( user.name if user is not None else 'anonymous')
- username()¶
Deprecated A view function that is protected with this class can access the logged username through this method. Example:
@app.route('/') @auth.login_required def index(): return "Hello, {}!".format(auth.username())
- verify_token(f)¶
Decorator for a function that will be called by the framework to verify that the credentials sent by the client with the
Authorizationheader are valid. The callback function takes one argument, the token provided by the client. The function must return the user object if the token is valid, orTrueif a user object is not available. In case of a failed authentication, the function should returnNoneorFalse. Example usage:@auth.verify_token def verify_token(token): return User.query.filter_by(token=token).first()
Note that a
verify_tokencallback is required when using this class.
- class flask_httpauth.MultiAuth(main_auth, *additional_auths)¶
Create a multiple authentication object.
The arguments are one or more instances of
HTTPBasicAuth,HTTPDigestAuthorHTTPTokenAuth. A route protected with this authentication method will try all the given authentication objects until one succeeds.- current_user()¶
The authenticated user.
- login_required(f=None, role=None, optional=None)¶
Decorator for a function that will be called when authentication is successful. This will typically be a Flask view function.