hug
hug copied to clipboard
Using firebase to authenticate hug api
First of all, great tool here! Absolutely love the simple approach (on the outside).
Next I'll admit that authentication and authorization makes my head spin. I've decided to control which users are allowed to use my app by using Google Clouds Firebase.
How should I integrate this with my hug
API? I have looked at examples/authentication.py
but it isn't clear to me how I should combine the libraries.
Hope someone can be helpful out there. Thanks a bunch!
Hello, please refer to Stack Overflow for this type of question. Besides that, I have a code that maybe can open better your mind:
def basicAccess(request, response, context=None, **kwargs):
"""Token verification"""
token = request.get_header('Authorization')
if token:
if token.split("Bearer ",1)[1]:
token = token.split("Bearer ",1)[1]
secret_key = getSecret()
try:
content = jwt.decode(token.encode('utf8'), secret_key, algorithm='HS256')
return True
except jwt.DecodeError:
response.status = HTTP_403
return { "error" : "access_denied" }
response.status = HTTP_403
return { "error" : "access_denied" }
response.status = HTTP_403
return { "error" : "access_denied" }
And then, you can use like that:
@hug.get('/', requires=basicAccess)
def home(.....
Maybe this repo can help you!