001package com.box.sdk;
002
003import com.eclipsesource.json.Json;
004import com.eclipsesource.json.JsonObject;
005import java.io.IOException;
006import java.io.Reader;
007
008/** Contains Box configurations. */
009public class BoxConfig {
010
011  private String clientId;
012  private String clientSecret;
013  private String enterpriseId;
014  private JWTEncryptionPreferences jwtEncryptionPreferences;
015
016  /**
017   * Creates a configuration with a clientId and clientSecret.
018   *
019   * @param clientId the client ID of the application
020   * @param clientSecret the client secret of the application
021   */
022  public BoxConfig(String clientId, String clientSecret) {
023    this.clientId = clientId;
024    this.clientSecret = clientSecret;
025  }
026
027  /**
028   * Creates a configuration with clientId, clientSecret and JWTEncryptionPreferences.
029   *
030   * @param clientId the client ID of the application
031   * @param clientSecret the client secret of the application
032   * @param enterpriseId the enterprise ID of the box account
033   * @param jwtEncryptionPreferences the JWTEncryptionPreferences of the application
034   */
035  public BoxConfig(
036      String clientId,
037      String clientSecret,
038      String enterpriseId,
039      JWTEncryptionPreferences jwtEncryptionPreferences) {
040    this.clientId = clientId;
041    this.clientSecret = clientSecret;
042    this.enterpriseId = enterpriseId;
043    this.jwtEncryptionPreferences = jwtEncryptionPreferences;
044  }
045
046  /**
047   * Creates a configuration with clientId, clientSecret, publicKeyID, privateKey,
048   * privateKeyPassword. and an encryptionAlgorithm.
049   *
050   * @param clientId the client ID of the application
051   * @param clientSecret the client secret of the application
052   * @param enterpriseId the enterprise ID of the box account
053   * @param publicKeyID the unique ID of the uploaded public key
054   * @param privateKey the private key used to sign JWT requests
055   * @param privateKeyPassword the passphrase for the private key
056   * @param encryptionAlgorithm the encryption algorithm that has to be used for signing JWT
057   *     requests
058   */
059  public BoxConfig(
060      String clientId,
061      String clientSecret,
062      String enterpriseId,
063      String publicKeyID,
064      String privateKey,
065      String privateKeyPassword,
066      EncryptionAlgorithm encryptionAlgorithm) {
067    this.clientId = clientId;
068    this.clientSecret = clientSecret;
069    this.enterpriseId = enterpriseId;
070    this.jwtEncryptionPreferences = new JWTEncryptionPreferences();
071    this.jwtEncryptionPreferences.setPublicKeyID(publicKeyID);
072    this.jwtEncryptionPreferences.setPrivateKey(privateKey);
073    this.jwtEncryptionPreferences.setPrivateKeyPassword(privateKeyPassword);
074    this.jwtEncryptionPreferences.setEncryptionAlgorithm(encryptionAlgorithm);
075  }
076
077  /**
078   * Creates a configuration with RSA_SHA_256 as the encryption algorithm.
079   *
080   * @param clientId the client ID of the application
081   * @param clientSecret the client secret of the application
082   * @param enterpriseId the enterprise ID of the box account
083   * @param publicKeyID the unique ID of the uploaded public key
084   * @param privateKey the private key used to sign JWT requests
085   * @param privateKeyPassword the passphrase for the private key
086   */
087  public BoxConfig(
088      String clientId,
089      String clientSecret,
090      String enterpriseId,
091      String publicKeyID,
092      String privateKey,
093      String privateKeyPassword) {
094    this.clientId = clientId;
095    this.clientSecret = clientSecret;
096    this.enterpriseId = enterpriseId;
097    this.jwtEncryptionPreferences = new JWTEncryptionPreferences();
098    this.jwtEncryptionPreferences.setPublicKeyID(publicKeyID);
099    this.jwtEncryptionPreferences.setPrivateKey(privateKey);
100    this.jwtEncryptionPreferences.setPrivateKeyPassword(privateKeyPassword);
101    this.jwtEncryptionPreferences.setEncryptionAlgorithm(EncryptionAlgorithm.RSA_SHA_256);
102  }
103
104  /**
105   * Reads OAuth 2.0 with JWT app configurations from the reader. The file should be in JSON format.
106   *
107   * @param reader a reader object which points to a JSON formatted configuration file
108   * @return a new Instance of BoxConfig
109   * @throws IOException when unable to access the mapping file's content of the reader
110   */
111  public static BoxConfig readFrom(Reader reader) throws IOException {
112    return createConfigFrom(Json.parse(reader).asObject());
113  }
114
115  /**
116   * Reads OAuth 2.0 with JWT app configurations from the Json string.
117   *
118   * @param jsonString a Json stringrepresenting formatted configuration file
119   * @return a new Instance of BoxConfig
120   */
121  public static BoxConfig readFrom(String jsonString) {
122    return createConfigFrom(Json.parse(jsonString).asObject());
123  }
124
125  private static BoxConfig createConfigFrom(JsonObject config) {
126    JsonObject settings = (JsonObject) config.get("boxAppSettings");
127    String clientId = settings.get("clientID").asString();
128    String clientSecret = settings.get("clientSecret").asString();
129    JsonObject appAuth = (JsonObject) settings.get("appAuth");
130    String publicKeyId = appAuth.get("publicKeyID").asString();
131    String privateKey = appAuth.get("privateKey").asString();
132    String passphrase = appAuth.get("passphrase").asString();
133    String enterpriseId = config.get("enterpriseID").asString();
134    return new BoxConfig(clientId, clientSecret, enterpriseId, publicKeyId, privateKey, passphrase);
135  }
136
137  /** @return client secret */
138  public String getClientSecret() {
139    return this.clientSecret;
140  }
141
142  /** @param clientSecret client secret of the application */
143  public void setClientSecret(String clientSecret) {
144    this.clientSecret = clientSecret;
145  }
146
147  /** @return enterprise ID */
148  public String getEnterpriseId() {
149    return this.enterpriseId;
150  }
151
152  /** @param enterpriseId enterprise ID of the application */
153  public void setEnterpriseId(String enterpriseId) {
154    this.enterpriseId = enterpriseId;
155  }
156
157  /** @return JWT Encryption Preferences */
158  public JWTEncryptionPreferences getJWTEncryptionPreferences() {
159    return this.jwtEncryptionPreferences;
160  }
161
162  /** @param jwtEncryptionPreferences encryption preferences for JWT based authentication */
163  public void setJWTEncryptionPreferences(JWTEncryptionPreferences jwtEncryptionPreferences) {
164    this.jwtEncryptionPreferences = jwtEncryptionPreferences;
165  }
166
167  /** @return client ID */
168  public String getClientId() {
169    return this.clientId;
170  }
171
172  /** @param clientId client ID of the Application */
173  public void setClientId(String clientId) {
174    this.clientId = clientId;
175  }
176
177  /**
178   * Sets a custom decryptor used for decrypting the private key.
179   *
180   * @param privateKeyDecryptor privateKeyDecryptor the decryptor used for decrypting the private
181   *     key.
182   */
183  public void setPrivateKeyDecryptor(IPrivateKeyDecryptor privateKeyDecryptor) {
184    this.jwtEncryptionPreferences.setPrivateKeyDecryptor(privateKeyDecryptor);
185  }
186}