Generic OAuth2 authorization inside view-based decorator/middleware?

Etienne Robillard tkadm30 at yandex.com
Fri Jul 13 04:36:04 EDT 2018


Hi!

This is quick post about my current attempt to develop a specific 
GoogleController extension for django-hotsauce 0.9.6 in order to allow 
OAuth2 authentication and authorization on a regular Django view.

Code:

from notmm.controllers.wsgi import WSGIController, sessionmanager
from wsgi_oauth2 import client

__all__ = ['OAuthController']

class OAuthController(WSGIController):
     debug = True
     def __init__(self, **kwargs):
         super(OAuthController, self).__init__(**kwargs)

class GoogleController(OAuthController):
     scope = 'email'
     def __init__(self, request, **kwargs):
         super(GoogleController, self).__init__()
         self._request = request
         self._client = client.GoogleClient(
             self.settings.OAUTH2_CLIENT_ID,
             access_token=self.settings.OAUTH2_ACCESS_TOKEN,
             scope=self.scope,
             redirect_url=self.settings.OAUTH2_REDIRECT_URL,
             )


     def application(self, env, start_response):
         with sessionmanager(self.request):
	    # wsgi_oauth2 response middleware
             response = 
self._client.wsgi_middleware(self.get_response(request=self.request),
             secret=self.settings.SECRET_KEY,  			 
login_path=self.settings.OAUTH2_LOGIN_URL)
         return response(env, start_response)

Decorator:

from functools import wraps
from notmm.controllers.oauth import GoogleController
from notmm.utils.django_settings import LazySettings

_settings = LazySettings()

__all__ = ('require_oauth', 'with_schevo_database',)

def require_oauth(view_func):
     controller_class = GoogleController

     def decorator(view_func, **kwargs):
         def _wrapper(*args, **kwargs):
             req = args[0]
             wsgi_app = controller_class(req, **kwargs)
             # XXX this does not work :-)
	    response = wsgi_app.get_response(request=req)
             return response
         return wraps(view_func)(_wrapper, **kwargs)
     return decorator(view_func)

I would like to know what's wrong with the code above... :-)




Thank you,

Etienne



-- 
Etienne Robillard
tkadm30 at yandex.com
https://www.isotopesoftware.ca/


More information about the Python-list mailing list