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 JsonObject config = Json.parse(reader).asObject(); 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 * @return client secret 113 */ 114 public String getClientSecret() { 115 return this.clientSecret; 116 } 117 118 /** 119 * @param clientSecret client secret of the application 120 */ 121 public void setClientSecret(String clientSecret) { 122 this.clientSecret = clientSecret; 123 } 124 125 /** 126 * @return enterprise ID 127 */ 128 public String getEnterpriseId() { 129 return this.enterpriseId; 130 } 131 132 /** 133 * @param enterpriseId enterprise ID of the application 134 */ 135 public void setEnterpriseId(String enterpriseId) { 136 this.enterpriseId = enterpriseId; 137 } 138 139 /** 140 * @return JWT Encryption Preferences 141 */ 142 public JWTEncryptionPreferences getJWTEncryptionPreferences() { 143 return this.jwtEncryptionPreferences; 144 } 145 146 /** 147 * @param jwtEncryptionPreferences encryption preferences for JWT based authentication 148 */ 149 public void setJWTEncryptionPreferences(JWTEncryptionPreferences jwtEncryptionPreferences) { 150 this.jwtEncryptionPreferences = jwtEncryptionPreferences; 151 } 152 153 /** 154 * @return client ID 155 */ 156 public String getClientId() { 157 return this.clientId; 158 } 159 160 /** 161 * @param clientId client ID of the Application 162 */ 163 public void setClientId(String clientId) { 164 this.clientId = clientId; 165 } 166}