/** * Retrieve access token, if needed, retrieve instance url, and set server host URL * {@inheritDoc} * @see org.apache.gobblin.writer.http.HttpWriter#onConnect(org.apache.http.HttpHost) */ @Override public void onConnect(URI serverHost) throws IOException { if (!StringUtils.isEmpty(accessToken)) { return; //No need to be called if accessToken is active. } try { getLog().info("Getting Oauth2 access token."); OAuthClientRequest request = OAuthClientRequest.tokenLocation(serverHost.toString()) .setGrantType(GrantType.PASSWORD) .setClientId(clientId) .setClientSecret(clientSecret) .setUsername(userId) .setPassword(password + securityToken).buildQueryMessage(); OAuthClient client = new OAuthClient(new URLConnectionClient()); OAuthJSONAccessTokenResponse response = client.accessToken(request, OAuth.HttpMethod.POST); accessToken = response.getAccessToken(); setCurServerHost(new URI(response.getParam("instance_url"))); } catch (OAuthProblemException e) { throw new NonTransientException("Error while authenticating with Oauth2", e); } catch (OAuthSystemException e) { throw new RuntimeException("Failed getting access token", e); } catch (URISyntaxException e) { throw new RuntimeException("Failed due to invalid instance url", e); } }
.setClientId(id) .setClientSecret(secret) .setRedirectURI(redirectUrl)
private OAuthClientRequest getAccessRequest(String tokenEndPoint, String clientId, String code, String clientSecret, String callbackurl) throws AuthenticationFailedException { OAuthClientRequest accessRequest = null; try { accessRequest = OAuthClientRequest.tokenLocation(tokenEndPoint) .setGrantType(GrantType.AUTHORIZATION_CODE).setClientId(clientId) .setClientSecret(clientSecret).setRedirectURI(callbackurl).setCode(code) .buildBodyMessage(); } catch (OAuthSystemException e) { if (log.isDebugEnabled()) { log.debug("Exception while building request for request access token", e); } throw new AuthenticationFailedException(e.getMessage(), e); } return accessRequest; }
/** * Helper method to configure the oauth accessCode/implicit flow parameters * @param clientId Client ID * @param clientSecret Client secret * @param redirectURI Redirect URI * @return ApiClient */ public ApiClient configureAuthorizationFlow(String clientId, String clientSecret, String redirectURI) { for(Interceptor apiAuthorization : apiAuthorizations.values()) { if (apiAuthorization instanceof OAuth) { OAuth oauth = (OAuth) apiAuthorization; oauth.getTokenRequestBuilder() .setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(redirectURI); oauth.getAuthenticationRequestBuilder() .setClientId(clientId) .setRedirectURI(redirectURI); return this; } } return this; }
/** * Helper method to configure the oauth accessCode/implicit flow parameters * @param clientId Client ID * @param clientSecret Client secret * @param redirectURI Redirect URI * @return ApiClient */ public ApiClient configureAuthorizationFlow(String clientId, String clientSecret, String redirectURI) { for(Interceptor apiAuthorization : apiAuthorizations.values()) { if (apiAuthorization instanceof OAuth) { OAuth oauth = (OAuth) apiAuthorization; oauth.getTokenRequestBuilder() .setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(redirectURI); oauth.getAuthenticationRequestBuilder() .setClientId(clientId) .setRedirectURI(redirectURI); return this; } } return this; }
/** * Helper method to configure the oauth accessCode/implicit flow parameters * @param clientId Client ID * @param clientSecret Client secret * @param redirectURI Redirect URI * @return ApiClient */ public ApiClient configureAuthorizationFlow(String clientId, String clientSecret, String redirectURI) { for(Interceptor apiAuthorization : apiAuthorizations.values()) { if (apiAuthorization instanceof OAuth) { OAuth oauth = (OAuth) apiAuthorization; oauth.getTokenRequestBuilder() .setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(redirectURI); oauth.getAuthenticationRequestBuilder() .setClientId(clientId) .setRedirectURI(redirectURI); return this; } } return this; }
/** * Helper method to configure the oauth accessCode/implicit flow parameters * @param clientId Client ID * @param clientSecret Client secret * @param redirectURI Redirect URI * @return ApiClient */ public ApiClient configureAuthorizationFlow(String clientId, String clientSecret, String redirectURI) { for(Interceptor apiAuthorization : apiAuthorizations.values()) { if (apiAuthorization instanceof OAuth) { OAuth oauth = (OAuth) apiAuthorization; oauth.getTokenRequestBuilder() .setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(redirectURI); oauth.getAuthenticationRequestBuilder() .setClientId(clientId) .setRedirectURI(redirectURI); return this; } } return this; }
/** * Helper method to configure the OAuth accessCode/implicit flow parameters * @param clientId OAuth2 client ID * @param clientSecret OAuth2 client secret * @param redirectURI OAuth2 redirect uri */ public void configureAuthorizationFlow(String clientId, String clientSecret, String redirectURI) { for(Authentication auth : authentications.values()) { if (auth instanceof OAuth) { OAuth oauth = (OAuth) auth; oauth.getTokenRequestBuilder() .setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(redirectURI); oauth.getAuthenticationRequestBuilder() .setClientId(clientId) .setRedirectURI(redirectURI); return; } } }
@Override public Optional<AccessTokenResponse> getAccessToken(final HttpServletRequest req, final String redirectUri) { try { OAuthAuthzResponse oar = OAuthAuthzResponse.oauthCodeAuthzResponse(req); OAuthClientRequest request = OAuthClientRequest .tokenLocation(tokenEndpointURL) .setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(redirectUri) .setGrantType(GrantType.AUTHORIZATION_CODE) .setCode(oar.getCode()) .buildBodyMessage(); OAuthClient client = new OAuthClient(new URLConnectionClient()); return Optional.of(client.accessToken(request, OltuAccessTokenResponse.class)); } catch (OAuthSystemException | OAuthProblemException e) { LOGGER.error("Authentication failed.", e); return Optional.empty(); } }
/** * Helper constructor for single password oauth2 * @param authName Authentication name * @param clientId Client ID * @param secret Client Secret * @param username Username * @param password Password */ public ApiClient(String authName, String clientId, String secret, String username, String password) { this(authName); this.getTokenEndPoint() .setClientId(clientId) .setClientSecret(secret) .setUsername(username) .setPassword(password); }
/** * Helper constructor for single password oauth2 * @param authName Authentication name * @param clientId Client ID * @param secret Client Secret * @param username Username * @param password Password */ public ApiClient(String authName, String clientId, String secret, String username, String password) { this(authName); this.getTokenEndPoint() .setClientId(clientId) .setClientSecret(secret) .setUsername(username) .setPassword(password); }
/** * Helper constructor for single password oauth2 * @param authName Authentication name * @param clientId Client ID * @param secret Client Secret * @param username Username * @param password Password */ public ApiClient(String authName, String clientId, String secret, String username, String password) { this(authName); this.getTokenEndPoint() .setClientId(clientId) .setClientSecret(secret) .setUsername(username) .setPassword(password); }
/** * Helper constructor for single password oauth2 * @param authName Authentication name * @param clientId Client ID * @param secret Client Secret * @param username Username * @param password Password */ public ApiClient(String authName, String clientId, String secret, String username, String password) { this(authName); this.getTokenEndPoint() .setClientId(clientId) .setClientSecret(secret) .setUsername(username) .setPassword(password); }
/** * Helper constructor for single password oauth2 * @param authName Authentication name * @param clientId Client ID * @param secret Client Secret * @param username Username * @param password Password */ public ApiClient(String authName, String clientId, String secret, String username, String password) { this(authName); this.getTokenEndPoint() .setClientId(clientId) .setClientSecret(secret) .setUsername(username) .setPassword(password); }
/** * Helper constructor for single password oauth2 * @param authName Authentication name * @param clientId Client ID * @param secret Client Secret * @param username Username * @param password Password */ public ApiClient(String authName, String clientId, String secret, String username, String password) { this(authName); this.getTokenEndPoint() .setClientId(clientId) .setClientSecret(secret) .setUsername(username) .setPassword(password); }
/** * Helper constructor for single password oauth2 * @param authName Authentication name * @param clientId Client ID * @param secret Client Secret * @param username Username * @param password Password */ public ApiClient(String authName, String clientId, String secret, String username, String password) { this(authName); this.getTokenEndPoint() .setClientId(clientId) .setClientSecret(secret) .setUsername(username) .setPassword(password); }
private OAuthClientRequest buidTokenRequest( String tokenEndPoint, String clientId, String clientSecret, String callbackurl, String code) throws ApplicationAuthenticatorException { OAuthClientRequest tokenRequest = null; try { tokenRequest = OAuthClientRequest.tokenLocation(tokenEndPoint).setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(callbackurl).setCode(code) .buildQueryMessage(); } catch (OAuthSystemException e) { throw new ApplicationAuthenticatorException("Exception while building access token request.", e); } return tokenRequest; }
private OAuthClientRequest buidTokenRequest( String tokenEndPoint, String clientId, String clientSecret, String callbackurl, String code) throws ApplicationAuthenticatorException { OAuthClientRequest tokenRequest = null; try { tokenRequest = OAuthClientRequest.tokenLocation(tokenEndPoint).setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(callbackurl).setCode(code) .buildQueryMessage(); } catch (OAuthSystemException e) { throw new ApplicationAuthenticatorException("Exception while building access token request.", e); } return tokenRequest; }
protected OAuthClientRequest buidTokenRequest( String tokenEndPoint, String clientId, String clientSecret, String callbackurl, String code) throws ApplicationAuthenticatorException { OAuthClientRequest tokenRequest = null; try { tokenRequest = OAuthClientRequest.tokenLocation(tokenEndPoint).setClientId(clientId) .setClientSecret(clientSecret) .setRedirectURI(callbackurl).setCode(code) .buildQueryMessage(); } catch (OAuthSystemException e) { throw new ApplicationAuthenticatorException("Exception while building access token request.", e); } return tokenRequest; }
/** * Helper constructor for OAuth2 * @param oAuthBasePath The API base path * @param authName the authentication method name ("oauth" or "api_key") * @param clientId OAuth2 Client ID * @param secret OAuth2 Client secret */ public ApiClient(String oAuthBasePath, String authName, String clientId, String secret) { this(oAuthBasePath, authName); this.getTokenEndPoint() .setClientId(clientId) .setClientSecret(secret); }