Beispiel #1
0
    def _get_authenticator(self, config: Mapping[str, Any]):
        # Added to maintain backward compatibility with previous versions
        if "api_token" in config:
            return TokenAuthenticator(config["api_token"])

        credentials = config.get("credentials")
        credentials_title = credentials.get("option_title")
        if credentials_title == "Default OAuth2.0 authorization":
            # We can get `refresh_token` only if the token rotation function is enabled for the Slack Oauth Application.
            # If it is disabled, then we use the generated `access_token`, which acts without expiration.
            # https://api.slack.com/authentication/rotation
            if credentials.get("refresh_token", "").strip():
                return Oauth2Authenticator(
                    token_refresh_endpoint=
                    "https://slack.com/api/oauth.v2.access",
                    client_id=credentials["client_id"],
                    client_secret=credentials["client_secret"],
                    refresh_token=credentials["refresh_token"],
                )
            return TokenAuthenticator(credentials["access_token"])
        elif credentials_title == "API Token Credentials":
            return TokenAuthenticator(credentials["api_token"])
        else:
            raise Exception(
                f"No supported option_title: {credentials_title} specified. See spec.json for references"
            )
 def test_refresh_request_body(self):
     """
     Request body should match given configuration.
     """
     scopes = ["scope1", "scope2"]
     oauth = Oauth2Authenticator(
         token_refresh_endpoint="refresh_end",
         client_id="some_client_id",
         client_secret="some_client_secret",
         refresh_token="some_refresh_token",
         scopes=["scope1", "scope2"],
         token_expiry_date=pendulum.now().add(days=3),
         refresh_request_body={"custom_field": "in_outbound_request", "another_field": "exists_in_body", "scopes": ["no_override"]},
     )
     body = oauth.get_refresh_request_body()
     expected = {
         "grant_type": "refresh_token",
         "client_id": "some_client_id",
         "client_secret": "some_client_secret",
         "refresh_token": "some_refresh_token",
         "scopes": scopes,
         "custom_field": "in_outbound_request",
         "another_field": "exists_in_body",
     }
     assert body == expected
Beispiel #3
0
 def get_oauth(self, config):
     return Oauth2Authenticator(
         token_refresh_endpoint="https://www.strava.com/oauth/token",
         client_id=config["client_id"],
         client_secret=config["client_secret"],
         refresh_token=config["refresh_token"],
         scopes=["read_all", "activity:read_all"],
     )
Beispiel #4
0
    def get_authenticator(config):
        credentials = config["credentials"]
        client_id = credentials["client_id"]
        client_secret = credentials["client_secret"]
        refresh_token = credentials["refresh_token"]

        return Oauth2Authenticator(
            token_refresh_endpoint="https://oauth2.googleapis.com/token",
            client_id=client_id,
            client_secret=client_secret,
            refresh_token=refresh_token,
        )
    def test_get_auth_header_fresh(self, mocker):
        """
        Should not retrieve new token if current token is valid.
        """
        oauth = Oauth2Authenticator(
            token_refresh_endpoint=TestOauth2Authenticator.refresh_endpoint,
            client_id=TestOauth2Authenticator.client_id,
            client_secret=TestOauth2Authenticator.client_secret,
            refresh_token=TestOauth2Authenticator.refresh_token,
        )

        mocker.patch.object(Oauth2Authenticator, "refresh_access_token", return_value=("access_token", 1000))
        header = oauth.get_auth_header()
        assert {"Authorization": "Bearer access_token"} == header
    def test_auth_call_method(self, mocker):
        oauth = Oauth2Authenticator(
            token_refresh_endpoint=TestOauth2Authenticator.refresh_endpoint,
            client_id=TestOauth2Authenticator.client_id,
            client_secret=TestOauth2Authenticator.client_secret,
            refresh_token=TestOauth2Authenticator.refresh_token,
        )

        mocker.patch.object(Oauth2Authenticator, "refresh_access_token", return_value=("access_token", 1000))
        prepared_request = requests.PreparedRequest()
        prepared_request.headers = {}
        oauth(prepared_request)

        assert {"Authorization": "Bearer access_token"} == prepared_request.headers
    def test_refresh_access_token(self, mocker):
        oauth = Oauth2Authenticator(
            token_refresh_endpoint="refresh_end",
            client_id="some_client_id",
            client_secret="some_client_secret",
            refresh_token="some_refresh_token",
            scopes=["scope1", "scope2"],
            token_expiry_date=pendulum.now().add(days=3),
            refresh_request_body={"custom_field": "in_outbound_request", "another_field": "exists_in_body", "scopes": ["no_override"]},
        )

        resp.status_code = 200
        mocker.patch.object(resp, "json", return_value={"access_token": "access_token", "expires_in": 1000})
        mocker.patch.object(requests, "request", side_effect=mock_request, autospec=True)
        token = oauth.refresh_access_token()

        assert ("access_token", 1000) == token
    def test_get_auth_header_expired(self, mocker):
        """
        Should retrieve new token if current token is expired.
        """
        oauth = Oauth2Authenticator(
            token_refresh_endpoint=TestOauth2Authenticator.refresh_endpoint,
            client_id=TestOauth2Authenticator.client_id,
            client_secret=TestOauth2Authenticator.client_secret,
            refresh_token=TestOauth2Authenticator.refresh_token,
        )

        expire_immediately = 0
        mocker.patch.object(Oauth2Authenticator, "refresh_access_token", return_value=("access_token_1", expire_immediately))
        oauth.get_auth_header()  # Set the first expired token.

        valid_100_secs = 100
        mocker.patch.object(Oauth2Authenticator, "refresh_access_token", return_value=("access_token_2", valid_100_secs))
        header = oauth.get_auth_header()
        assert {"Authorization": "Bearer access_token_2"} == header
    def test_refresh_access_token(self, mocker):
        oauth = Oauth2Authenticator(
            token_refresh_endpoint=TestOauth2Authenticator.refresh_endpoint,
            client_id=TestOauth2Authenticator.client_id,
            client_secret=TestOauth2Authenticator.client_secret,
            refresh_token=TestOauth2Authenticator.refresh_token,
        )
        resp = Response()
        resp.status_code = 200

        mocker.patch.object(requests, "request", return_value=resp)
        mocker.patch.object(resp,
                            "json",
                            return_value={
                                "access_token": "access_token",
                                "expires_in": 1000
                            })
        token = oauth.refresh_access_token()

        assert ("access_token", 1000) == token
 def test_refresh_request_body(self):
     """
     Request body should match given configuration.
     """
     scopes = ["scope1", "scope2"]
     oauth = Oauth2Authenticator(
         token_refresh_endpoint=TestOauth2Authenticator.refresh_endpoint,
         client_id=TestOauth2Authenticator.client_id,
         client_secret=TestOauth2Authenticator.client_secret,
         refresh_token=TestOauth2Authenticator.refresh_token,
         scopes=scopes,
     )
     body = oauth.get_refresh_request_body()
     expected = {
         "grant_type": "refresh_token",
         "client_id": "client_id",
         "client_secret": "client_secret",
         "refresh_token": "refresh_token",
         "scopes": scopes,
     }
     assert body == expected
Beispiel #11
0
    def __init__(self, credentials: Mapping[str, Any]):
        self._session = requests.Session()
        credentials_title = credentials.get("credentials_title")

        if credentials_title == "OAuth Credentials":
            self._session.auth = Oauth2Authenticator(
                token_refresh_endpoint=self.BASE_URL + "/oauth/v1/token",
                client_id=credentials["client_id"],
                client_secret=credentials["client_secret"],
                refresh_token=credentials["refresh_token"],
            )
        elif credentials_title == "API Key Credentials":
            self._session.params["hapikey"] = credentials.get("api_key")
        else:
            raise Exception(
                "No supported `credentials_title` specified. See spec.json for references"
            )

        self._session.headers = {
            "Content-Type": "application/json",
            "User-Agent": self.USER_AGENT,
        }