|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud; |
| 18 | + |
| 19 | +import static com.google.common.base.Charsets.UTF_8; |
| 20 | +import static org.junit.Assert.assertArrayEquals; |
| 21 | +import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertNull; |
| 23 | +import static org.junit.Assert.assertSame; |
| 24 | + |
| 25 | +import com.google.auth.oauth2.AccessToken; |
| 26 | +import com.google.auth.oauth2.ServiceAccountCredentials; |
| 27 | +import com.google.cloud.AuthCredentials.OAuth2AuthCredentials; |
| 28 | +import com.google.cloud.AuthCredentials.ServiceAccountAuthCredentials; |
| 29 | +import com.google.common.io.BaseEncoding; |
| 30 | + |
| 31 | +import org.junit.BeforeClass; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import java.io.ByteArrayInputStream; |
| 35 | +import java.io.IOException; |
| 36 | +import java.security.InvalidKeyException; |
| 37 | +import java.security.KeyFactory; |
| 38 | +import java.security.NoSuchAlgorithmException; |
| 39 | +import java.security.PrivateKey; |
| 40 | +import java.security.Signature; |
| 41 | +import java.security.SignatureException; |
| 42 | +import java.security.spec.InvalidKeySpecException; |
| 43 | +import java.security.spec.PKCS8EncodedKeySpec; |
| 44 | +import java.util.Date; |
| 45 | + |
| 46 | +public class AuthCredentialsTest { |
| 47 | + |
| 48 | + private static final String ACCESS_TOKEN = "accessToken"; |
| 49 | + private static final Date EXPIRATION_DATE = new Date(); |
| 50 | + private static final String SERVICE_ACCOUNT = "[email protected]"; |
| 51 | + private static final String PRIVATE_KEY_STRING = "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoG" |
| 52 | + + "BAL2xolH1zrISQ8+GzOV29BNjjzq4/HIP8Psd1+cZb81vDklSF+95wB250MSE0BDc81pvIMwj5OmIfLg1NY6uB1xav" |
| 53 | + + "OPpVdx1z664AGc/BEJ1zInXGXaQ6s+SxGenVq40Yws57gikQGMZjttpf1Qbz4DjkxsbRoeaRHn06n9pH1ejAgMBAAE" |
| 54 | + + "CgYEAkWcm0AJF5LMhbWKbjkxm/LG06UNApkHX6vTOOOODkonM/qDBnhvKCj8Tan+PaU2j7679Cd19qxCm4SBQJET7e" |
| 55 | + + "BhqLD9L2j9y0h2YUQnLbISaqUS1/EXcr2C1Lf9VCEn1y/GYuDYqs85rGoQ4ZYfM9ClROSq86fH+cbIIssqJqukCQQD" |
| 56 | + + "18LjfJz/ichFeli5/l1jaFid2XoCH3T6TVuuysszVx68fh60gSIxEF/0X2xB+wuPxTP4IQ+t8tD/ktd232oWXAkEAx" |
| 57 | + + "XPych2QBHePk9/lek4tOkKBgfnDzex7S/pI0G1vpB3VmzBbCsokn9lpOv7JV8071GDlW/7R6jlLfpQy3hN31QJAE10" |
| 58 | + + "osSk99m5Uv8XDU3hvHnywDrnSFOBulNs7I47AYfSe7TSZhPkxUgsxejddTR27JLyTI8N1PxRSE4feNSOXcQJAMMKJR" |
| 59 | + + "JT4U6IS2rmXubREhvaVdLtxFxEnAYQ1JwNfZm/XqBMw6GEy2iaeTetNXVlZRQEIoscyn1y2v/No/F5iYQJBAKBOGAS" |
| 60 | + + "oQcBjGTOg/H/SfcE8QVNsKEpthRrs6CkpT80aZ/AV+ksfoIf2zw2M3mAHfrO+TBLdz4sicuFQvlN9SEc="; |
| 61 | + private static final String JSON_KEY = "{\n" |
| 62 | + + " \"private_key_id\": \"somekeyid\",\n" |
| 63 | + + " \"private_key\": \"-----BEGIN PRIVATE KEY-----\\n" + PRIVATE_KEY_STRING |
| 64 | + + "\\n-----END PRIVATE KEY-----\\n\",\n" |
| 65 | + + " \"client_email\": \"[email protected]\",\n" |
| 66 | + + " \"client_id\": \"someclientid.apps.googleusercontent.com\",\n" |
| 67 | + + " \"type\": \"service_account\"\n" |
| 68 | + + "}"; |
| 69 | + private static final AuthCredentials NO_AUTH_CREDENTIALS = AuthCredentials.noAuth(); |
| 70 | + private static final OAuth2AuthCredentials OAUTH2_AUTH_CREDENTIALS = |
| 71 | + AuthCredentials.createFor(ACCESS_TOKEN, EXPIRATION_DATE); |
| 72 | + private static final byte[] BYTES_TO_SIGN = PRIVATE_KEY_STRING.getBytes(UTF_8); |
| 73 | + |
| 74 | + private static PrivateKey privateKey; |
| 75 | + private static byte[] signedBytes; |
| 76 | + |
| 77 | + @BeforeClass |
| 78 | + public static void beforeClass() throws NoSuchAlgorithmException, InvalidKeySpecException, |
| 79 | + InvalidKeyException, SignatureException { |
| 80 | + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| 81 | + privateKey = keyFactory.generatePrivate( |
| 82 | + new PKCS8EncodedKeySpec(BaseEncoding.base64().decode(PRIVATE_KEY_STRING))); |
| 83 | + Signature signature = Signature.getInstance("SHA256withRSA"); |
| 84 | + signature.initSign(privateKey); |
| 85 | + signature.update(BYTES_TO_SIGN); |
| 86 | + signedBytes = signature.sign(); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testNoAuthCredentials() { |
| 91 | + assertSame(NO_AUTH_CREDENTIALS, AuthCredentials.noAuth()); |
| 92 | + assertNull(NO_AUTH_CREDENTIALS.credentials()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void testOAuth2AuthCredentials() { |
| 97 | + AccessToken accessToken = OAUTH2_AUTH_CREDENTIALS.credentials().getAccessToken(); |
| 98 | + assertEquals(ACCESS_TOKEN, accessToken.getTokenValue()); |
| 99 | + assertEquals(EXPIRATION_DATE, accessToken.getExpirationTime()); |
| 100 | + OAuth2AuthCredentials oAuth2AuthCredentials = |
| 101 | + AuthCredentials.createFor(ACCESS_TOKEN); |
| 102 | + accessToken = oAuth2AuthCredentials.credentials().getAccessToken(); |
| 103 | + assertEquals(ACCESS_TOKEN, accessToken.getTokenValue()); |
| 104 | + assertNull(accessToken.getExpirationTime()); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testServiceAccountFromJson() throws IOException, SignatureException { |
| 109 | + ServiceAccountAuthCredentials serviceAccountAuthCredentials = |
| 110 | + AuthCredentials.createForJson(new ByteArrayInputStream(JSON_KEY.getBytes())); |
| 111 | + ServiceAccountCredentials credentials = serviceAccountAuthCredentials.credentials(); |
| 112 | + assertEquals(SERVICE_ACCOUNT, serviceAccountAuthCredentials.account()); |
| 113 | + assertEquals(SERVICE_ACCOUNT, credentials.getClientEmail()); |
| 114 | + assertEquals(privateKey, credentials.getPrivateKey()); |
| 115 | + assertArrayEquals(signedBytes, serviceAccountAuthCredentials.sign(BYTES_TO_SIGN)); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void testServiceAccountFromKey() throws IOException, SignatureException { |
| 120 | + ServiceAccountAuthCredentials serviceAccountAuthCredentials = |
| 121 | + AuthCredentials.createFor(SERVICE_ACCOUNT, privateKey); |
| 122 | + ServiceAccountCredentials credentials = serviceAccountAuthCredentials.credentials(); |
| 123 | + assertEquals(SERVICE_ACCOUNT, serviceAccountAuthCredentials.account()); |
| 124 | + assertEquals(SERVICE_ACCOUNT, credentials.getClientEmail()); |
| 125 | + assertEquals(privateKey, credentials.getPrivateKey()); |
| 126 | + assertArrayEquals(signedBytes, serviceAccountAuthCredentials.sign(BYTES_TO_SIGN)); |
| 127 | + } |
| 128 | +} |
0 commit comments