def _auth_from_config(config): return Oauth2Authenticator( client_id=config["credentials"]["client_id"], client_secret=config["credentials"]["client_secret"], refresh_token=config["credentials"]["refresh_token"], token_refresh_endpoint=f"{SourceLeverHiring.URL_MAP_ACCORDING_ENVIRONMENT[config['environment']]['login']}oauth/token", )
def test_refresh_access_token(self, requests_mock): mock_refresh_token_call = requests_mock.post( TestOauth2Authenticator.refresh_endpoint, json={ "access_token": "token", "expires_in": 10 }) oauth = Oauth2Authenticator( TestOauth2Authenticator.refresh_endpoint, TestOauth2Authenticator.client_id, TestOauth2Authenticator.client_secret, TestOauth2Authenticator.refresh_token, refresh_access_token_headers=TestOauth2Authenticator. refresh_access_token_headers, ) token = oauth.refresh_access_token() assert ("token", 10) == token for header in self.refresh_access_token_headers: assert header in mock_refresh_token_call.last_request.headers assert self.refresh_access_token_headers[ header] == mock_refresh_token_call.last_request.headers[header] assert mock_refresh_token_call.called
def get_stream_kwargs(config: Mapping[str, Any]) -> Mapping[str, Any]: authorization = config.get("authorization", {}) stream_kwargs = dict() auth_type = authorization.get("auth_type") if auth_type == "Client": stream_kwargs["authenticator"] = Oauth2Authenticator( token_refresh_endpoint= "https://oauth.pipedrive.com/oauth/token", client_secret=authorization.get("client_secret"), client_id=authorization.get("client_id"), refresh_token=authorization.get("refresh_token"), ) elif auth_type == "Token": stream_kwargs["authenticator"] = { "api_token": authorization.get("api_token") } # backward compatibility else: if config.get("api_token"): stream_kwargs["authenticator"] = { "api_token": config.get("api_token") } else: raise Exception(f"Invalid auth type: {auth_type}") return stream_kwargs
def streams(self, config: Mapping[str, Any]) -> List[Stream]: authenticator = Oauth2Authenticator( token_refresh_endpoint= f"{self.URL_MAP_ACCORDING_ENVIRONMENT[config['environment']]['login']}oauth/token", client_id=config["client_id"], client_secret=config["client_secret"], refresh_token=config["refresh_token"], ) full_refresh_params = { "authenticator": authenticator, "base_url": self.URL_MAP_ACCORDING_ENVIRONMENT[config["environment"]]["api"] } stream_params_with_start_date = { **full_refresh_params, "start_date": config["start_date"] } return [ Applications(**stream_params_with_start_date), Interviews(**stream_params_with_start_date), Notes(**stream_params_with_start_date), Offers(**stream_params_with_start_date), Opportunities(**stream_params_with_start_date), Referrals(**stream_params_with_start_date), Users(**full_refresh_params), ]
def get_stream_kwargs(config: Mapping[str, Any]) -> Mapping[str, Any]: authorization = config.get("authorization", {}) stream_kwargs = { "site_urls": config.get("site_urls"), "start_date": config.get("start_date"), "end_date": config.get("end_date") or pendulum.now().to_date_string(), } auth_type = authorization.get("auth_type") if auth_type == "Client": stream_kwargs["authenticator"] = Oauth2Authenticator( token_refresh_endpoint="https://oauth2.googleapis.com/token", client_secret=authorization.get("client_secret"), client_id=authorization.get("client_id"), refresh_token=authorization.get("refresh_token"), ) elif auth_type == "Service": stream_kwargs["authenticator"] = ServiceAccountAuthenticator( service_account_info=json.loads(authorization.get("service_account_info")), email=authorization.get("email") ) else: raise Exception(f"Invalid auth type: {auth_type}") return stream_kwargs
def _make_authenticator(config: Mapping[str, Any]): return Oauth2Authenticator( token_refresh_endpoint=TOKEN_URL, client_id=config["client_id"], client_secret=config["client_secret"], refresh_token=config["refresh_token"], )
def _make_authenticator(config: AmazonAdsConfig): return Oauth2Authenticator( token_refresh_endpoint=TOKEN_URL, client_id=config.client_id, client_secret=config.client_secret, refresh_token=config.refresh_token, scopes=[config.scope], )
def test_refresh_access_authenticator(self): oauth = Oauth2Authenticator( TestOauth2Authenticator.refresh_endpoint, TestOauth2Authenticator.client_id, TestOauth2Authenticator.client_secret, TestOauth2Authenticator.refresh_token, refresh_access_token_authenticator=TestOauth2Authenticator.refresh_access_token_authenticator, ) expected_headers = {"Authorization": "Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ="} assert expected_headers == oauth.get_refresh_access_token_headers()
def check_connection(self, logger, config: Mapping[str, Any]) -> Tuple[bool, any]: authenticator = Oauth2Authenticator( token_refresh_endpoint= f"{self.URL_MAP_ACCORDING_ENVIRONMENT[config['environment']]['login']}oauth/token", client_id=config["client_id"], client_secret=config["client_secret"], refresh_token=config["refresh_token"], ) _ = authenticator.get_auth_header() return True, None
def get_authenticator(config): user_pass = (config.get("client_id") + ":" + config.get("client_secret")).encode("ascii") auth = "Basic " + standard_b64encode(user_pass).decode("ascii") return Oauth2Authenticator( token_refresh_endpoint=f"{PinterestStream.url_base}oauth/token", client_secret=config.get("client_secret"), client_id=config.get("client_id"), refresh_access_token_headers={"Authorization": auth}, refresh_token=config.get("refresh_token"), )
def test_get_auth_header_fresh(self, mocker): """ Should not retrieve new token if current token is valid. """ oauth = Oauth2Authenticator( TestOauth2Authenticator.refresh_endpoint, TestOauth2Authenticator.client_id, TestOauth2Authenticator.client_secret, 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_refresh_access_token(self, mocker): oauth = Oauth2Authenticator( TestOauth2Authenticator.refresh_endpoint, TestOauth2Authenticator.client_id, TestOauth2Authenticator.client_secret, 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 get_authenticator(config): credentials = config.get("credentials", {}) if credentials and "client_id" in credentials: return Oauth2Authenticator( token_refresh_endpoint= "https://app.retently.com/api/oauth/token", client_id=credentials["client_id"], client_secret=credentials["client_secret"], refresh_token=credentials["refresh_token"], ) api_key = credentials.get("api_key", config.get("api_key")) if not api_key: raise Exception( "Config validation error: 'api_key' is a required property") auth_method = f"api_key={api_key}" return TokenAuthenticator(token="", auth_method=auth_method)
def get_authenticator(config): # backwards compatibility, credentials_json used to be in the top level of the connector if config.get("credentials_json"): return GoogleAnalyticsServiceOauth2Authenticator(config) auth_params = config.get("credentials") if auth_params.get("auth_type") == "Service" or auth_params.get("credentials_json"): return GoogleAnalyticsServiceOauth2Authenticator(auth_params) else: return Oauth2Authenticator( token_refresh_endpoint="https://oauth2.googleapis.com/token", client_secret=auth_params.get("client_secret"), client_id=auth_params.get("client_id"), refresh_token=auth_params.get("refresh_token"), scopes=["https://www.googleapis.com/auth/analytics.readonly"], )
def test_get_auth_header_expired(self, mocker): """ Should retrieve new token if current token is expired. """ oauth = Oauth2Authenticator( TestOauth2Authenticator.refresh_endpoint, TestOauth2Authenticator.client_id, TestOauth2Authenticator.client_secret, 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_request_body(self): """ Request body should match given configuration. """ scopes = ["scope1", "scope2"] oauth = Oauth2Authenticator( TestOauth2Authenticator.refresh_endpoint, TestOauth2Authenticator.client_id, TestOauth2Authenticator.client_secret, TestOauth2Authenticator.refresh_token, 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
def get_authenticator(cls, config: Mapping[str, Any]) -> TokenAuthenticator: """ Validate input parameters and generate a necessary Authentication object This connectors support 2 auth methods: 1) direct access token with TTL = 2 months 2) refresh token (TTL = 1 year) which can be converted to access tokens, Every new refresh revokes all previous access tokens """ auth_method = config.get("credentials", {}).get("auth_method") if not auth_method or auth_method == "access_token": # support of backward compatibility with old exists configs access_token = config["credentials"][ "access_token"] if auth_method else config["access_token"] return TokenAuthenticator(token=access_token) elif auth_method == "oAuth2.0": return Oauth2Authenticator( token_refresh_endpoint= "https://www.linkedin.com/oauth/v2/accessToken", client_id=config["credentials"]["client_id"], client_secret=config["credentials"]["client_secret"], refresh_token=config["credentials"]["refresh_token"], ) raise Exception("incorrect input parameters")