You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jclouds.apache.org by na...@apache.org on 2015/04/15 21:41:34 UTC

[29/39] jclouds git commit: Fix support for bearer tokens

Fix support for bearer tokens


Project: http://git-wip-us.apache.org/repos/asf/jclouds/repo
Commit: http://git-wip-us.apache.org/repos/asf/jclouds/commit/639b4806
Tree: http://git-wip-us.apache.org/repos/asf/jclouds/tree/639b4806
Diff: http://git-wip-us.apache.org/repos/asf/jclouds/diff/639b4806

Branch: refs/heads/master
Commit: 639b4806502032c64976c02c5494b78a68985d14
Parents: eb8b154
Author: Chris Custine <cc...@apache.org>
Authored: Fri Oct 24 09:13:05 2014 -0600
Committer: Chris Custine <cc...@apache.org>
Committed: Fri Nov 7 17:44:24 2014 -0700

----------------------------------------------------------------------
 .../java/org/jclouds/oauth/v2/OAuthApiMetadata.java   |  3 +++
 .../oauth/v2/filters/BearerTokenAuthenticator.java    |  2 +-
 .../oauth/v2/functions/OAuthCredentialsSupplier.java  | 14 +++++++++++---
 .../java/org/jclouds/oauth/v2/OAuthTestUtils.java     |  6 ++++--
 .../v2/functions/OAuthCredentialsFromPKTest.java      |  3 ++-
 .../v2/functions/OAuthCredentialsSupplierTest.java    |  7 ++++---
 6 files changed, 25 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jclouds/blob/639b4806/apis/oauth/src/main/java/org/jclouds/oauth/v2/OAuthApiMetadata.java
----------------------------------------------------------------------
diff --git a/apis/oauth/src/main/java/org/jclouds/oauth/v2/OAuthApiMetadata.java b/apis/oauth/src/main/java/org/jclouds/oauth/v2/OAuthApiMetadata.java
index 2690c15..58714e0 100644
--- a/apis/oauth/src/main/java/org/jclouds/oauth/v2/OAuthApiMetadata.java
+++ b/apis/oauth/src/main/java/org/jclouds/oauth/v2/OAuthApiMetadata.java
@@ -17,6 +17,8 @@
 package org.jclouds.oauth.v2;
 
 import static org.jclouds.Constants.PROPERTY_SESSION_INTERVAL;
+import static org.jclouds.oauth.v2.config.CredentialType.SERVICE_ACCOUNT_CREDENTIALS;
+import static org.jclouds.oauth.v2.config.OAuthProperties.CREDENTIAL_TYPE;
 import static org.jclouds.oauth.v2.config.OAuthProperties.SIGNATURE_OR_MAC_ALGORITHM;
 
 import java.net.URI;
@@ -50,6 +52,7 @@ public class OAuthApiMetadata extends BaseHttpApiMetadata<OAuthApi> {
    public static Properties defaultProperties() {
       Properties properties = BaseHttpApiMetadata.defaultProperties();
       properties.put(SIGNATURE_OR_MAC_ALGORITHM, "RS256");
+      properties.put(CREDENTIAL_TYPE, SERVICE_ACCOUNT_CREDENTIALS);
       properties.put(PROPERTY_SESSION_INTERVAL, 3600);
       return properties;
    }

http://git-wip-us.apache.org/repos/asf/jclouds/blob/639b4806/apis/oauth/src/main/java/org/jclouds/oauth/v2/filters/BearerTokenAuthenticator.java
----------------------------------------------------------------------
diff --git a/apis/oauth/src/main/java/org/jclouds/oauth/v2/filters/BearerTokenAuthenticator.java b/apis/oauth/src/main/java/org/jclouds/oauth/v2/filters/BearerTokenAuthenticator.java
index 5ff8c50..c5d944c 100644
--- a/apis/oauth/src/main/java/org/jclouds/oauth/v2/filters/BearerTokenAuthenticator.java
+++ b/apis/oauth/src/main/java/org/jclouds/oauth/v2/filters/BearerTokenAuthenticator.java
@@ -34,6 +34,6 @@ public final class BearerTokenAuthenticator implements OAuthAuthenticationFilter
    }
 
    @Override public HttpRequest filter(HttpRequest request) throws HttpException {
-      return request.toBuilder().addHeader("Authorization", format("%s %s", "Bearer ", creds.get().credential)).build();
+      return request.toBuilder().addHeader("Authorization", format("%s %s", "Bearer", creds.get().credential)).build();
    }
 }

http://git-wip-us.apache.org/repos/asf/jclouds/blob/639b4806/apis/oauth/src/main/java/org/jclouds/oauth/v2/functions/OAuthCredentialsSupplier.java
----------------------------------------------------------------------
diff --git a/apis/oauth/src/main/java/org/jclouds/oauth/v2/functions/OAuthCredentialsSupplier.java b/apis/oauth/src/main/java/org/jclouds/oauth/v2/functions/OAuthCredentialsSupplier.java
index cd7e3d6..3ec907e 100644
--- a/apis/oauth/src/main/java/org/jclouds/oauth/v2/functions/OAuthCredentialsSupplier.java
+++ b/apis/oauth/src/main/java/org/jclouds/oauth/v2/functions/OAuthCredentialsSupplier.java
@@ -30,16 +30,15 @@ import java.io.IOException;
 import java.security.GeneralSecurityException;
 import java.security.KeyFactory;
 import java.security.PrivateKey;
-
 import javax.inject.Inject;
 import javax.inject.Named;
 import javax.inject.Singleton;
 
 import org.jclouds.domain.Credentials;
 import org.jclouds.location.Provider;
+import org.jclouds.oauth.v2.config.CredentialType;
 import org.jclouds.oauth.v2.domain.OAuthCredentials;
 import org.jclouds.rest.AuthorizationException;
-
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Charsets;
 import com.google.common.base.Supplier;
@@ -77,16 +76,25 @@ public final class OAuthCredentialsSupplier implements Supplier<OAuthCredentials
    @VisibleForTesting
    static final class OAuthCredentialsForCredentials extends CacheLoader<Credentials, OAuthCredentials> {
       private final String keyFactoryAlgorithm;
+      private final CredentialType credentialType;
 
-      @Inject OAuthCredentialsForCredentials(@Named(SIGNATURE_OR_MAC_ALGORITHM) String signatureOrMacAlgorithm) {
+      @Inject OAuthCredentialsForCredentials(@Named(SIGNATURE_OR_MAC_ALGORITHM) String signatureOrMacAlgorithm,
+            CredentialType credentialType) {
          this.keyFactoryAlgorithm = OAUTH_ALGORITHM_NAMES_TO_KEYFACTORY_ALGORITHM_NAMES.get(checkNotNull(
                  signatureOrMacAlgorithm, "signatureOrMacAlgorithm"));
+         this.credentialType = credentialType;
       }
 
       @Override public OAuthCredentials load(Credentials in) {
          try {
             String identity = in.identity;
             String privateKeyInPemFormat = in.credential;
+
+            // If passing Bearer tokens, simply create and pass it along
+            if (credentialType == CredentialType.BEARER_TOKEN_CREDENTIALS) {
+               return new OAuthCredentials.Builder().identity(identity).credential(in.credential).build();
+            }
+
             if (keyFactoryAlgorithm.equals(NO_ALGORITHM)) {
                return new OAuthCredentials.Builder().identity(identity).credential(privateKeyInPemFormat).build();
             }

http://git-wip-us.apache.org/repos/asf/jclouds/blob/639b4806/apis/oauth/src/test/java/org/jclouds/oauth/v2/OAuthTestUtils.java
----------------------------------------------------------------------
diff --git a/apis/oauth/src/test/java/org/jclouds/oauth/v2/OAuthTestUtils.java b/apis/oauth/src/test/java/org/jclouds/oauth/v2/OAuthTestUtils.java
index 438d624..08d98c1 100644
--- a/apis/oauth/src/test/java/org/jclouds/oauth/v2/OAuthTestUtils.java
+++ b/apis/oauth/src/test/java/org/jclouds/oauth/v2/OAuthTestUtils.java
@@ -18,6 +18,8 @@ package org.jclouds.oauth.v2;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Throwables.propagate;
+import static org.jclouds.oauth.v2.OAuthConstants.NO_ALGORITHM;
+import static org.jclouds.oauth.v2.config.CredentialType.BEARER_TOKEN_CREDENTIALS;
 import static org.jclouds.oauth.v2.config.OAuthProperties.AUDIENCE;
 import static org.jclouds.util.Strings2.toStringAndClose;
 
@@ -25,7 +27,6 @@ import java.io.File;
 import java.io.IOException;
 import java.util.Properties;
 
-import org.jclouds.oauth.v2.config.CredentialType;
 import org.jclouds.oauth.v2.config.OAuthProperties;
 
 import com.google.common.base.Charsets;
@@ -52,7 +53,8 @@ public class OAuthTestUtils {
       properties.put("oauth.credential", "1/8xbJqaOZXSUZbHLl5EOtu1pxz3fmmetKx9W8CV4t79M");
       properties.put("oauth.endpoint", "http://localhost:5000/o/oauth2/token");
       properties.put(AUDIENCE, "https://accounts.google.com/o/oauth2/token");
-      properties.put(OAuthProperties.CREDENTIAL_TYPE, CredentialType.BEARER_TOKEN_CREDENTIALS.toString());
+      properties.put(OAuthProperties.CREDENTIAL_TYPE, BEARER_TOKEN_CREDENTIALS.toString());
+      properties.put(OAuthProperties.SIGNATURE_OR_MAC_ALGORITHM, NO_ALGORITHM.toString());
       return properties;
    }
 

http://git-wip-us.apache.org/repos/asf/jclouds/blob/639b4806/apis/oauth/src/test/java/org/jclouds/oauth/v2/functions/OAuthCredentialsFromPKTest.java
----------------------------------------------------------------------
diff --git a/apis/oauth/src/test/java/org/jclouds/oauth/v2/functions/OAuthCredentialsFromPKTest.java b/apis/oauth/src/test/java/org/jclouds/oauth/v2/functions/OAuthCredentialsFromPKTest.java
index e3794d6..6355a40 100644
--- a/apis/oauth/src/test/java/org/jclouds/oauth/v2/functions/OAuthCredentialsFromPKTest.java
+++ b/apis/oauth/src/test/java/org/jclouds/oauth/v2/functions/OAuthCredentialsFromPKTest.java
@@ -17,6 +17,7 @@
 package org.jclouds.oauth.v2.functions;
 
 import static com.google.common.base.Suppliers.ofInstance;
+import static org.jclouds.oauth.v2.config.CredentialType.SERVICE_ACCOUNT_CREDENTIALS;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertNotNull;
 
@@ -46,7 +47,7 @@ public class OAuthCredentialsFromPKTest {
          CertificateException, InvalidKeySpecException {
       OAuthCredentialsSupplier loader = new OAuthCredentialsSupplier(ofInstance(new Credentials("foo",
             Files.asCharSource(new File("src/test/resources/testpk.pem"), Charsets.UTF_8).read())),
-            new OAuthCredentialsForCredentials("RS256"), "RS256");
+            new OAuthCredentialsForCredentials("RS256", SERVICE_ACCOUNT_CREDENTIALS), "RS256");
       return loader.get();
    }
 

http://git-wip-us.apache.org/repos/asf/jclouds/blob/639b4806/apis/oauth/src/test/java/org/jclouds/oauth/v2/functions/OAuthCredentialsSupplierTest.java
----------------------------------------------------------------------
diff --git a/apis/oauth/src/test/java/org/jclouds/oauth/v2/functions/OAuthCredentialsSupplierTest.java b/apis/oauth/src/test/java/org/jclouds/oauth/v2/functions/OAuthCredentialsSupplierTest.java
index 2b3d094..cadb85e 100644
--- a/apis/oauth/src/test/java/org/jclouds/oauth/v2/functions/OAuthCredentialsSupplierTest.java
+++ b/apis/oauth/src/test/java/org/jclouds/oauth/v2/functions/OAuthCredentialsSupplierTest.java
@@ -24,6 +24,7 @@ import org.testng.annotations.Test;
 
 import java.util.Properties;
 
+import static org.jclouds.oauth.v2.config.CredentialType.SERVICE_ACCOUNT_CREDENTIALS;
 import static org.jclouds.oauth.v2.functions.OAuthCredentialsSupplier.OAuthCredentialsForCredentials;
 import static org.testng.Assert.assertNotNull;
 
@@ -33,14 +34,14 @@ public class OAuthCredentialsSupplierTest {
    @Test(expectedExceptions = AuthorizationException.class)
    public void testAuthorizationExceptionIsThrownOnBadKeys() {
       OAuthCredentialsSupplier supplier = new OAuthCredentialsSupplier(Suppliers.ofInstance(new Credentials("MOMMA",
-              "MIA")), new OAuthCredentialsForCredentials("RS256"), "RS256");
+              "FileNotFoundCredential")), new OAuthCredentialsForCredentials("RS256", SERVICE_ACCOUNT_CREDENTIALS), "RS256");
       supplier.get();
    }
 
    @Test(expectedExceptions = IllegalArgumentException.class)
    public void testGSEChildExceptionsPropagateAsAuthorizationException() {
       OAuthCredentialsSupplier supplier = new OAuthCredentialsSupplier(Suppliers.ofInstance(new Credentials("MOMMA",
-              "MIA")), new OAuthCredentialsForCredentials("MOMMA"), "MOMMA");
+              "MIA")), new OAuthCredentialsForCredentials("MOMMA", SERVICE_ACCOUNT_CREDENTIALS), "MOMMA");
       supplier.get();
    }
 
@@ -49,7 +50,7 @@ public class OAuthCredentialsSupplierTest {
       Credentials validCredentials = new Credentials(propertied.getProperty("oauth.identity"),
               propertied.getProperty("oauth.credential"));
       OAuthCredentialsSupplier supplier = new OAuthCredentialsSupplier(Suppliers.ofInstance(validCredentials),
-              new OAuthCredentialsForCredentials("RS256"), "RS256");
+              new OAuthCredentialsForCredentials("RS256", SERVICE_ACCOUNT_CREDENTIALS), "RS256");
       assertNotNull(supplier.get());
    }
 }