Here are the examples of the java api class com.google.cloud.AuthCredentials.ServiceAccountAuthCredentials taken from open source projects.
1. StorageImplTest#testSignUrl()
Project: gcloud-java
File: StorageImplTest.java
File: StorageImplTest.java
@Test public void testSignUrl() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException { EasyMock.replay(storageRpcMock); ServiceAccountAuthCredentials authCredentials = ServiceAccountAuthCredentials.createFor(ACCOUNT, privateKey); storage = options.toBuilder().authCredentials(authCredentials).build().service(); URL url = storage.signUrl(BLOB_INFO1, 14, TimeUnit.DAYS); String stringUrl = url.toString(); String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1).append('/').append(BLOB_NAME1).append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=").append(42L + 1209600).append("&Signature=").toString(); assertTrue(stringUrl.startsWith(expectedUrl)); String signature = stringUrl.substring(expectedUrl.length()); StringBuilder signedMessageBuilder = new StringBuilder(); signedMessageBuilder.append(HttpMethod.GET).append("\n\n\n").append(42L + 1209600).append("\n/").append(BUCKET_NAME1).append('/').append(BLOB_NAME1); Signature signer = Signature.getInstance("SHA256withRSA"); signer.initVerify(publicKey); signer.update(signedMessageBuilder.toString().getBytes(UTF_8)); assertTrue(signer.verify(BaseEncoding.base64().decode(URLDecoder.decode(signature, UTF_8.name())))); }
2. StorageImplTest#testSignUrlWithOptions()
Project: gcloud-java
File: StorageImplTest.java
File: StorageImplTest.java
@Test public void testSignUrlWithOptions() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException { EasyMock.replay(storageRpcMock); ServiceAccountAuthCredentials authCredentials = ServiceAccountAuthCredentials.createFor(ACCOUNT, privateKey); storage = options.toBuilder().authCredentials(authCredentials).build().service(); URL url = storage.signUrl(BLOB_INFO1, 14, TimeUnit.DAYS, Storage.SignUrlOption.httpMethod(HttpMethod.POST), Storage.SignUrlOption.withContentType(), Storage.SignUrlOption.withMd5()); String stringUrl = url.toString(); String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1).append('/').append(BLOB_NAME1).append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=").append(42L + 1209600).append("&Signature=").toString(); assertTrue(stringUrl.startsWith(expectedUrl)); String signature = stringUrl.substring(expectedUrl.length()); StringBuilder signedMessageBuilder = new StringBuilder(); signedMessageBuilder.append(HttpMethod.POST).append('\n').append(BLOB_INFO1.md5()).append('\n').append(BLOB_INFO1.contentType()).append('\n').append(42L + 1209600).append("\n/").append(BUCKET_NAME1).append('/').append(BLOB_NAME1); Signature signer = Signature.getInstance("SHA256withRSA"); signer.initVerify(publicKey); signer.update(signedMessageBuilder.toString().getBytes(UTF_8)); assertTrue(signer.verify(BaseEncoding.base64().decode(URLDecoder.decode(signature, UTF_8.name())))); }
3. StorageImplTest#testSignUrlLeadingSlash()
Project: gcloud-java
File: StorageImplTest.java
File: StorageImplTest.java
@Test public void testSignUrlLeadingSlash() throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException { String blobName = "/b1"; EasyMock.replay(storageRpcMock); ServiceAccountAuthCredentials authCredentials = ServiceAccountAuthCredentials.createFor(ACCOUNT, privateKey); storage = options.toBuilder().authCredentials(authCredentials).build().service(); URL url = storage.signUrl(BlobInfo.builder(BUCKET_NAME1, blobName).build(), 14, TimeUnit.DAYS); String stringUrl = url.toString(); String expectedUrl = new StringBuilder("https://storage.googleapis.com/").append(BUCKET_NAME1).append(blobName).append("?GoogleAccessId=").append(ACCOUNT).append("&Expires=").append(42L + 1209600).append("&Signature=").toString(); assertTrue(stringUrl.startsWith(expectedUrl)); String signature = stringUrl.substring(expectedUrl.length()); StringBuilder signedMessageBuilder = new StringBuilder(); signedMessageBuilder.append(HttpMethod.GET).append("\n\n\n").append(42L + 1209600).append("\n/").append(BUCKET_NAME1).append(blobName); Signature signer = Signature.getInstance("SHA256withRSA"); signer.initVerify(publicKey); signer.update(signedMessageBuilder.toString().getBytes(UTF_8)); assertTrue(signer.verify(BaseEncoding.base64().decode(URLDecoder.decode(signature, UTF_8.name())))); }
4. AuthCredentialsTest#testServiceAccountFromKey()
Project: gcloud-java
File: AuthCredentialsTest.java
File: AuthCredentialsTest.java
@Test public void testServiceAccountFromKey() throws IOException, SignatureException { ServiceAccountAuthCredentials serviceAccountAuthCredentials = AuthCredentials.createFor(SERVICE_ACCOUNT, privateKey); ServiceAccountCredentials credentials = serviceAccountAuthCredentials.credentials(); assertEquals(SERVICE_ACCOUNT, serviceAccountAuthCredentials.account()); assertEquals(SERVICE_ACCOUNT, credentials.getClientEmail()); assertEquals(privateKey, credentials.getPrivateKey()); assertArrayEquals(signedBytes, serviceAccountAuthCredentials.sign(BYTES_TO_SIGN)); }
5. AuthCredentialsTest#testServiceAccountFromJson()
Project: gcloud-java
File: AuthCredentialsTest.java
File: AuthCredentialsTest.java
@Test public void testServiceAccountFromJson() throws IOException, SignatureException { ServiceAccountAuthCredentials serviceAccountAuthCredentials = AuthCredentials.createForJson(new ByteArrayInputStream(JSON_KEY.getBytes())); ServiceAccountCredentials credentials = serviceAccountAuthCredentials.credentials(); assertEquals(SERVICE_ACCOUNT, serviceAccountAuthCredentials.account()); assertEquals(SERVICE_ACCOUNT, credentials.getClientEmail()); assertEquals(privateKey, credentials.getPrivateKey()); assertArrayEquals(signedBytes, serviceAccountAuthCredentials.sign(BYTES_TO_SIGN)); }