001package com.box.sdk; 002 003import java.io.IOException; 004import java.io.Reader; 005 006import com.eclipsesource.json.JsonObject; 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 JsonObject config = JsonObject.readFrom(reader); 100 JsonObject settings = (JsonObject) config.get("boxAppSettings"); 101 String clientId = settings.get("clientID").asString(); 102 String clientSecret = settings.get("clientSecret").asString(); 103 JsonObject appAuth = (JsonObject) settings.get("appAuth"); 104 String publicKeyId = appAuth.get("publicKeyID").asString(); 105 String privateKey = appAuth.get("privateKey").asString(); 106 String passphrase = appAuth.get("passphrase").asString(); 107 String enterpriseId = config.get("enterpriseID").asString(); 108 return new BoxConfig(clientId, clientSecret, enterpriseId, publicKeyId, privateKey, passphrase); 109 } 110 111 /** 112 * 113 * @param clientId client ID of the Application 114 */ 115 public void setClientId(String clientId) { 116 this.clientId = clientId; 117 } 118 119 /** 120 * 121 * @return client secret 122 */ 123 public String getClientSecret() { 124 return this.clientSecret; 125 } 126 127 /** 128 * 129 * @param clientSecret client secret of the application 130 */ 131 public void setClientSecret(String clientSecret) { 132 this.clientSecret = clientSecret; 133 } 134 135 /** 136 * 137 * @return enterprise ID 138 */ 139 public String getEnterpriseId() { 140 return this.enterpriseId; 141 } 142 143 /** 144 * 145 * @param enterpriseId enterprise ID of the application 146 */ 147 public void setEnterpriseId(String enterpriseId) { 148 this.enterpriseId = enterpriseId; 149 } 150 151 /** 152 * 153 * @return JWT Encryption Preferences 154 */ 155 public JWTEncryptionPreferences getJWTEncryptionPreferences() { 156 return this.jwtEncryptionPreferences; 157 } 158 159 /** 160 * 161 * @return client ID 162 */ 163 public String getClientId() { 164 return this.clientId; 165 } 166 167 /** 168 * 169 * @param jwtEncryptionPreferences encryption preferences for JWT based authentication 170 */ 171 public void setJWTEncryptionPreferences(JWTEncryptionPreferences jwtEncryptionPreferences) { 172 this.jwtEncryptionPreferences = jwtEncryptionPreferences; 173 } 174}