在jclouds中对Google API进行手动身份验证,从而分离了令牌获取 [英] Manual authentication for Google API in jclouds, separating token acquisition

查看:100
本文介绍了在jclouds中对Google API进行手动身份验证,从而分离了令牌获取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将身份验证阶段与Google的Api创建分开,但是(对我而言)很难实现.

I need to separate the authentication phase from Google's Api creation, but it's very difficult (for me) to make it possible.

这非常重要,因为我正在创建一个REST API,该API应该接收先前获取的授权令牌,而不是出于安全原因直接从其用户那里接收凭据,因为使用令牌,我可以按照 RFC 6750 .

This is very important because I am creating a REST API that should receive the authorization tokens previously acquired and not the credentials directly from its users for security reasons, because with tokens I can set a lifetime limit as specified in RFC 6750.

我有以下代码:

public class Main { 

    public static void main(String[] args) {      

        // Reads the JSON credential file provided by Google
        String jsonContent = readJson(args[1]);  

        // Pass the credential content
        GoogleComputeEngineApi googleApi = 
                createApi(jsonContent); 
    }

    public static GoogleComputeEngineApi createApi(final String jsonCredentialContent) {
        try {
            Supplier<Credentials> credentialSupplier = new GoogleCredentialsFromJson(
                    jsonCredentialContent);

            ComputeServiceContext context = ContextBuilder
                    .newBuilder("google-compute-engine")
                    .credentialsSupplier(credentialSupplier)
                    .buildView(ComputeServiceContext.class);

            Credentials credentials = credentialSupplier.get();
            ContextBuilder contextBuilder = ContextBuilder
                    .newBuilder(GoogleComputeEngineProviderMetadata.builder()
                            .build())
                    .credentials(credentials.identity, credentials.credential);

            Injector injector = contextBuilder.buildInjector();
            return injector.getInstance(GoogleComputeEngineApi.class);

        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return null;
        }
    }  
}

以下是我需要的伪代码:

Below is a fake code with my needs:

public class Main { 

    public static void main(String[] args) {        

        String jsonCredentialContent = readJson(args[1]);  
        String oauthToken = "";

        // First acquires the OAuth token
        if(getAuthenticationType("google-compute-engine").equals("oauth")) {
            oauthToken = getTokenForOAuth(jsonCredentialContent);
        }        

        // Creates the Api with the previously acquired token
        GoogleComputeEngineApi googleApi = 
                createApi(oauthToken); 
    }       

    [...]

}

推荐答案

您可以直接使用jclouds OAuth API获取承载令牌,如下所示:

You can directly use the jclouds OAuth API to get the bearer token, as follows:

GoogleCredentialsFromJson credentials = new GoogleCredentialsFromJson(jsoncreds);

AuthorizationApi oauth = ContextBuilder.newBuilder("google-compute-engine")
    .credentialsSupplier(credentials)
    .buildApi(AuthorizationApi.class);

try {
    long nowInSeconds = System.currentTimeMillis() / 1000;
    Claims claims = Claims.create(
        credentials.get().identity, // issuer
        "https://www.googleapis.com/auth/compute", // write scope
        "https://accounts.google.com/o/oauth2/token", // audience
        nowInSeconds + 60, // token expiration (seconds)
        nowInSeconds // current time (secods)
    );
    Token token = oauth.authorize(claims);
    System.out.println(token);
} finally {
    oauth.close();
}

一旦拥有Bearer访问令牌,就可以使用它创建jclouds上下文,如下所示:

Once you have the Bearer access token you can create the jclouds context with it as follows:

// Override GCE default Oauth flow (JWT) by the Bearer token flow
Properties overrides = new Properties();
overrides.put(OAuthProperties.CREDENTIAL_TYPE, CredentialType.BEARER_TOKEN_CREDENTIALS.toString());

// It is important to set the proper identity too, as it is used to resolve the GCE project
ComputeServiceContext ctx = ContextBuilder.newBuilder("google-compute-engine")
    .overrides(overrides)
    .credentials(credentials.get().identity, token.accessToken())
    .buildView(ComputeServiceContext.class);

GoogleComputeEngineApi google = ctx.unwrapApi(GoogleComputeEngineApi.class);

这篇关于在jclouds中对Google API进行手动身份验证,从而分离了令牌获取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆