use of org.apache.drill.common.logical.AbstractSecuredStoragePluginConfig in project drill by apache.
the class StorageResources method updateAuthToken.
@GET
@Path("/storage/{name}/update_oath2_authtoken")
@Produces(MediaType.TEXT_HTML)
public Response updateAuthToken(@PathParam("name") String name, @QueryParam("code") String code) {
try {
if (storage.getPlugin(name).getConfig() instanceof AbstractSecuredStoragePluginConfig) {
AbstractSecuredStoragePluginConfig securedStoragePluginConfig = (AbstractSecuredStoragePluginConfig) storage.getPlugin(name).getConfig();
CredentialsProvider credentialsProvider = securedStoragePluginConfig.getCredentialsProvider();
String callbackURL = this.request.getRequestURL().toString();
// Now exchange the authorization token for an access token
Builder builder = new OkHttpClient.Builder();
OkHttpClient client = builder.build();
Request accessTokenRequest = OAuthUtils.getAccessTokenRequest(credentialsProvider, code, callbackURL);
Map<String, String> updatedTokens = OAuthUtils.getOAuthTokens(client, accessTokenRequest);
// Add to token registry
TokenRegistry tokenRegistry = ((AbstractStoragePlugin) storage.getPlugin(name)).getContext().getoAuthTokenProvider().getOauthTokenRegistry();
// Add a token registry table if none exists
tokenRegistry.createTokenTable(name);
PersistentTokenTable tokenTable = tokenRegistry.getTokenTable(name);
// Add tokens to persistent storage
tokenTable.setAccessToken(updatedTokens.get(OAuthTokenCredentials.ACCESS_TOKEN));
tokenTable.setRefreshToken(updatedTokens.get(OAuthTokenCredentials.REFRESH_TOKEN));
// Get success page
String successPage = null;
try (InputStream inputStream = Resource.newClassPathResource(OAUTH_SUCCESS_PAGE).getInputStream()) {
InputStreamReader reader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(reader);
successPage = bufferedReader.lines().collect(Collectors.joining("\n"));
bufferedReader.close();
reader.close();
} catch (IOException e) {
Response.status(Status.OK).entity("You may close this window.").build();
}
return Response.status(Status.OK).entity(successPage).build();
} else {
logger.error("{} is not a HTTP plugin. You can only add auth code to HTTP plugins.", name);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message("Unable to add authorization code: %s", name)).build();
}
} catch (PluginException e) {
logger.error("Error when adding auth token to {}", name);
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(message("Unable to add authorization code: %s", e.getMessage())).build();
}
}
use of org.apache.drill.common.logical.AbstractSecuredStoragePluginConfig in project drill by apache.
the class PluginConfigWrapper method isOauth.
/**
* Determines whether the storage plugin in question needs the OAuth button in the UI. In
* order to be considered an OAuth plugin, the plugin must:
* 1. Use AbstractSecuredStoragePluginConfig
* 2. The credential provider must not be null
* 3. The credentialsProvider must contain a client_id and client_secret
* @return true if the plugin uses OAuth, false if not.
*/
@JsonIgnore
public boolean isOauth() {
if (!(config instanceof AbstractSecuredStoragePluginConfig)) {
return false;
}
AbstractSecuredStoragePluginConfig securedStoragePluginConfig = (AbstractSecuredStoragePluginConfig) config;
CredentialsProvider credentialsProvider = securedStoragePluginConfig.getCredentialsProvider();
if (credentialsProvider == null) {
return false;
}
OAuthTokenCredentials tokenCredentials = new OAuthTokenCredentials(credentialsProvider);
return !StringUtils.isEmpty(tokenCredentials.getClientID()) || !StringUtils.isEmpty(tokenCredentials.getClientSecret());
}
Aggregations