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