public static OAuthScope[] fromValues(String... values) { if (values == null || values.length == 0) { return new OAuthScope[0]; } final OAuthScope[] result = new OAuthScope[values.length]; int i = 0; for (String value : values) { result[i++] = fromValue(value); } return result; } }
@Override public void saveOAuthToken(OAuthToken newToken) { token = newToken; if (secureStorage != null) { secureStorage.saveOAuthToken(newToken); } } }
@Override public OAuthToken getOAuthToken() { // delegate only if token doesn't exist or has expired if (secureStorage != null && (token == null || token.getExpiryTime() < System.currentTimeMillis())) { token = secureStorage.getOAuthToken(); } return token; }
if (oAuthToken == null || oAuthToken.getExpiryTime() < currentTime) { LOG.info("OAuth token doesn't exist or has expired"); final OAuthSecureStorage secureStorage = oAuthParams.getSecureStorage(); if (secureStorage != null) { oAuthToken = secureStorage.getOAuthToken(); if (oAuthToken != null && oAuthToken.getExpiryTime() > currentTime) { return; if (oAuthParams.getUserPassword() == null || oAuthParams.getUserPassword().isEmpty()) { throw new IllegalArgumentException("Missing password for LinkedIn authorization"); final String refreshToken = getRefreshToken(); this.oAuthToken = getAccessToken(refreshToken); LOG.info("OAuth token created!"); secureStorage.saveOAuthToken(this.oAuthToken);
@Override protected Object doInvokeMethod(Map<String, Object> args) { try { return super.doInvokeMethod(args); } catch (RuntimeCamelException e) { if (e.getCause() instanceof WebApplicationException) { WebApplicationException cause = (WebApplicationException) e.getCause(); final Response response = cause.getResponse(); if (response.hasEntity()) { // try and convert it to LinkedInException final org.apache.camel.component.linkedin.api.model.Error error = response.readEntity(Error.class); throw new RuntimeCamelException( String.format("Error invoking %s: %s", method.getName(), error.getMessage()), new LinkedInException(error, response)); } } throw e; } } }
private OAuthToken getAccessToken(String refreshToken) throws IOException { final String tokenUrl = String.format(ACCESS_TOKEN_URL, refreshToken, oAuthParams.getRedirectUri(), oAuthParams.getClientId(), oAuthParams.getClientSecret()); final WebRequest webRequest = new WebRequest(new URL(tokenUrl), HttpMethod.POST); final WebResponse webResponse = webClient.loadWebResponse(webRequest); if (webResponse.getStatusCode() != HttpStatus.SC_OK) { throw new IOException(String.format("Error getting access token: [%s: %s]", webResponse.getStatusCode(), webResponse.getStatusMessage())); } final long currentTime = System.currentTimeMillis(); final ObjectMapper mapper = new ObjectMapper(); final Map map = mapper.readValue(webResponse.getContentAsStream(), Map.class); final String accessToken = map.get("access_token").toString(); final Integer expiresIn = Integer.valueOf(map.get("expires_in").toString()); return new OAuthToken(refreshToken, accessToken, currentTime + TimeUnit.MILLISECONDS.convert(expiresIn, TimeUnit.SECONDS)); }
private void closeLogException(LinkedInOAuthRequestFilter requestFilter) { try { requestFilter.close(); } catch (Exception e) { LOG.warn("Error closing OAuth2 request filter: {}", e.getMessage(), e); } } }
private static OAuthParams getOAuthParams(LinkedInConfiguration configuration) { return new OAuthParams(configuration.getUserName(), configuration.getUserPassword(), new CachingOAuthSecureStorage(configuration.getSecureStorage()), configuration.getClientId(), configuration.getClientSecret(), configuration.getRedirectUri(), configuration.getScopes()); }
private static LinkedInOAuthRequestFilter createRequestFilter(LinkedInConfiguration configuration) { // validate configuration configuration.validate(); final String[] enabledProtocols; try { // use default SSP to create supported non-SSL protocols list final SSLContext sslContext = new SSLContextParameters().createSSLContext(); enabledProtocols = sslContext.createSSLEngine().getEnabledProtocols(); } catch (GeneralSecurityException e) { throw ObjectHelper.wrapRuntimeCamelException(e); } catch (IOException e) { throw ObjectHelper.wrapRuntimeCamelException(e); } return new LinkedInOAuthRequestFilter(getOAuthParams(configuration), configuration.getHttpParams(), configuration.isLazyAuth(), enabledProtocols); }
public DefaultOAuthSecureStorage(String accessToken, Long expiryTime) { super(null); if (expiryTime == null) { expiryTime = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(60, TimeUnit.DAYS); } this.token = new OAuthToken(null, accessToken, expiryTime); } }
@Override public void filter(ClientRequestContext requestContext) throws IOException { updateOAuthToken(); // add OAuth query param final String requestUri = requestContext.getUri().toString(); final StringBuilder builder = new StringBuilder(requestUri); if (requestUri.contains("?")) { builder.append('&'); } else { builder.append('?'); } builder.append("oauth2_access_token=").append(oAuthToken.getAccessToken()); requestContext.setUri(URI.create(builder.toString())); }
updateOAuthToken(); } catch (IOException e) { throw new IllegalArgumentException( String.format("Error authorizing user %s: %s", oAuthParams.getUserName(), e.getMessage()), e);
public static OAuthScope fromValue(String value) { for (OAuthScope scope : values()) { if (scope.value.equals(value)) { return scope; } } throw new IllegalArgumentException(value); }
@Override protected Object doInvokeMethod(Map<String, Object> args) { try { return super.doInvokeMethod(args); } catch (RuntimeCamelException e) { if (e.getCause() instanceof WebApplicationException) { WebApplicationException cause = (WebApplicationException) e.getCause(); final Response response = cause.getResponse(); if (response.hasEntity()) { // try and convert it to LinkedInException final org.apache.camel.component.linkedin.api.model.Error error = response.readEntity(Error.class); throw new RuntimeCamelException( String.format("Error invoking %s: %s", method.getName(), error.getMessage()), new LinkedInException(error, response)); } } throw e; } } }
@Override public OAuthToken getOAuthToken() { // delegate only if token doesn't exist or has expired if (secureStorage != null && (token == null || token.getExpiryTime() < System.currentTimeMillis())) { token = secureStorage.getOAuthToken(); } return token; }
@Override public void saveOAuthToken(OAuthToken newToken) { token = newToken; if (secureStorage != null) { secureStorage.saveOAuthToken(newToken); } } }
private void closeLogException(LinkedInOAuthRequestFilter requestFilter) { try { requestFilter.close(); } catch (Exception e) { LOG.warn("Error closing OAuth2 request filter: " + e.getMessage(), e); } } }
private static OAuthParams getOAuthParams(LinkedInConfiguration configuration) { OAuthSecureStorage secureStorage = configuration.getSecureStorage(); if (secureStorage == null && !ObjectHelper.isEmpty(configuration.getAccessToken())) { secureStorage = new DefaultOAuthSecureStorage(configuration.getAccessToken(), configuration.getExpiryTime()); } return new OAuthParams(configuration.getUserName(), configuration.getUserPassword(), new CachingOAuthSecureStorage(secureStorage), configuration.getClientId(), configuration.getClientSecret(), configuration.getRedirectUri(), configuration.getScopes()); }
private LinkedInOAuthRequestFilter createRequestFilter(LinkedInConfiguration configuration) { // validate configuration configuration.validate(); final String[] enabledProtocols; try { // use default SSP to create supported non-SSL protocols list final SSLContext sslContext = new SSLContextParameters().createSSLContext(getCamelContext()); enabledProtocols = sslContext.createSSLEngine().getEnabledProtocols(); } catch (GeneralSecurityException e) { throw ObjectHelper.wrapRuntimeCamelException(e); } catch (IOException e) { throw ObjectHelper.wrapRuntimeCamelException(e); } return new LinkedInOAuthRequestFilter(getOAuthParams(configuration), configuration.getHttpParams(), configuration.isLazyAuth(), enabledProtocols); }
@Override protected Object doInvokeMethod(ApiMethod method, Map<String, Object> properties) throws RuntimeCamelException { try { return super.doInvokeMethod(method, properties); } catch (RuntimeCamelException e) { if (e.getCause() instanceof WebApplicationException) { final WebApplicationException cause = (WebApplicationException) e.getCause(); final Response response = cause.getResponse(); if (response.hasEntity()) { // try and convert it to LinkedInException final org.apache.camel.component.linkedin.api.model.Error error = response.readEntity(Error.class); throw new RuntimeCamelException( String.format("Error invoking %s: %s", method.getName(), error.getMessage()), new LinkedInException(error, response)); } } throw e; } } }