@Override public AccessToken refreshAccessToken() throws IOException { Socket socket = new Socket("localhost", this.getAuthPort()); socket.setSoTimeout(READ_TIMEOUT_MS); AccessToken token; try { OutputStream os = socket.getOutputStream(); os.write(GET_AUTH_TOKEN_REQUEST_BYTES); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); input.readLine(); // Skip over the first line JsonParser parser = OAuth2Utils.JSON_FACTORY.createJsonParser(input); List<Object> messageArray = (List<Object>) parser.parseArray(ArrayList.class, Object.class); String accessToken = messageArray.get(ACCESS_TOKEN_INDEX).toString(); token = new AccessToken(accessToken, null); } finally { socket.close(); } return token; }
public CloudShellCredentials build() { return new CloudShellCredentials(authPort); } }
/** * @param authPort Authentication port. * @return The CloudShellCredentials. * @deprecated Use {@link #create(int)} instead. This method will be deleted in a later version. */ @Deprecated public static CloudShellCredentials of(int authPort) { return create(authPort); }
public static CloudShellCredentials create(int authPort) { return CloudShellCredentials.newBuilder().setAuthPort(authPort).build(); }
public static CloudShellCredentials create(int authPort) { return CloudShellCredentials.newBuilder().setAuthPort(authPort).build(); }
@Test public void toString_containsFields() throws IOException { String expectedToString = String.format("CloudShellCredentials{authPort=%d}", 42); GoogleCredentials credentials = CloudShellCredentials.newBuilder() .setAuthPort(42) .build(); assertEquals(expectedToString, credentials.toString()); }
@Override public AccessToken refreshAccessToken() throws IOException { Socket socket = new Socket("localhost", this.getAuthPort()); socket.setSoTimeout(READ_TIMEOUT_MS); AccessToken token; try { OutputStream os = socket.getOutputStream(); os.write(GET_AUTH_TOKEN_REQUEST_BYTES); BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream())); input.readLine(); // Skip over the first line JsonParser parser = OAuth2Utils.JSON_FACTORY.createJsonParser(input); List<Object> messageArray = (List<Object>) parser.parseArray(ArrayList.class, Object.class); String accessToken = messageArray.get(ACCESS_TOKEN_INDEX).toString(); token = new AccessToken(accessToken, null); } finally { socket.close(); } return token; }
/** * @param authPort Authentication port. * @return The CloudShellCredentials. * @deprecated Use {@link #create(int)} instead. This method will be deleted in a later version. */ @Deprecated public static CloudShellCredentials of(int authPort) { return create(authPort); }
public CloudShellCredentials build() { return new CloudShellCredentials(authPort); } }
@Test public void hashCode_equals() throws IOException { GoogleCredentials credentials = CloudShellCredentials.newBuilder() .setAuthPort(42) .build(); GoogleCredentials otherCredentials = CloudShellCredentials.newBuilder() .setAuthPort(42) .build(); assertEquals(credentials.hashCode(), otherCredentials.hashCode()); }
@Test public void getDefaultCredentials_cloudshell() throws IOException { MockHttpTransportFactory transportFactory = new MockHttpTransportFactory(); TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider(); testProvider.setEnv(DefaultCredentialsProvider.CLOUD_SHELL_ENV_VAR, "4"); GoogleCredentials defaultCredentials = testProvider.getDefaultCredentials(transportFactory); assertTrue(defaultCredentials instanceof CloudShellCredentials); assertEquals(((CloudShellCredentials) defaultCredentials).getAuthPort(), 4); }
private GoogleCredentials tryGetCloudShellCredentials() { String port = getEnv(CLOUD_SHELL_ENV_VAR); if (port != null) { return CloudShellCredentials.create(Integer.parseInt(port)); } else { return null; } }
serverThread.start(); GoogleCredentials creds = CloudShellCredentials.newBuilder() .setAuthPort(authSocket.getLocalPort()) .build();
@Test public void getDefaultCredentials_cloudshell_withComputCredentialsPresent() throws IOException { MockMetadataServerTransportFactory transportFactory = new MockMetadataServerTransportFactory(); transportFactory.transport.setAccessToken(ACCESS_TOKEN); TestDefaultCredentialsProvider testProvider = new TestDefaultCredentialsProvider(); testProvider.setEnv(DefaultCredentialsProvider.CLOUD_SHELL_ENV_VAR, "4"); GoogleCredentials defaultCredentials = testProvider.getDefaultCredentials(transportFactory); assertTrue(defaultCredentials instanceof CloudShellCredentials); assertEquals(((CloudShellCredentials) defaultCredentials).getAuthPort(), 4); }
private GoogleCredentials tryGetCloudShellCredentials() { String port = getEnv(CLOUD_SHELL_ENV_VAR); if (port != null) { return CloudShellCredentials.create(Integer.parseInt(port)); } else { return null; } }
@Test public void equals_true() throws IOException { GoogleCredentials credentials = CloudShellCredentials.newBuilder() .setAuthPort(42) .build(); GoogleCredentials otherCredentials = CloudShellCredentials.newBuilder() .setAuthPort(42) .build(); assertTrue(credentials.equals(otherCredentials)); assertTrue(otherCredentials.equals(credentials)); }
@Test public void equals_false_authPort() throws IOException { GoogleCredentials credentials = CloudShellCredentials.newBuilder() .setAuthPort(42) .build(); GoogleCredentials otherCredentials = CloudShellCredentials.newBuilder() .setAuthPort(43) .build(); assertFalse(credentials.equals(otherCredentials)); assertFalse(otherCredentials.equals(credentials)); }
@Test public void serialize() throws IOException, ClassNotFoundException { GoogleCredentials credentials = CloudShellCredentials.newBuilder() .setAuthPort(42) .build(); GoogleCredentials deserializedCredentials = serializeAndDeserialize(credentials); assertEquals(credentials, deserializedCredentials); assertEquals(credentials.hashCode(), deserializedCredentials.hashCode()); assertEquals(credentials.toString(), deserializedCredentials.toString()); assertSame(deserializedCredentials.clock, Clock.SYSTEM); } }