2017-11-05 9 views
0

Google의 API 생성과 인증 단계를 분리해야하지만 가능하기가 어렵습니다.jclouds의 Google API에 대한 수동 인증, 토큰 획득 분리

토큰을 사용하여 RFC 6750에 지정된 평생 제한을 설정할 수 있기 때문에 보안상의 이유로 이전에 획득 한 인증 토큰과 사용자의 자격 증명을 직접 수신해야하는 REST API를 만들기 때문에 이는 매우 중요합니다. 다음과 같이

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); 
    }  

    [...] 

} 

답변

2

직접 베어러 토큰을 얻기 위해 jclouds에게의 OAuth API를 사용할 수 있습니다 아래

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; 
     } 
    } 
} 

내 요구에 가짜 코드 :

나는 다음과 같은 코드가 있습니다 : 당신은 무기명 액세스 토큰이 있으면 다음과 같이

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(); 
} 

당신은 그것을 가진 jclouds 컨텍스트를 생성 할 수 있습니다 :

// 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); 
+0

효과가있었습니다. 동일한 jclouds 라이브러리를 사용하여 토큰을 얻을 수있는 방법을 알려주시겠습니까? 구현하기도하지만 어려움이 있습니다. – lopes

+2

완전한 예를 들어 답을 편집했습니다. –