/** * Constructs a Client ID from JSON file stored as a resource. * * @param relativeClass A class in the same namespace as the resource. * @param resourceName The name of the resource * @return The constructed ClientID instance based on the JSON in the resource. * @throws IOException The JSON could not be loaded or parsed. */ public static ClientId fromResource(Class<?> relativeClass, String resourceName) throws IOException { InputStream stream = relativeClass.getResourceAsStream(resourceName); return fromStream(stream); }
protected Builder(ClientId clientId) { this.clientId = clientId.getClientId(); this.clientSecret = clientId.getClientSecret(); }
public ClientId build() { return new ClientId(clientId, clientSecret); } }
@Test public void fromJson_installedNoSecret() throws IOException { GenericJson json = writeClientIdJson("installed", CLIENT_ID, null); ClientId clientId = ClientId.fromJson(json); assertEquals(CLIENT_ID, clientId.getClientId()); assertNull(clientId.getClientSecret()); }
@Test public void fromStream() throws IOException { String text = "{" + "\"web\": {" + "\"client_id\" : \"" + CLIENT_ID + "\"," + "\"client_secret\" : \"" + CLIENT_SECRET + "\"" + "}" + "}"; InputStream stream = TestUtils.stringToInputStream(text); ClientId clientId = ClientId.fromStream(stream); assertEquals(CLIENT_ID, clientId.getClientId()); assertEquals(CLIENT_SECRET, clientId.getClientSecret()); }
@Test public void constructor_nullClientSecret() { ClientId clientId = ClientId.newBuilder() .setClientId(CLIENT_ID) .build(); assertEquals(CLIENT_ID, clientId.getClientId()); assertNull(clientId.getClientSecret()); }
@Test public void fromResource() throws IOException { ClientId clientId = ClientId.fromResource(ClientIdTest.class, "/client_secret.json"); assertEquals(CLIENT_ID, clientId.getClientId()); assertEquals(CLIENT_SECRET, clientId.getClientSecret()); }
/** * Constructs a Client ID from JSON file stream. * * @param stream Stream of the downloaded JSON file. * @return The constructed ClientID instance based on the JSON in the stream. * @throws IOException The JSON could not be read or parsed. */ public static ClientId fromStream(InputStream stream) throws IOException { Preconditions.checkNotNull(stream); JsonObjectParser parser = new JsonObjectParser(OAuth2Utils.JSON_FACTORY); GenericJson parsedJson = parser.parseAndClose( stream, StandardCharsets.UTF_8, GenericJson.class); return fromJson(parsedJson); }
/** * Return an URL that performs the authorization consent prompt web UI. * * @param userId Application's identifier for the end user. * @param state State that is passed on to the OAuth2 callback URI after the consent. * @param baseUri The URI to resolve the OAuth2 callback URI relative to. * @return The URL that can be navigated or redirected to. */ public URL getAuthorizationUrl(String userId, String state, URI baseUri) { URI resolvedCallbackUri = getCallbackUri(baseUri); String scopesString = Joiner.on(' ').join(scopes); GenericUrl url = new GenericUrl(userAuthUri); url.put("response_type", "code"); url.put("client_id", clientId.getClientId()); url.put("redirect_uri", resolvedCallbackUri); url.put("scope", scopesString); if (state != null) { url.put("state", state); } url.put("access_type", "offline"); url.put("approval_prompt", "force"); if (userId != null) { url.put("login_hint", userId); } url.put("include_granted_scopes", true); return url.toURL(); }
@Test(expected = NullPointerException.class) public void fromResource_badResource() throws IOException { ClientId.fromResource(ClientIdTest.class, "invalid.json"); }
@Test(expected = NullPointerException.class) public void constructor_nullClientId_throws() { ClientId.newBuilder() .setClientSecret(CLIENT_SECRET) .build(); }
@Test public void fromJson_web() throws IOException { GenericJson json = writeClientIdJson("web", CLIENT_ID, CLIENT_SECRET); ClientId clientId = ClientId.fromJson(json); assertEquals(CLIENT_ID, clientId.getClientId()); assertEquals(CLIENT_SECRET, clientId.getClientSecret()); }
@Test public void constructor() { ClientId clientId = ClientId.newBuilder() .setClientId(CLIENT_ID) .setClientSecret(CLIENT_SECRET) .build(); assertEquals(CLIENT_ID, clientId.getClientId()); assertEquals(CLIENT_SECRET, clientId.getClientSecret()); }
/** * Constructs a Client ID from JSON file stream. * * @param stream Stream of the downloaded JSON file. * @return The constructed ClientID instance based on the JSON in the stream. * @throws IOException The JSON could not be read or parsed. */ public static ClientId fromStream(InputStream stream) throws IOException { Preconditions.checkNotNull(stream); JsonObjectParser parser = new JsonObjectParser(OAuth2Utils.JSON_FACTORY); GenericJson parsedJson = parser.parseAndClose( stream, StandardCharsets.UTF_8, GenericJson.class); return fromJson(parsedJson); }
/** * Return an URL that performs the authorization consent prompt web UI. * * @param userId Application's identifier for the end user. * @param state State that is passed on to the OAuth2 callback URI after the consent. * @param baseUri The URI to resolve the OAuth2 callback URI relative to. * @return The URL that can be navigated or redirected to. */ public URL getAuthorizationUrl(String userId, String state, URI baseUri) { URI resolvedCallbackUri = getCallbackUri(baseUri); String scopesString = Joiner.on(' ').join(scopes); GenericUrl url = new GenericUrl(userAuthUri); url.put("response_type", "code"); url.put("client_id", clientId.getClientId()); url.put("redirect_uri", resolvedCallbackUri); url.put("scope", scopesString); if (state != null) { url.put("state", state); } url.put("access_type", "offline"); url.put("approval_prompt", "force"); if (userId != null) { url.put("login_hint", userId); } url.put("include_granted_scopes", true); return url.toURL(); }
@Test public void fromJson_installed() throws IOException { GenericJson json = writeClientIdJson("installed", CLIENT_ID, CLIENT_SECRET); ClientId clientId = ClientId.fromJson(json); assertEquals(CLIENT_ID, clientId.getClientId()); assertEquals(CLIENT_SECRET, clientId.getClientSecret()); }
protected Builder(ClientId clientId) { this.clientId = clientId.getClientId(); this.clientSecret = clientId.getClientSecret(); }
@Test(expected = IOException.class) public void fromJson_zeroLengthClientId_throws() throws IOException { GenericJson json = writeClientIdJson("web", "", null); ClientId.fromJson(json); }
/** * Constructs a Client ID from JSON file stored as a resource. * * @param relativeClass A class in the same namespace as the resource. * @param resourceName The name of the resource * @return The constructed ClientID instance based on the JSON in the resource. * @throws IOException The JSON could not be loaded or parsed. */ public static ClientId fromResource(Class<?> relativeClass, String resourceName) throws IOException { InputStream stream = relativeClass.getResourceAsStream(resourceName); return fromStream(stream); }
/** * Constructs a client ID from an explicit ID and secret. * * <p>Note: Direct use of this factory method in application code is not recommended to avoid * having secrets or values that need to be updated in source code.</p> * * @param clientId Text identifier of the Client ID. * @param clientSecret Secret to associated with the Client ID. * @return The ClientId instance. */ public static ClientId of(String clientId, String clientSecret) { return new ClientId(clientId, clientSecret); }