Tabnine Logo
ServiceAccountJwtAccessCredentials
Code IndexAdd Tabnine to your IDE (free)

How to use
ServiceAccountJwtAccessCredentials
in
com.google.auth.oauth2

Best Java code snippets using com.google.auth.oauth2.ServiceAccountJwtAccessCredentials (Showing top 20 results out of 315)

origin: googleapis/google-auth-library-java

@Override
public String getAccount() {
 return getClientEmail();
}
origin: googleapis/google-auth-library-java

@Override
public byte[] sign(byte[] toSign) {
 try {
  Signature signer = Signature.getInstance(OAuth2Utils.SIGNATURE_ALGORITHM);
  signer.initSign(getPrivateKey());
  signer.update(toSign);
  return signer.sign();
 } catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException ex) {
  throw new ServiceAccountSigner.SigningException("Failed to sign the provided bytes", ex);
 }
}
origin: googleapis/google-auth-library-java

/**
 * Factory using PKCS#8 for the private key.
 *
 * @param clientId Client ID of the service account from the console. May be null.
 * @param clientEmail Client email address of the service account from the console.
 * @param privateKeyPkcs8 RSA private key object for the service account in PKCS#8 format.
 * @param privateKeyId Private key identifier for the service account. May be null.
 * @return New ServiceAccountJwtAcceessCredentials created from a private key.
 * @throws IOException if the credential cannot be created from the private key.
 */
public static ServiceAccountJwtAccessCredentials fromPkcs8(String clientId, String clientEmail, 
  String privateKeyPkcs8, String privateKeyId) throws IOException {
 return fromPkcs8(clientId, clientEmail, privateKeyPkcs8, privateKeyId, null);
}
origin: googleapis/google-auth-library-java

@Test 
public void constructor_allParameters_constructs() throws IOException {
 PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
 ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .build();
 
 assertEquals(SA_CLIENT_ID, credentials.getClientId());
 assertEquals(SA_CLIENT_EMAIL, credentials.getClientEmail());
 assertEquals(privateKey, credentials.getPrivateKey());
 assertEquals(SA_PRIVATE_KEY_ID, credentials.getPrivateKeyId());
}
origin: googleapis/google-auth-library-java

@Test
public void getRequestMetadata_blocking_cached() throws IOException {
 TestClock testClock = new TestClock();
 PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
 ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .build();
 credentials.clock = testClock;
 Map<String, List<String>> metadata1 = credentials.getRequestMetadata(CALL_URI);
 // Fast forward time a little
 long lifeSpanMs = TimeUnit.SECONDS.toMillis(10);
 testClock.setCurrentTime(lifeSpanMs);
 Map<String, List<String>> metadata2 = credentials.getRequestMetadata(CALL_URI);
 assertEquals(metadata1, metadata2);
}
origin: GoogleCloudPlatform/cloud-bigtable-client

/**
 * Initializes OAuth2 credential from a private keyfile, as described in
 * <a href="https://developers.google.com/api-client-library/java/google-api-java-client/oauth2#service_accounts"
 * >Service accounts</a>.
 *
 * @param serviceAccountEmail Email address of the service account associated with the keyfile.
 * @param privateKeyFile Full local path to private keyfile.
 * @return a {@link com.google.auth.Credentials} object.
 * @throws java.io.IOException if any.
 * @throws java.security.GeneralSecurityException if any.
 */
public static Credentials getCredentialFromPrivateKeyServiceAccount(
  String serviceAccountEmail, String privateKeyFile)
  throws IOException, GeneralSecurityException {
 PrivateKey privateKey =
   SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(),
     new FileInputStream(privateKeyFile), "notasecret", "privatekey", "notasecret");
 return ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientEmail(serviceAccountEmail)
   .setPrivateKey(privateKey)
   .build();
}
origin: googleapis/google-auth-library-java

@Test
public void sign_sameAs()
  throws IOException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
 PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
 byte[] toSign = {0xD, 0xE, 0xA, 0xD};
 ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .build();
 byte[] signedBytes = credentials.sign(toSign);
 Signature signature = Signature.getInstance(OAuth2Utils.SIGNATURE_ALGORITHM);
 signature.initSign(credentials.getPrivateKey());
 signature.update(toSign);
 assertArrayEquals(signature.sign(), signedBytes);
}
origin: googleapis/google-auth-library-java

@Test
public void equals_false_clientId() throws IOException {
 PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
 ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .setDefaultAudience(CALL_URI)
   .build();
 ServiceAccountJwtAccessCredentials otherCredentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId("otherClientId")
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .setDefaultAudience(CALL_URI)
   .build();
 assertFalse(credentials.equals(otherCredentials));
 assertFalse(otherCredentials.equals(credentials));
}
origin: googleapis/google-auth-library-java

@Test
public void serialize() throws IOException, ClassNotFoundException {
 PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
 ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .setDefaultAudience(CALL_URI)
   .build();
 ServiceAccountJwtAccessCredentials deserializedCredentials =
   serializeAndDeserialize(credentials);
 verifyJwtAccess(deserializedCredentials.getRequestMetadata(), SA_CLIENT_EMAIL, CALL_URI, SA_PRIVATE_KEY_ID);
 assertEquals(credentials, deserializedCredentials);
 assertEquals(credentials.hashCode(), deserializedCredentials.hashCode());
 assertEquals(credentials.toString(), deserializedCredentials.toString());
 assertSame(deserializedCredentials.clock, Clock.SYSTEM);
}
origin: googleapis/google-auth-library-java

@Test
public void getAccount_sameAs() throws IOException {
 PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
 ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .build();
 assertEquals(SA_CLIENT_EMAIL, credentials.getAccount());
}
origin: googleapis/google-auth-library-java

@Test
public void hashCode_equals() throws IOException {
 PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
 ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .setDefaultAudience(CALL_URI)
   .build();
 ServiceAccountJwtAccessCredentials otherCredentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .setDefaultAudience(CALL_URI)
   .build();
 assertEquals(credentials.hashCode(), otherCredentials.hashCode());
}
origin: googleapis/google-auth-library-java

@Test
public void toString_containsFields() throws IOException {
 PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
 ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .setDefaultAudience(CALL_URI)
   .build();
 String expectedToString = String.format(
   "ServiceAccountJwtAccessCredentials{clientId=%s, clientEmail=%s, privateKeyId=%s, "
     + "defaultAudience=%s}",
   SA_CLIENT_ID,
   SA_CLIENT_EMAIL,
   SA_PRIVATE_KEY_ID,
   CALL_URI);
 assertEquals(expectedToString, credentials.toString());
}
origin: com.google.auth/google-auth-library-oauth2-http

/**
 * Returns credentials defined by a Service Account key file in JSON format from the Google
 * Developers Console.
 *
 * @param credentialsStream the stream with the credential definition.
 * @return the credential defined by the credentialsStream.
 * @throws IOException if the credential cannot be created from the stream.
 **/
public static ServiceAccountJwtAccessCredentials fromStream(InputStream credentialsStream)
  throws IOException {
 return fromStream(credentialsStream, null);
}
origin: googleapis/gax-java

@Test
public void serviceAccountReplacedWithJwtTokens() throws Exception {
 ServiceAccountCredentials serviceAccountCredentials =
   ServiceAccountCredentials.newBuilder()
     .setClientId("fake-client-id")
     .setClientEmail("fake@example.com")
     .setPrivateKeyId("fake-private-key")
     .setPrivateKey(Mockito.mock(PrivateKey.class))
     .build();
 PowerMockito.mockStatic(GoogleCredentials.class);
 Mockito.when(GoogleCredentials.getApplicationDefault()).thenReturn(serviceAccountCredentials);
 GoogleCredentialsProvider provider =
   GoogleCredentialsProvider.newBuilder()
     .setScopesToApply(ImmutableList.of("scope1", "scope2"))
     .setJwtEnabledScopes(ImmutableList.of("scope1"))
     .build();
 Credentials credentials = provider.getCredentials();
 assertThat(credentials).isInstanceOf(ServiceAccountJwtAccessCredentials.class);
 ServiceAccountJwtAccessCredentials jwtCreds = (ServiceAccountJwtAccessCredentials) credentials;
 assertThat(jwtCreds.getClientId()).isEqualTo(serviceAccountCredentials.getClientId());
 assertThat(jwtCreds.getClientEmail()).isEqualTo(serviceAccountCredentials.getClientEmail());
 assertThat(jwtCreds.getPrivateKeyId()).isEqualTo(serviceAccountCredentials.getPrivateKeyId());
 assertThat(jwtCreds.getPrivateKey()).isEqualTo(serviceAccountCredentials.getPrivateKey());
}
origin: googleapis/google-auth-library-java

@Test
public void getRequestMetadata_blocking_cache_expired() throws IOException {
 TestClock testClock = new TestClock();
 PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
 ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .build();
 credentials.clock = testClock;
 Map<String, List<String>> metadata1 = credentials.getRequestMetadata(CALL_URI);
 // Fast forward time past the expiration
 long lifeSpanMs = TimeUnit.SECONDS.toMillis(ServiceAccountJwtAccessCredentials.LIFE_SPAN_SECS);
 testClock.setCurrentTime(lifeSpanMs);
 Map<String, List<String>> metadata2 = credentials.getRequestMetadata(CALL_URI);
 assertNotEquals(metadata1, metadata2);
}
origin: GoogleCloudPlatform/cloud-bigtable-client

 private static Credentials getJwtToken(ServiceAccountCredentials serviceAccount) {
  return ServiceAccountJwtAccessCredentials.newBuilder()
    .setClientEmail(serviceAccount.getClientEmail())
    .setClientId(serviceAccount.getClientId())
    .setPrivateKey(serviceAccount.getPrivateKey())
    .setPrivateKeyId(serviceAccount.getPrivateKeyId())
    .build();
 }
}
origin: googleapis/google-auth-library-java

@Test
public void equals_false_callUri() throws IOException {
 PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
 final URI otherCallUri = URI.create("https://foo.com/bar");
 ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .setDefaultAudience(CALL_URI)
   .build();
 ServiceAccountJwtAccessCredentials otherCredentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .setDefaultAudience(otherCallUri)
   .build();
 assertFalse(credentials.equals(otherCredentials));
 assertFalse(otherCredentials.equals(credentials));
}
origin: googleapis/google-auth-library-java

/**
 * Returns credentials defined by a Service Account key file in JSON format from the Google
 * Developers Console.
 *
 * @param credentialsStream the stream with the credential definition.
 * @return the credential defined by the credentialsStream.
 * @throws IOException if the credential cannot be created from the stream.
 **/
public static ServiceAccountJwtAccessCredentials fromStream(InputStream credentialsStream)
  throws IOException {
 return fromStream(credentialsStream, null);
}
origin: googleapis/google-auth-library-java

@Test
public void getRequestMetadata_blocking_hasJwtAccess() throws IOException {
 PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
 ServiceAccountJwtAccessCredentials credentials = ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .setPrivateKeyId(SA_PRIVATE_KEY_ID)
   .build();
 Map<String, List<String>> metadata = credentials.getRequestMetadata(CALL_URI);
 verifyJwtAccess(metadata, SA_CLIENT_EMAIL, CALL_URI, SA_PRIVATE_KEY_ID);
}
origin: googleapis/google-auth-library-java

@Test 
public void constructor_noPrivateKeyId_constructs() throws IOException {
 PrivateKey privateKey = ServiceAccountCredentials.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8);
 ServiceAccountJwtAccessCredentials.newBuilder()
   .setClientId(SA_CLIENT_ID)
   .setClientEmail(SA_CLIENT_EMAIL)
   .setPrivateKey(privateKey)
   .build();  }
com.google.auth.oauth2ServiceAccountJwtAccessCredentials

Javadoc

Service Account credentials for calling Google APIs using a JWT directly for access.

Uses a JSON Web Token (JWT) directly in the request metadata to provide authorization.

Most used methods

  • newBuilder
  • getClientEmail
  • getPrivateKey
  • fromPkcs8
    Factory using PKCS#8 for the private key.
  • fromStream
    Returns credentials defined by a Service Account key file in JSON format from the Google Developers
  • getClientId
  • getPrivateKeyId
  • <init>
    Constructor with full information.
  • blockingGetToCallback
  • createCache
  • equals
  • fromJson
    Returns service account credentials defined by JSON using the format supported by the Google Develop
  • equals,
  • fromJson,
  • getAccount,
  • getJwtAccess,
  • getRequestMetadata,
  • hashCode,
  • sign,
  • toString

Popular in Java

  • Updating database using SQL prepared statement
  • setContentView (Activity)
  • putExtra (Intent)
  • setRequestProperty (URLConnection)
  • HashMap (java.util)
    HashMap is an implementation of Map. All optional operations are supported.All elements are permitte
  • Semaphore (java.util.concurrent)
    A counting semaphore. Conceptually, a semaphore maintains a set of permits. Each #acquire blocks if
  • Pattern (java.util.regex)
    Patterns are compiled regular expressions. In many cases, convenience methods such as String#matches
  • JButton (javax.swing)
  • Table (org.hibernate.mapping)
    A relational table
  • Option (scala)
  • From CI to AI: The AI layer in your organization
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now