Пример #1
0
 def _check_response(self, response):
     msg = None
     if response.status_code == 200:
         return
     try:
         as_json = response.json()
         msg = as_json.get("message", None)
         raise AuthenticationException(response, msg)
     # msg is None if response is not JSON, but it's fine.
     except Exception:
         msg = "Request failed with status %i" % response.status_code
         raise AuthenticationException(response, msg=msg)
Пример #2
0
 async def _ensure_success_response(self, response):
     msg = None
     if response.status == 200:
         return
     try:
         as_json = await response.json()
         msg = as_json.get('message', None)
     # msg is None if response is not JSON, but it's fine.
     except:
         raise AuthenticationException(
             response, 'Request failed with status %i' % response.status)
     raise AuthenticationException(response, msg)
Пример #3
0
    def authenticate(self):
        if logger.isEnabledFor(logging.DEBUG):
            logger.debug("xauth authentication of user %s", user)
        if not self.server.is_xauth():
            raise AuthenticationException(
                "XAuth is not configured. Missing consumer key and/or secret.")
        # todo use xauth server key/secret
        client = self.client_class(self.consumer_key, self.consumer_secret)
        params = {}
        params["x_auth_username"] = self.xauth_credentials.user
        params["x_auth_password"] = self.xauth_credentials.password
        params["x_auth_mode"] = "client_auth"

        # avoid dumping password in log files.
        logging.disable(logging.DEBUG)

        uri, headers, body = client.sign(
            self.server.access_token_endpoint,
            "POST",
            headers=self.DEFAULT_CONTENT_TYPE,
            body=params,
        )

        response = self.http_session.post(uri,
                                          headers=headers,
                                          data=body,
                                          verify=self.verify)

        logging.disable(logging.NOTSET)

        self._check_response(response)
        self._update_token_from_request_body(response.content)
        self._update_client()
Пример #4
0
 def __init__(
     self,
     userid_password,
     http_session,
     server,
     consumer_key,
     consumer_secret,
     signature_method=None,
     client_class=oauth.Client,
 ):
     super().__init__(
         http_session,
         server,
         server.xauth_consumer_key,
         server.xauth_consumer_secret,
         signature_method=signature_method,
         client_class=client_class,
     )
     if not self.server.is_xauth():
         raise AuthenticationException(
             "XAuth is not configured for this server. Missing xauth consumer key and/or secret."
         )
     if isinstance(userid_password, tuple) and len(userid_password) == 2:
         self.xauth_credentials = UserIDPassword(*userid_password)
     elif isinstance(userid_password, UserIDPassword):
         self.xauth_credentials = userid_password
     else:
         raise ValueError(
             "User ID and password must be specified as a tuple or a UserIDPassword instance."
         )
    def _authenticate(self):
        """Authenticate with the server using the credentials.

        This method supports both oauth and xauth methods. It is not necessary
        to call it, since the session will try to authenticate when the first
        request is issued. """
        logger.info("Authenticating to the server.")
        if self.credentials is None:
            raise AuthenticationException("Missing credentials.")
        if self.credentials.is_xauth:
            self.oauth_session = self.xauth_session_class(
                self.credentials,
                self.http_session,
                self.server,
                self.server.xauth_consumer_key,
                self.server.xauth_consumer_secret,
            )
        else:
            self.oauth_session = self.oauth_session_class(
                self.http_session,
                self.server,
                self.credentials.consumer_key,
                self.credentials.consumer_secret,
            )
        self.oauth_session.authenticate()