Created
November 23, 2017 11:05
-
-
Save sreecodeslayer/e06f8b71665ab2a7c54969c4a85e1972 to your computer and use it in GitHub Desktop.
Use authorization decorator for UI Based session login as well as Auth Header Request like in Basic Auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def validate_api_or_user(f): | |
@wraps(f) | |
def decorated(*args, **kwargs): | |
auth = request.authorization | |
print auth | |
if not auth: # no header set | |
if current_user.is_authenticated: # check active session | |
g.user = current_user | |
return f(*args, **kwargs) | |
else: | |
abort(401) | |
user = User.objects.get(username=auth.username) | |
if user is None or not user.verify(auth.password): | |
abort(401) | |
g.user = user | |
return f(*args, **kwargs) | |
return decorated |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment