def parse_webform_return_url(return_url, request): """ Given a webform url and request containing authentication information extract authentication data encoded in the url and validate using either this data or data in the request. Construct a proper return URL, which has stripped the authentication data, to return the user. """ jwt_param = None url = urlparse(return_url) try: # get jwt from url - probably zebra via enketo jwt_param = filter( lambda p: p.startswith('jwt'), url.query.split('&')) jwt_param = jwt_param and jwt_param[0].split('=')[1] if not jwt_param: return except IndexError: pass if '/_/' in return_url: # offline url redirect_url = "%s://%s%s#%s" % ( url.scheme, url.netloc, url.path, url.fragment) elif '/::' in return_url: # non-offline url redirect_url = "%s://%s%s" % (url.scheme, url.netloc, url.path) else: # unexpected format return response_redirect = HttpResponseRedirect(redirect_url) # if the requesting user is not authenticated but the token has been # retrieved from the url - probably zebra via enketo express - use the # token to create signed cookies which will be used by subsequent # enketo calls to authenticate the user if jwt_param: if request.user.is_anonymous(): api_token = authentication.get_api_token(jwt_param) if getattr(api_token, 'user'): username = api_token.user.username else: username = request.user.username response_redirect = set_enketo_signed_cookies( response_redirect, username=username, json_web_token=jwt_param) return response_redirect
def parse_webform_return_url(return_url, request): """ Given a webform url and request containing authentication information extract authentication data encoded in the url and validate using either this data or data in the request. Construct a proper return URL, which has stripped the authentication data, to return the user. """ jwt_param = None url = urlparse(return_url) try: # get jwt from url - probably zebra via enketo jwt_param = [p for p in url.query.split('&') if p.startswith('jwt')] jwt_param = jwt_param and jwt_param[0].split('=')[1] if not jwt_param: return except IndexError: pass if '/_/' in return_url: # offline url redirect_url = "%s://%s%s#%s" % ( url.scheme, url.netloc, url.path, url.fragment) elif '/::' in return_url: # non-offline url redirect_url = "%s://%s%s" % (url.scheme, url.netloc, url.path) else: # unexpected format return response_redirect = HttpResponseRedirect(redirect_url) # if the requesting user is not authenticated but the token has been # retrieved from the url - probably zebra via enketo express - use the # token to create signed cookies which will be used by subsequent # enketo calls to authenticate the user if jwt_param: if request.user.is_anonymous: api_token = authentication.get_api_token(jwt_param) if getattr(api_token, 'user'): username = api_token.user.username else: username = request.user.username response_redirect = set_enketo_signed_cookies( response_redirect, username=username, json_web_token=jwt_param) return response_redirect