hug icon indicating copy to clipboard operation
hug copied to clipboard

Using firebase to authenticate hug api

Open NixBiks opened this issue 5 years ago • 2 comments

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!

NixBiks avatar Mar 26 '19 21:03 NixBiks

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(.....

JGabrielGruber avatar Apr 10 '19 23:04 JGabrielGruber

Maybe this repo can help you!

JGabrielGruber avatar Apr 10 '19 23:04 JGabrielGruber