001package com.box.sdkgen.box.jwtauth; 002 003import static com.box.sdkgen.internal.utils.UtilsManager.readTextFromFile; 004import static com.box.sdkgen.serialization.json.JsonManager.jsonToSerializedData; 005 006import com.box.sdkgen.box.tokenstorage.InMemoryTokenStorage; 007import com.box.sdkgen.box.tokenstorage.TokenStorage; 008import com.box.sdkgen.internal.utils.DefaultPrivateKeyDecryptor; 009import com.box.sdkgen.internal.utils.JwtAlgorithm; 010import com.box.sdkgen.internal.utils.PrivateKeyDecryptor; 011import com.box.sdkgen.serialization.json.EnumWrapper; 012import com.box.sdkgen.serialization.json.JsonManager; 013 014public class JWTConfig { 015 016 /** App client ID */ 017 public final String clientId; 018 019 /** App client secret */ 020 public final String clientSecret; 021 022 /** Public key ID */ 023 public final String jwtKeyId; 024 025 /** Private key */ 026 public final String privateKey; 027 028 /** Passphrase */ 029 public final String privateKeyPassphrase; 030 031 /** Enterprise ID */ 032 public String enterpriseId; 033 034 /** User ID */ 035 public String userId; 036 037 public EnumWrapper<JwtAlgorithm> algorithm; 038 039 public TokenStorage tokenStorage; 040 041 public PrivateKeyDecryptor privateKeyDecryptor; 042 043 public JWTConfig( 044 String clientId, 045 String clientSecret, 046 String jwtKeyId, 047 String privateKey, 048 String privateKeyPassphrase) { 049 this.clientId = clientId; 050 this.clientSecret = clientSecret; 051 this.jwtKeyId = jwtKeyId; 052 this.privateKey = privateKey; 053 this.privateKeyPassphrase = privateKeyPassphrase; 054 this.algorithm = new EnumWrapper<JwtAlgorithm>(JwtAlgorithm.RS256); 055 this.tokenStorage = new InMemoryTokenStorage(); 056 this.privateKeyDecryptor = new DefaultPrivateKeyDecryptor(); 057 } 058 059 protected JWTConfig(Builder builder) { 060 this.clientId = builder.clientId; 061 this.clientSecret = builder.clientSecret; 062 this.jwtKeyId = builder.jwtKeyId; 063 this.privateKey = builder.privateKey; 064 this.privateKeyPassphrase = builder.privateKeyPassphrase; 065 this.enterpriseId = builder.enterpriseId; 066 this.userId = builder.userId; 067 this.algorithm = builder.algorithm; 068 this.tokenStorage = builder.tokenStorage; 069 this.privateKeyDecryptor = builder.privateKeyDecryptor; 070 } 071 072 /** 073 * Create an auth instance as defined by a string content of JSON file downloaded from the Box 074 * Developer Console. See https://developer.box.com/en/guides/authentication/jwt/ for more 075 * information. 076 * 077 * @param configJsonString String content of JSON file containing the configuration. 078 */ 079 public static JWTConfig fromConfigJsonString(String configJsonString) { 080 return fromConfigJsonString(configJsonString, null, null); 081 } 082 083 /** 084 * Create an auth instance as defined by a string content of JSON file downloaded from the Box 085 * Developer Console. See https://developer.box.com/en/guides/authentication/jwt/ for more 086 * information. 087 * 088 * @param configJsonString String content of JSON file containing the configuration. 089 * @param tokenStorage Object responsible for storing token. If no custom implementation provided, 090 * the token will be stored in memory 091 */ 092 public static JWTConfig fromConfigJsonString(String configJsonString, TokenStorage tokenStorage) { 093 return fromConfigJsonString(configJsonString, tokenStorage, null); 094 } 095 096 /** 097 * Create an auth instance as defined by a string content of JSON file downloaded from the Box 098 * Developer Console. See https://developer.box.com/en/guides/authentication/jwt/ for more 099 * information. 100 * 101 * @param configJsonString String content of JSON file containing the configuration. 102 * @param privateKeyDecryptor Object responsible for decrypting private key for jwt auth. If no 103 * custom implementation provided, the DefaultPrivateKeyDecryptor will be used. 104 */ 105 public static JWTConfig fromConfigJsonString( 106 String configJsonString, PrivateKeyDecryptor privateKeyDecryptor) { 107 return fromConfigJsonString(configJsonString, null, privateKeyDecryptor); 108 } 109 110 /** 111 * Create an auth instance as defined by a string content of JSON file downloaded from the Box 112 * Developer Console. See https://developer.box.com/en/guides/authentication/jwt/ for more 113 * information. 114 * 115 * @param configJsonString String content of JSON file containing the configuration. 116 * @param tokenStorage Object responsible for storing token. If no custom implementation provided, 117 * the token will be stored in memory 118 * @param privateKeyDecryptor Object responsible for decrypting private key for jwt auth. If no 119 * custom implementation provided, the DefaultPrivateKeyDecryptor will be used. 120 */ 121 public static JWTConfig fromConfigJsonString( 122 String configJsonString, TokenStorage tokenStorage, PrivateKeyDecryptor privateKeyDecryptor) { 123 JwtConfigFile configJson = 124 JsonManager.deserialize(jsonToSerializedData(configJsonString), JwtConfigFile.class); 125 TokenStorage tokenStorageToUse = 126 (tokenStorage == null ? new InMemoryTokenStorage() : tokenStorage); 127 PrivateKeyDecryptor privateKeyDecryptorToUse = 128 (privateKeyDecryptor == null ? new DefaultPrivateKeyDecryptor() : privateKeyDecryptor); 129 JWTConfig newConfig = 130 new JWTConfig.Builder( 131 configJson.getBoxAppSettings().getClientId(), 132 configJson.getBoxAppSettings().getClientSecret(), 133 configJson.getBoxAppSettings().getAppAuth().getPublicKeyId(), 134 configJson.getBoxAppSettings().getAppAuth().getPrivateKey(), 135 configJson.getBoxAppSettings().getAppAuth().getPassphrase()) 136 .enterpriseId(configJson.getEnterpriseId()) 137 .userId(configJson.getUserId()) 138 .tokenStorage(tokenStorageToUse) 139 .privateKeyDecryptor(privateKeyDecryptorToUse) 140 .build(); 141 return newConfig; 142 } 143 144 /** 145 * Create an auth instance as defined by a JSON file downloaded from the Box Developer Console. 146 * See https://developer.box.com/en/guides/authentication/jwt/ for more information. 147 * 148 * @param configFilePath Path to the JSON file containing the configuration. 149 */ 150 public static JWTConfig fromConfigFile(String configFilePath) { 151 return fromConfigFile(configFilePath, null, null); 152 } 153 154 /** 155 * Create an auth instance as defined by a JSON file downloaded from the Box Developer Console. 156 * See https://developer.box.com/en/guides/authentication/jwt/ for more information. 157 * 158 * @param configFilePath Path to the JSON file containing the configuration. 159 * @param tokenStorage Object responsible for storing token. If no custom implementation provided, 160 * the token will be stored in memory. 161 */ 162 public static JWTConfig fromConfigFile(String configFilePath, TokenStorage tokenStorage) { 163 return fromConfigFile(configFilePath, tokenStorage, null); 164 } 165 166 /** 167 * Create an auth instance as defined by a JSON file downloaded from the Box Developer Console. 168 * See https://developer.box.com/en/guides/authentication/jwt/ for more information. 169 * 170 * @param configFilePath Path to the JSON file containing the configuration. 171 * @param privateKeyDecryptor Object responsible for decrypting private key for jwt auth. If no 172 * custom implementation provided, the DefaultPrivateKeyDecryptor will be used. 173 */ 174 public static JWTConfig fromConfigFile( 175 String configFilePath, PrivateKeyDecryptor privateKeyDecryptor) { 176 return fromConfigFile(configFilePath, null, privateKeyDecryptor); 177 } 178 179 /** 180 * Create an auth instance as defined by a JSON file downloaded from the Box Developer Console. 181 * See https://developer.box.com/en/guides/authentication/jwt/ for more information. 182 * 183 * @param configFilePath Path to the JSON file containing the configuration. 184 * @param tokenStorage Object responsible for storing token. If no custom implementation provided, 185 * the token will be stored in memory. 186 * @param privateKeyDecryptor Object responsible for decrypting private key for jwt auth. If no 187 * custom implementation provided, the DefaultPrivateKeyDecryptor will be used. 188 */ 189 public static JWTConfig fromConfigFile( 190 String configFilePath, TokenStorage tokenStorage, PrivateKeyDecryptor privateKeyDecryptor) { 191 String configJsonString = readTextFromFile(configFilePath); 192 return JWTConfig.fromConfigJsonString(configJsonString, tokenStorage, privateKeyDecryptor); 193 } 194 195 public String getClientId() { 196 return clientId; 197 } 198 199 public String getClientSecret() { 200 return clientSecret; 201 } 202 203 public String getJwtKeyId() { 204 return jwtKeyId; 205 } 206 207 public String getPrivateKey() { 208 return privateKey; 209 } 210 211 public String getPrivateKeyPassphrase() { 212 return privateKeyPassphrase; 213 } 214 215 public String getEnterpriseId() { 216 return enterpriseId; 217 } 218 219 public String getUserId() { 220 return userId; 221 } 222 223 public EnumWrapper<JwtAlgorithm> getAlgorithm() { 224 return algorithm; 225 } 226 227 public TokenStorage getTokenStorage() { 228 return tokenStorage; 229 } 230 231 public PrivateKeyDecryptor getPrivateKeyDecryptor() { 232 return privateKeyDecryptor; 233 } 234 235 public static class Builder { 236 237 protected final String clientId; 238 239 protected final String clientSecret; 240 241 protected final String jwtKeyId; 242 243 protected final String privateKey; 244 245 protected final String privateKeyPassphrase; 246 247 protected String enterpriseId; 248 249 protected String userId; 250 251 protected EnumWrapper<JwtAlgorithm> algorithm; 252 253 protected TokenStorage tokenStorage; 254 255 protected PrivateKeyDecryptor privateKeyDecryptor; 256 257 public Builder( 258 String clientId, 259 String clientSecret, 260 String jwtKeyId, 261 String privateKey, 262 String privateKeyPassphrase) { 263 this.clientId = clientId; 264 this.clientSecret = clientSecret; 265 this.jwtKeyId = jwtKeyId; 266 this.privateKey = privateKey; 267 this.privateKeyPassphrase = privateKeyPassphrase; 268 } 269 270 public Builder enterpriseId(String enterpriseId) { 271 this.enterpriseId = enterpriseId; 272 return this; 273 } 274 275 public Builder userId(String userId) { 276 this.userId = userId; 277 return this; 278 } 279 280 public Builder algorithm(JwtAlgorithm algorithm) { 281 this.algorithm = new EnumWrapper<JwtAlgorithm>(algorithm); 282 return this; 283 } 284 285 public Builder algorithm(EnumWrapper<JwtAlgorithm> algorithm) { 286 this.algorithm = algorithm; 287 return this; 288 } 289 290 public Builder tokenStorage(TokenStorage tokenStorage) { 291 this.tokenStorage = tokenStorage; 292 return this; 293 } 294 295 public Builder privateKeyDecryptor(PrivateKeyDecryptor privateKeyDecryptor) { 296 this.privateKeyDecryptor = privateKeyDecryptor; 297 return this; 298 } 299 300 public JWTConfig build() { 301 if (this.algorithm == null) { 302 this.algorithm = new EnumWrapper<JwtAlgorithm>(JwtAlgorithm.RS256); 303 } 304 if (this.tokenStorage == null) { 305 this.tokenStorage = new InMemoryTokenStorage(); 306 } 307 if (this.privateKeyDecryptor == null) { 308 this.privateKeyDecryptor = new DefaultPrivateKeyDecryptor(); 309 } 310 return new JWTConfig(this); 311 } 312 } 313}