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