/** * 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); } }
public static String getLoginURL(Req req, OAuthProvider provider, String oauthDomain) { if (OAUTH.isEmpty()) { Log.warn("OAuth is not configured!"); } String name = provider.getName().toLowerCase(); Config providerConfig = OAUTH.sub(name); Value<String> clientId = providerConfig.entry("clientId").str(); Value<String> clientSecret = providerConfig.entry("clientSecret").str(); String callbackPath = Msc.specialUri(name + "OauthCallback"); boolean popup = req.param("popup", null) != null; String redirectUrl = U.notEmpty(oauthDomain) ? oauthDomain + callbackPath : HttpUtils.constructUrl(req, callbackPath); String statePrefix = popup ? "P" : "N"; String state = statePrefix + STATE_CHECK.generateState(clientSecret, req.sessionId()); try { OAuthClientRequest request = OAuthClientRequest.authorizationLocation(provider.getAuthEndpoint()) .setClientId(clientId.str().get()).setRedirectURI(redirectUrl).setScope(provider.getEmailScope()) .setState(state).setResponseType("code").buildQueryMessage(); return request.getLocationUri(); } catch (OAuthSystemException e) { throw U.rte(e); } }
String redirectUrl = U.notEmpty(domain) ? domain + callbackPath : HttpUtils.constructUrl(req, callbackPath); TokenRequestBuilder reqBuilder = OAuthClientRequest.tokenLocation(provider.getTokenEndpoint()) .setGrantType(GrantType.AUTHORIZATION_CODE) .setClientId(id) .setClientSecret(secret) .setRedirectURI(redirectUrl) .setCode(code); OAuthClientRequest request = paramsInBody() ? reqBuilder.buildBodyMessage() : reqBuilder.buildBodyMessage(); OAuthClientRequest bearerClientRequest = new OAuthBearerClientRequest(profileUrl).setAccessToken( accessToken).buildQueryMessage();
public OAuth(OAuthFlow flow, String authorizationUrl, String tokenUrl, String scopes) { this(OAuthClientRequest.tokenLocation(tokenUrl).setScope(scopes)); setFlow(flow); authenticationRequestBuilder = OAuthClientRequest.authorizationLocation(authorizationUrl); }
public String generateForwardUrl(String registrationEndpoint, String authorizeUrl, String returnUrl) throws ServerException, UserException { try (DatabaseSession session = getBimServer().getDatabase().createSession()) { OAuthServer oAuthServer = session.querySingle(StorePackage.eINSTANCE.getOAuthServer_RegistrationEndpoint(), registrationEndpoint); if (oAuthServer == null) { throw new UserException("Application not registered"); } OAuthClientRequest request2 = OAuthClientRequest.authorizationLocation(authorizeUrl).setParameter("auth_type", "service").setClientId(oAuthServer.getClientId()).setRedirectURI(returnUrl).setResponseType(ResponseType.CODE.toString()).setState("state").buildQueryMessage(); return request2.getLocationUri(); } catch (Exception e) { return handleException(e); } }
public OAuthClientRequest createRequest(){ try { return OAuthClientRequest.authorizationProvider(providerType).setClientId(clientId).setResponseType(OAuth.OAUTH_CODE) .setScope("email"). setRedirectURI(redirectUrl).buildQueryMessage(); } catch (OAuthSystemException e) { throw new RuntimeException(e); } } public String authorize(String code){
int responseCode; try { URL url = new URL(request.getLocationUri()); if (request.getHeaders() != null) { for (Map.Entry<String, String> header : request.getHeaders().entrySet()) { httpURLConnection.addRequestProperty(header.getKey(), header.getValue());
private void setRequestBody(OAuthClientRequest request, String requestMethod, HttpURLConnection httpURLConnection) throws IOException { String requestBody = request.getBody(); if (OAuthUtils.isEmpty(requestBody)) { return; } if (OAuth.HttpMethod.POST.equals(requestMethod) || OAuth.HttpMethod.PUT.equals(requestMethod)) { httpURLConnection.setDoOutput(true); OutputStream ost = httpURLConnection.getOutputStream(); PrintWriter pw = new PrintWriter(ost); pw.print(requestBody); pw.flush(); pw.close(); } }
public OAuthClientRequest buildHeaderMessage() throws OAuthSystemException { OAuthClientRequest request = new OAuthClientRequest(url); this.applier = new ClientHeaderParametersApplier(); return (OAuthClientRequest)applier.applyOAuthParameters(request, parameters); } }
/** * Add a user agent to the OAuth request. * * @param request OAuth request */ private void addUserAgent(OAuthClientRequest request) { request.addHeader(HEADER_USER_AGENT, userAgent); }
public static AuthenticationRequestBuilder authorizationLocation(String url) { return new AuthenticationRequestBuilder(url); }
public static TokenRequestBuilder tokenLocation(String url) { return new TokenRequestBuilder(url); }
public OAuth(OAuthFlow flow, String authorizationUrl, String tokenUrl, String scopes) { this(OAuthClientRequest.tokenLocation(tokenUrl).setScope(scopes)); setFlow(flow); authenticationRequestBuilder = OAuthClientRequest.authorizationLocation(authorizationUrl); }
public OAuthClientRequest buildHeaderMessage() throws OAuthSystemException { OAuthClientRequest request = new OAuthClientRequest(url); this.applier = new ClientHeaderParametersApplier(); return (OAuthClientRequest)applier.applyOAuthParameters(request, parameters); } }
public static AuthenticationRequestBuilder authorizationLocation(String url) { return new AuthenticationRequestBuilder(url); }
public OAuth(OAuthFlow flow, String authorizationUrl, String tokenUrl, String scopes) { this(OAuthClientRequest.tokenLocation(tokenUrl).setScope(scopes)); setFlow(flow); authenticationRequestBuilder = OAuthClientRequest.authorizationLocation(authorizationUrl); }
public OAuth(OAuthFlow flow, String authorizationUrl, String tokenUrl, String scopes) { this(OAuthClientRequest.tokenLocation(tokenUrl).setScope(scopes)); setFlow(flow); authenticationRequestBuilder = OAuthClientRequest.authorizationLocation(authorizationUrl); }
public OAuth(OAuthFlow flow, String authorizationUrl, String tokenUrl, String scopes) { this(OAuthClientRequest.tokenLocation(tokenUrl).setScope(scopes)); setFlow(flow); authenticationRequestBuilder = OAuthClientRequest.authorizationLocation(authorizationUrl); }
public OAuth(OAuthFlow flow, String authorizationUrl, String tokenUrl, String scopes) { this(OAuthClientRequest.tokenLocation(tokenUrl).setScope(scopes)); setFlow(flow); authenticationRequestBuilder = OAuthClientRequest.authorizationLocation(authorizationUrl); }
public OAuth(OAuthFlow flow, String authorizationUrl, String tokenUrl, String scopes) { this(OAuthClientRequest.tokenLocation(tokenUrl).setScope(scopes)); setFlow(flow); authenticationRequestBuilder = OAuthClientRequest.authorizationLocation(authorizationUrl); }