Skip to content

Commit bb27843

Browse files
SDK regenerated by CI server [ci skip]
1 parent 228898c commit bb27843

19 files changed

+1767
-224
lines changed

src/main/java/com/aspose/words/cloud/ApiClient.java

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
import com.aspose.words.cloud.model.requests.*;
3131
import com.squareup.okhttp.*;
3232
import com.squareup.okhttp.internal.http.HttpMethod;
33-
import com.squareup.okhttp.logging.HttpLoggingInterceptor;
34-
import com.squareup.okhttp.logging.HttpLoggingInterceptor.Level;
3533
import okio.BufferedSink;
3634
import okio.Okio;
3735
import org.threeten.bp.LocalDate;
@@ -40,23 +38,15 @@
4038
import javax.crypto.BadPaddingException;
4139
import javax.crypto.Cipher;
4240
import javax.crypto.IllegalBlockSizeException;
43-
import javax.crypto.NoSuchPaddingException;
4441
import javax.mail.BodyPart;
4542
import javax.mail.MessagingException;
4643
import javax.mail.internet.MimeMultipart;
4744
import javax.mail.util.ByteArrayDataSource;
4845
import java.io.*;
4946
import java.lang.reflect.Type;
50-
import java.math.BigInteger;
5147
import java.net.URLConnection;
5248
import java.net.URLEncoder;
5349
import java.nio.charset.StandardCharsets;
54-
import java.security.InvalidKeyException;
55-
import java.security.KeyFactory;
56-
import java.security.NoSuchAlgorithmException;
57-
import java.security.PublicKey;
58-
import java.security.spec.InvalidKeySpecException;
59-
import java.security.spec.RSAPublicKeySpec;
6050
import java.util.*;
6151
import java.util.Map.Entry;
6252
import java.util.concurrent.TimeUnit;
@@ -84,7 +74,8 @@ public class ApiClient {
8474
private String refreshToken;
8575
private String ClientSecret;
8676
private String clientId;
87-
private Cipher key;
77+
private Cipher encryptor;
78+
private EncryptorFactory encryptorProvider;
8879

8980
public ApiClient(String clientId, String clientSecret, String baseUrl) {
9081
this();
@@ -111,22 +102,23 @@ public ApiClient() {
111102
setReadTimeout(180);
112103
}
113104

114-
115105
/**
116-
* Gets a public key
117-
* @return public key
106+
* Gets an encryptor provider
107+
*
108+
* @return encryptor provider
118109
*/
119-
public Cipher getKey() {
120-
return key;
110+
public EncryptorFactory getEncryptorProvider() {
111+
return encryptorProvider;
121112
}
122113

123114
/**
124-
* Sets a public key
125-
* @param key
115+
* Sets an encryptor provider
116+
*
117+
* @param encryptorProvider
126118
* @return api client
127119
*/
128-
public ApiClient setKey(Cipher key) {
129-
this.key = key;
120+
public ApiClient setEncryptorProvider(EncryptorFactory encryptorProvider) {
121+
this.encryptorProvider = encryptorProvider;
130122
return this;
131123
}
132124

@@ -1046,30 +1038,12 @@ public void requestToken() throws ApiException {
10461038
}
10471039
}
10481040

1049-
public void setRsaKey(String modulus, String exponent) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException {
1050-
byte[] modulusByte = Base64.getDecoder().decode(modulus);
1051-
BigInteger modulusInt = new BigInteger(1, modulusByte);
1052-
byte[] exponentByte = Base64.getDecoder().decode(exponent);
1053-
BigInteger exponentInt = new BigInteger(1, exponentByte);
1054-
RSAPublicKeySpec spec = new RSAPublicKeySpec(modulusInt, exponentInt);
1055-
KeyFactory factory = KeyFactory.getInstance("RSA");
1056-
PublicKey key = factory.generatePublic(spec);
1057-
this.key = Cipher.getInstance("RSA");
1058-
this.key.init(Cipher.ENCRYPT_MODE, key);
1059-
}
1060-
10611041
/**
10621042
* AddParameterToQuery
10631043
*/
1064-
public void addParameterToQuery(List<Pair> queryParams, String paramName, Object paramValue) {
1044+
public void addParameterToQuery(List<Pair> queryParams, String paramName, Object paramValue) throws ApiException, IOException {
10651045
if (paramName.equals("password") && paramValue != null && !paramValue.toString().isEmpty()) {
1066-
try {
1067-
queryParams.addAll(parameterToPair("encryptedPassword", Base64.getEncoder().encode(this.key.doFinal(paramValue.toString().getBytes(StandardCharsets.UTF_8)))));
1068-
}
1069-
catch (IllegalBlockSizeException e) {
1070-
}
1071-
catch (BadPaddingException e) {
1072-
}
1046+
queryParams.addAll(parameterToPair("encryptedPassword", encrypt((String) paramValue)));
10731047
}
10741048
else {
10751049
queryParams.addAll(parameterToPair(paramName, paramValue));
@@ -1256,6 +1230,31 @@ public Object parseBatchPart(RequestIfc apiRequest, Request sysRequest, BodyPart
12561230
}
12571231
}
12581232

1233+
/**
1234+
* Encrypt string to base64-encoded string
1235+
*/
1236+
public String encrypt(String data) throws ApiException, IOException {
1237+
if (data != null && !data.isEmpty()) {
1238+
return null;
1239+
}
1240+
1241+
if (this.encryptor == null) {
1242+
if (this.encryptorProvider == null) {
1243+
throw new ApiException("Encryption error: EncryptorProvider isn't set.");
1244+
}
1245+
1246+
this.encryptor = this.encryptorProvider.create();
1247+
}
1248+
1249+
try {
1250+
return Base64.getEncoder()
1251+
.encodeToString(this.encryptor.doFinal(data.toString().getBytes(StandardCharsets.UTF_8)));
1252+
}
1253+
catch (IllegalBlockSizeException | BadPaddingException e) {
1254+
throw new ApiException(e);
1255+
}
1256+
}
1257+
12591258
/**
12601259
* Find part in multipart by name
12611260
*/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* --------------------------------------------------------------------------------
3+
* <copyright company="Aspose" file="EncryptorFactory.java">
4+
* Copyright (c) 2022 Aspose.Words for Cloud
5+
* </copyright>
6+
* <summary>
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
* </summary>
25+
* --------------------------------------------------------------------------------
26+
*/
27+
28+
package com.aspose.words.cloud;
29+
import java.io.IOException;
30+
import javax.crypto.Cipher;
31+
32+
/**
33+
* Data encryptor provider.
34+
*/
35+
public interface EncryptorFactory {
36+
/**
37+
* Create an encryptor.
38+
*
39+
* @return encryptor.
40+
*/
41+
Cipher create() throws ApiException, IOException;
42+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* --------------------------------------------------------------------------------
3+
* <copyright company="Aspose" file="SimpleEncryptorFactory.java">
4+
* Copyright (c) 2022 Aspose.Words for Cloud
5+
* </copyright>
6+
* <summary>
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
* </summary>
25+
* --------------------------------------------------------------------------------
26+
*/
27+
28+
package com.aspose.words.cloud;
29+
30+
import java.io.IOException;
31+
import java.math.BigInteger;
32+
import java.security.InvalidKeyException;
33+
import java.security.KeyFactory;
34+
import java.security.NoSuchAlgorithmException;
35+
import java.security.PublicKey;
36+
import java.security.spec.InvalidKeySpecException;
37+
import java.security.spec.RSAPublicKeySpec;
38+
import java.util.Base64;
39+
40+
import javax.crypto.Cipher;
41+
import javax.crypto.NoSuchPaddingException;
42+
43+
public class SimpleEncryptorFactory implements EncryptorFactory {
44+
45+
private String exponent;
46+
private String modulus;
47+
48+
public SimpleEncryptorFactory(String exponent, String modulus) {
49+
if (exponent == null || exponent.isEmpty()) {
50+
throw new IllegalArgumentException("Exponent must be non empty base64 encoded string");
51+
}
52+
53+
if (modulus == null || modulus.isEmpty()) {
54+
throw new IllegalArgumentException("Modulus must be non empty base64 encoded string");
55+
}
56+
57+
this.exponent = exponent;
58+
this.modulus = modulus;
59+
}
60+
61+
@Override
62+
public Cipher create() throws ApiException, IOException {
63+
64+
try {
65+
byte[] modulusByte = Base64.getDecoder().decode(this.modulus);
66+
BigInteger modulusInt = new BigInteger(1, modulusByte);
67+
byte[] exponentByte = Base64.getDecoder().decode(this.exponent);
68+
BigInteger exponentInt = new BigInteger(1, exponentByte);
69+
RSAPublicKeySpec spec = new RSAPublicKeySpec(modulusInt, exponentInt);
70+
KeyFactory factory = KeyFactory.getInstance("RSA");
71+
PublicKey key = factory.generatePublic(spec);
72+
Cipher encryptor = Cipher.getInstance("RSA");
73+
encryptor.init(Cipher.ENCRYPT_MODE, key);
74+
75+
return encryptor;
76+
}
77+
catch (InvalidKeyException | NoSuchAlgorithmException | InvalidKeySpecException | NoSuchPaddingException e) {
78+
throw new ApiException(e);
79+
}
80+
}
81+
}

src/main/java/com/aspose/words/cloud/api/WordsApi.java

Lines changed: 15 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,19 @@
3131
import com.aspose.words.cloud.model.*;
3232
import com.aspose.words.cloud.model.requests.*;
3333
import com.aspose.words.cloud.model.responses.*;
34-
import com.google.gson.reflect.TypeToken;
3534
import com.squareup.okhttp.Response;
3635
import java.io.IOException;
37-
import javax.crypto.NoSuchPaddingException;
3836
import javax.mail.BodyPart;
37+
import javax.crypto.Cipher;
3938
import javax.mail.MessagingException;
4039
import java.lang.reflect.Type;
4140
import java.security.InvalidKeyException;
4241
import java.security.NoSuchAlgorithmException;
4342
import java.security.spec.InvalidKeySpecException;
4443
import java.util.*;
44+
import java.io.File;
4545

46-
public class WordsApi {
46+
public class WordsApi implements EncryptorFactory {
4747
private ApiClient apiClient;
4848

4949
public WordsApi(String clientId, String clientSecret, String baseUrl) {
@@ -56,23 +56,7 @@ public WordsApi() {
5656

5757
public WordsApi(ApiClient apiClient) {
5858
this.apiClient = apiClient;
59-
try {
60-
this.checkRsaKey();
61-
}
62-
catch (ApiException e) {
63-
}
64-
catch (MessagingException e) {
65-
}
66-
catch (IOException e) {
67-
}
68-
catch (InvalidKeySpecException e) {
69-
}
70-
catch (NoSuchAlgorithmException e) {
71-
}
72-
catch (NoSuchPaddingException e) {
73-
}
74-
catch (InvalidKeyException e) {
75-
}
59+
this.apiClient.setEncryptorProvider(this);
7660
}
7761

7862
public ApiClient getApiClient() {
@@ -81,23 +65,6 @@ public ApiClient getApiClient() {
8165

8266
public void setApiClient(ApiClient apiClient) {
8367
this.apiClient = apiClient;
84-
try {
85-
this.checkRsaKey();
86-
}
87-
catch (ApiException e) {
88-
}
89-
catch (MessagingException e) {
90-
}
91-
catch (IOException e) {
92-
}
93-
catch (InvalidKeySpecException e) {
94-
}
95-
catch (NoSuchAlgorithmException e) {
96-
}
97-
catch (NoSuchPaddingException e) {
98-
}
99-
catch (InvalidKeyException e) {
100-
}
10168
}
10269

10370
@SuppressWarnings("rawtypes")
@@ -22543,8 +22510,16 @@ public Object deserializeResponse(ApiClient apiClient, Response response) throws
2254322510
return (Object[]) (apiClient.execute(call, internalRequest).getData());
2254422511
}
2254522512

22546-
private void checkRsaKey() throws ApiException, MessagingException, IOException, InvalidKeySpecException, NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException {
22547-
PublicKeyResponse pkResponse = this.getPublicKey(new GetPublicKeyRequest());
22548-
this.apiClient.setRsaKey(pkResponse.getModulus(), pkResponse.getExponent());
22513+
@Override
22514+
public Cipher create() throws ApiException, IOException {
22515+
22516+
try {
22517+
PublicKeyResponse pkResponse = this.getPublicKey(new GetPublicKeyRequest());
22518+
EncryptorFactory factory = new SimpleEncryptorFactory(pkResponse.getExponent(), pkResponse.getModulus());
22519+
return factory.create();
22520+
}
22521+
catch (MessagingException e) {
22522+
throw new ApiException(e);
22523+
}
2254922524
}
2255022525
}

0 commit comments

Comments
 (0)