class DelegatedOAuthMiddleware(object): def __init__(self): if settings.OAUTH_ENABLED: self.is_oauth_enabled = True logger.info("clientid = %s" % settings.OAUTH_CLIENT_ID) self.oauth = OAuth( key=settings.OAUTH_CLIENT_ID, secret=settings.OAUTH_CLIENT_SECRET, callback_url=settings.OAUTH_CALLBACK, domain=settings.OAUTH_DOMAIN, access_token_url=settings.OAUTH_ACCESS_TOKEN_URL, authorize_url=settings.OAUTH_AUTHORIZE_URL, scope=settings.OAUTH_DEFAULT_SCOPE) logger.info("Successfully created OAuth!") else: logger.info("OAuth is not enabled!") self.is_oauth_enabled = False def process_request(self, request): if request.path.startswith('/auth/'): logger.debug("Bypass OAuth redirect request " + request.path) return None if request.path.startswith('/health_check/'): logger.debug("Bypass health_check request " + request.path) return None if not self.is_oauth_enabled: anonymous = UserIdentity(name="anonymous") request.teletraan_user_id = anonymous return None # extract employee oauth token, redirect to OAuth if missing or invalid if self.oauth.validate_token(session=request.session): username = request.session.get('teletraan_user') token = request.session.get('oauth_token') userId = UserIdentity(name=username, token=token) request.teletraan_user_id = userId return None else: # TODO call logout to remove session cleanly # self.logout(request) data = {'origin_path': request.get_full_path()} url = self.oauth.get_authorization_url(session=request.session, data=data) logger.debug("Redirect oauth for authentication!, url = " + url) return HttpResponseRedirect(url) # TODO not currently used, need to add logout button on the UI and call this def logout(self, request): self.oauth.logout(session=request.session) if 'teletraan_user' in request.session: del request.session['teletraan_user'] return HttpResponseRedirect('/')
class DelegatedOAuthMiddleware(object): def __init__(self): if settings.OAUTH_ENABLED: self.is_oauth_enabled = True logger.info("clientid = %s" % settings.OAUTH_CLIENT_ID) self.oauth = OAuth( key=settings.OAUTH_CLIENT_ID, secret=settings.OAUTH_CLIENT_SECRET, callback_url=settings.OAUTH_CALLBACK, domain=settings.OAUTH_DOMAIN, access_token_url=settings.OAUTH_ACCESS_TOKEN_URL, authorize_url=settings.OAUTH_AUTHORIZE_URL, scope=settings.OAUTH_DEFAULT_SCOPE ) logger.info("Successfully created OAuth!") else: logger.info("OAuth is not enabled!") self.is_oauth_enabled = False def process_request(self, request): if request.path.startswith('/auth/'): logger.debug("Bypass OAuth redirect request " + request.path) return None if request.path.startswith('/health_check/'): logger.debug("Bypass health_check request " + request.path) return None if not self.is_oauth_enabled: anonymous = UserIdentity(name="anonymous") request.teletraan_user_id = anonymous return None # extract employee oauth token, redirect to OAuth if missing or invalid if self.oauth.validate_token(session=request.session): username = request.session.get('teletraan_user') token = request.session.get('oauth_token') userId = UserIdentity(name=username, token=token) request.teletraan_user_id = userId return None else: # TODO call logout to remove session cleanly # self.logout(request) data = {'origin_path': request.get_full_path()} url = self.oauth.get_authorization_url(session=request.session, data=data) logger.debug("Redirect oauth for authentication!, url = " + url) return HttpResponseRedirect(url) # TODO not currently used, need to add logout button on the UI and call this def logout(self, request): self.oauth.logout(session=request.session) if 'teletraan_user' in request.session: del request.session['teletraan_user'] return HttpResponseRedirect('/')