def call_api(self, _call_method, _session_id="", _call_args=None): """Call YourMembership API.""" if _call_args is None: _call_args = {} data = self.generate_request_xml( _call_method, _session_id=_session_id, _call_args=_call_args) # Get the data to post headers = { 'Content-Type': 'application/x-www-form-urlencoded' } # Use this content type response = request("POST", self.YMAPI_SERVLET_URI, headers=headers, data=data) # Send the request xmldoc = ElementTree.fromstring(response.text) errcode = int(xmldoc.findall("./ErrCode")[0].text) ExtendedErrorInfo = xmldoc.findall("./ExtendedErrorInfo") errdesc = xmldoc.findall("./ErrDesc") if errcode == 0: ym_response = xmldoc.findall("./" + _call_method)[0] return_dict = {"call_method": _call_method} for node in ym_response: return_dict[node.tag] = node.text return return_dict elif errcode == 999: raise AuthUnknownError(self, errdesc[0].text) elif errcode in [101, 102, 103, 201, 301, 404]: raise AuthMissingParameter(self, errdesc[0].text) elif errcode in [405]: raise AuthStateForbidden(self, errdesc[0].text) else: raise AuthUnknownError(self, errdesc[0].text)
def auth_complete(self, *args, **kwargs): """Completes logging process, must return user instance.""" self.process_error(self.data) try: response = self.request_access_token( self.ACCESS_TOKEN_URL, params={ 'token': self.data.get("token"), 'secret': self.setting('SECRET') }, data=self.auth_complete_params(self.validate_state()), headers=self.auth_headers(), method=self.ACCESS_TOKEN_METHOD) except HTTPError as err: if err.response.status_code == 400: raise AuthCanceled(self) else: raise except KeyError: raise AuthUnknownError(self) self.process_error(response) return self.do_auth(response['access_token'], response=response, *args, **kwargs)
def fetch_certificate(self, cert_id): url = self.cert_url(cert_id) cert_data = self.request(url) if cert_data.status_code != 200: raise AuthUnknownError(self, "Failed to fetch certificate") return cert_data.text
def process_error(self, data): if not data: raise AuthException(self, 'OpenID relying party endpoint') elif data.status == FAILURE: raise AuthFailed(self, data.message) elif data.status == CANCEL: raise AuthCanceled(self) elif data.status != SUCCESS: raise AuthUnknownError(self, data.status)
def process_error(self, data): error = data.get('error') if error: if error == 'access_denied': raise AuthCanceled(self) else: raise AuthUnknownError(self, 'Jawbone error was {0}'.format(error)) return super(JawboneOAuth2, self).process_error(data)
def auth_complete(self, *args, **kwargs): self.process_error(self.data) try: response = self.check_signature( self.dstud_url, data=self.auth_complete_params(self.validate_state()), ) except HTTPError as err: if err.response.status_code in [403, 404]: raise AuthCanceled(self) else: raise except Exception as ex: raise AuthUnknownError(self) if response is None: raise AuthUnknownError(self) return self.do_auth(response)
def auth_complete(self, *args, **kwargs): """Complete auth process""" response = self.consumer().complete( dict(self.data.items()), self.strategy.absolute_uri(self.redirect_uri)) if not response: raise AuthException(self, 'OpenID relying party endpoint') elif response.status == SUCCESS: kwargs.update({'response': response, 'backend': self}) return self.strategy.authenticate(*args, **kwargs) elif response.status == FAILURE: raise AuthFailed(self, response.message) elif response.status == CANCEL: raise AuthCanceled(self) else: raise AuthUnknownError(self, response.status)
def do_auth(self, access_token, response=None, *args, **kwargs): response = response or {} data = self.user_data(access_token) if not isinstance(data, dict): # From time to time Facebook responds back a JSON with just # False as value, the reason is still unknown, but since the # data is needed (it contains the user ID used to identify the # account on further logins), this app cannot allow it to # continue with the auth process. raise AuthUnknownError(self, 'An error ocurred while retrieving ' 'users Facebook data') data['access_token'] = access_token if 'expires' in response: data['expires'] = response['expires'] kwargs.update({'backend': self, 'response': data}) return self.strategy.authenticate(*args, **kwargs)
def auth_complete(self, *args, **kwargs): """Completes loging process, must return user instance""" state = self.validate_state() self.process_error(self.data) try: response = self.request_access_token( self.access_token_url(), data=self.auth_complete_params(state), headers=self.auth_headers(), method=self.ACCESS_TOKEN_METHOD) except HTTPError as err: if err.response.status_code == 400: raise AuthCanceled(self) else: raise except KeyError: raise AuthUnknownError(self) self.process_error(response) return self.do_auth(response['access_token'], response=response, *args, **kwargs)
def process_error(self, data): if 'oauth_problem' in data: if data['oauth_problem'] == 'user_refused': raise AuthCanceled(self, 'User refused the access') raise AuthUnknownError(self, 'Error was ' + data['oauth_problem'])
class AuthUnknownErrorTest(BaseExceptionTestCase): exception = AuthUnknownError('foobar', 'some error') expected_message = 'An unknown error happened while ' \ 'authenticating some error'