All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rt.storage.auth.oauth2.ClientId Maven / Gradle / Ivy

package com.rt.storage.auth.oauth2;

import com.rt.storage.api.client.json.GenericJson;
import com.rt.storage.api.client.json.JsonObjectParser;
import com.rt.storage.api.client.util.Preconditions;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * An OAuth2 user authorization Client ID and associated information.
 *
 * 

Corresponds to the information in the json file downloadable for a Client ID. */ public class ClientId { private static final String FIELD_TYPE_INSTALLED = "installed"; private static final String FIELD_TYPE_WEB = "web"; private static final String FIELD_CLIENT_ID = "client_id"; private static final String FIELD_CLIENT_SECRET = "client_secret"; private static final String JSON_PARSE_ERROR = "Error parsing Client ID JSON: "; private final String clientId; private final String clientSecret; /** * Constructs a client ID from an explicit ID and secret. * *

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. * * @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); } /** * Constructs a Client ID from JSON from a downloaded file. * * @param json The JSON from the downloaded file. * @return the ClientId instance based on the JSON. * @throws IOException The JSON could not be parsed. */ public static ClientId fromJson(Map json) throws IOException { Object rawDetail = null; rawDetail = json.get(FIELD_TYPE_INSTALLED); if (rawDetail == null) { rawDetail = json.get(FIELD_TYPE_WEB); } if (rawDetail == null || !(rawDetail instanceof Map)) { throw new IOException( "Unable to parse Client ID JSON. Expecting top-level field '" + FIELD_TYPE_WEB + "' or '" + FIELD_TYPE_INSTALLED + "' of collection type"); } @SuppressWarnings("unchecked") Map detail = (Map) rawDetail; String clientId = OAuth2Utils.validateString(detail, FIELD_CLIENT_ID, JSON_PARSE_ERROR); if (clientId == null || clientId.length() == 0) { throw new IOException( "Unable to parse ClientId. Field '" + FIELD_CLIENT_ID + "' is required."); } String clientSecret = OAuth2Utils.validateOptionalString(detail, FIELD_CLIENT_SECRET, JSON_PARSE_ERROR); return new ClientId(clientId, clientSecret); } /** * 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 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); } /** * Constructs a client ID using an explicit ID and secret * *

Note: Direct use of this constructor in application code is not recommended to avoid having * secrets or values that need to be updated in source code. * * @param clientId Text identifier of the Client ID. * @param clientSecret Secret to associated with the Client ID. */ private ClientId(String clientId, String clientSecret) { this.clientId = Preconditions.checkNotNull(clientId); this.clientSecret = clientSecret; } /** * Returns the text identifier of the Client ID. * * @return The text identifier of the Client ID. */ public final String getClientId() { return clientId; } /** * Returns the secret associated with the Client ID. * * @return The secret associated with the Client ID. */ public final String getClientSecret() { return clientSecret; } public static Builder newBuilder() { return new Builder(); } public Builder toBuilder() { return new Builder(this); } public static class Builder { private String clientId; private String clientSecret; protected Builder() {} protected Builder(ClientId clientId) { this.clientId = clientId.getClientId(); this.clientSecret = clientId.getClientSecret(); } public Builder setClientId(String clientId) { this.clientId = clientId; return this; } public Builder setClientSecret(String clientSecret) { this.clientSecret = clientSecret; return this; } public String getClientSecret() { return clientSecret; } public ClientId build() { return new ClientId(clientId, clientSecret); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy