Search in sources :

Example 1 with TokenRegistry

use of org.apache.drill.exec.oauth.TokenRegistry 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();
    }
}
Also used : OkHttpClient(okhttp3.OkHttpClient) InputStreamReader(java.io.InputStreamReader) TokenRegistry(org.apache.drill.exec.oauth.TokenRegistry) InputStream(java.io.InputStream) Builder(okhttp3.OkHttpClient.Builder) PluginException(org.apache.drill.exec.store.StoragePluginRegistry.PluginException) Request(okhttp3.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) CredentialsProvider(org.apache.drill.common.logical.security.CredentialsProvider) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) PersistentTokenTable(org.apache.drill.exec.oauth.PersistentTokenTable) AbstractSecuredStoragePluginConfig(org.apache.drill.common.logical.AbstractSecuredStoragePluginConfig) AbstractStoragePlugin(org.apache.drill.exec.store.AbstractStoragePlugin) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with TokenRegistry

use of org.apache.drill.exec.oauth.TokenRegistry in project drill by apache.

the class StorageResources method deletePlugin.

@DELETE
@Path("/storage/{name}.json")
@Produces(MediaType.APPLICATION_JSON)
public Response deletePlugin(@PathParam("name") String name) {
    try {
        TokenRegistry tokenRegistry = ((AbstractStoragePlugin) storage.getPlugin(name)).getContext().getoAuthTokenProvider().getOauthTokenRegistry();
        // Delete a token registry table if it exists
        tokenRegistry.deleteTokenTable(name);
        storage.remove(name);
        return Response.ok().entity(message("Success")).build();
    } catch (PluginException e) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(message("Error while deleting plugin: %s", e.getMessage())).build();
    }
}
Also used : TokenRegistry(org.apache.drill.exec.oauth.TokenRegistry) PluginException(org.apache.drill.exec.store.StoragePluginRegistry.PluginException) AbstractStoragePlugin(org.apache.drill.exec.store.AbstractStoragePlugin) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

Aggregations

Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 TokenRegistry (org.apache.drill.exec.oauth.TokenRegistry)2 AbstractStoragePlugin (org.apache.drill.exec.store.AbstractStoragePlugin)2 PluginException (org.apache.drill.exec.store.StoragePluginRegistry.PluginException)2 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 DELETE (javax.ws.rs.DELETE)1 GET (javax.ws.rs.GET)1 OkHttpClient (okhttp3.OkHttpClient)1 Builder (okhttp3.OkHttpClient.Builder)1 Request (okhttp3.Request)1 AbstractSecuredStoragePluginConfig (org.apache.drill.common.logical.AbstractSecuredStoragePluginConfig)1 CredentialsProvider (org.apache.drill.common.logical.security.CredentialsProvider)1 PersistentTokenTable (org.apache.drill.exec.oauth.PersistentTokenTable)1