Skip to content

Commit 05082e8

Browse files
author
Stanislav Parshin
authored
Decrypt passport (#137)
Added support for Telegram Passport with decryption API
1 parent b72ff20 commit 05082e8

26 files changed

+3177
-9
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ build
55
out
66

77
.DS_Store
8-
local.properties
8+
local.properties
9+
private.key

library/src/main/java/com/pengrad/telegrambot/BotUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.google.gson.Gson;
44
import com.pengrad.telegrambot.model.Update;
55

6+
import java.io.ByteArrayOutputStream;
7+
import java.io.IOException;
8+
import java.io.InputStream;
69
import java.io.Reader;
710

811
/**
@@ -21,4 +24,13 @@ public static Update parseUpdate(Reader reader) {
2124
return gson.fromJson(reader, Update.class);
2225
}
2326

27+
static byte[] getBytesFromInputStream(InputStream is) throws IOException {
28+
ByteArrayOutputStream os = new ByteArrayOutputStream();
29+
byte[] buffer = new byte[0xFFFF];
30+
for (int len = is.read(buffer); len != -1; len = is.read(buffer)) {
31+
os.write(buffer, 0, len);
32+
}
33+
return os.toByteArray();
34+
}
35+
2436
}

library/src/main/java/com/pengrad/telegrambot/TelegramBot.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
import com.pengrad.telegrambot.request.BaseRequest;
99
import com.pengrad.telegrambot.request.GetUpdates;
1010
import com.pengrad.telegrambot.response.BaseResponse;
11+
12+
import java.io.InputStream;
13+
import java.net.URL;
14+
import java.net.URLConnection;
15+
1116
import okhttp3.Interceptor;
1217
import okhttp3.OkHttpClient;
1318
import okhttp3.logging.HttpLoggingInterceptor;
@@ -44,6 +49,15 @@ public String getFullFilePath(File file) {
4449
return fileApi.getFullFilePath(file.filePath());
4550
}
4651

52+
public byte[] getFileContent(File file) throws Exception {
53+
String fileUrl = getFullFilePath(file);
54+
URLConnection connection = new URL(fileUrl).openConnection();
55+
InputStream is = connection.getInputStream();
56+
byte[] data = BotUtils.getBytesFromInputStream(is);
57+
is.close();
58+
return data;
59+
}
60+
4761
public void setUpdatesListener(UpdatesListener listener) {
4862
setUpdatesListener(listener, new GetUpdates());
4963
}

library/src/main/java/com/pengrad/telegrambot/model/Message.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.pengrad.telegrambot.model;
22

3+
import com.pengrad.telegrambot.passport.PassportData;
4+
35
import java.io.Serializable;
46
import java.util.Arrays;
57

@@ -54,6 +56,7 @@ public class Message implements Serializable {
5456
private Invoice invoice;
5557
private SuccessfulPayment successful_payment;
5658
private String connected_website;
59+
private PassportData passport_data;
5760

5861
public Integer messageId() {
5962
return message_id;
@@ -235,6 +238,10 @@ public String connectedWebsite() {
235238
return connected_website;
236239
}
237240

241+
public PassportData passportData() {
242+
return passport_data;
243+
}
244+
238245
@Override
239246
public boolean equals(Object o) {
240247
if (this == o) return true;
@@ -307,7 +314,9 @@ public boolean equals(Object o) {
307314
if (invoice != null ? !invoice.equals(message.invoice) : message.invoice != null) return false;
308315
if (successful_payment != null ? !successful_payment.equals(message.successful_payment) : message.successful_payment != null)
309316
return false;
310-
return connected_website != null ? connected_website.equals(message.connected_website) : message.connected_website == null;
317+
if (connected_website != null ? !connected_website.equals(message.connected_website) : message.connected_website != null)
318+
return false;
319+
return passport_data != null ? passport_data.equals(message.passport_data) : message.passport_data == null;
311320
}
312321

313322
@Override
@@ -362,6 +371,7 @@ public String toString() {
362371
", invoice=" + invoice +
363372
", successful_payment=" + successful_payment +
364373
", connected_website='" + connected_website + '\'' +
374+
", passport_data=" + passport_data +
365375
'}';
366376
}
367377
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.pengrad.telegrambot.passport;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Stas Parshin
7+
* 31 July 2018
8+
*/
9+
public class Credentials implements Serializable {
10+
private final static long serialVersionUID = 0L;
11+
12+
private SecureData secure_data;
13+
private String payload;
14+
15+
public SecureData secureData() {
16+
return secure_data;
17+
}
18+
19+
public String payload() {
20+
return payload;
21+
}
22+
23+
@Override
24+
public boolean equals(Object o) {
25+
if (this == o) return true;
26+
if (o == null || getClass() != o.getClass()) return false;
27+
28+
Credentials that = (Credentials) o;
29+
30+
if (secure_data != null ? !secure_data.equals(that.secure_data) : that.secure_data != null) return false;
31+
return payload != null ? payload.equals(that.payload) : that.payload == null;
32+
}
33+
34+
@Override
35+
public int hashCode() {
36+
int result = secure_data != null ? secure_data.hashCode() : 0;
37+
result = 31 * result + (payload != null ? payload.hashCode() : 0);
38+
return result;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "Credentials{" +
44+
"secure_data=" + secure_data +
45+
", payload='" + payload + '\'' +
46+
'}';
47+
}
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.pengrad.telegrambot.passport;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* Stas Parshin
7+
* 31 July 2018
8+
*/
9+
public class DataCredentials implements Serializable {
10+
private final static long serialVersionUID = 0L;
11+
12+
private String data_hash;
13+
private String secret;
14+
15+
public String dataHash() {
16+
return data_hash;
17+
}
18+
19+
public String secret() {
20+
return secret;
21+
}
22+
23+
@Override
24+
public boolean equals(Object o) {
25+
if (this == o) return true;
26+
if (o == null || getClass() != o.getClass()) return false;
27+
28+
DataCredentials that = (DataCredentials) o;
29+
30+
if (data_hash != null ? !data_hash.equals(that.data_hash) : that.data_hash != null) return false;
31+
return secret != null ? secret.equals(that.secret) : that.secret == null;
32+
}
33+
34+
@Override
35+
public int hashCode() {
36+
int result = data_hash != null ? data_hash.hashCode() : 0;
37+
result = 31 * result + (secret != null ? secret.hashCode() : 0);
38+
return result;
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return "DataCredentials{" +
44+
"data_hash='" + data_hash + '\'' +
45+
", secret='" + secret + '\'' +
46+
'}';
47+
}
48+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.pengrad.telegrambot.passport;
2+
3+
/**
4+
* Stas Parshin
5+
* 02 August 2018
6+
* <p>
7+
* Decrypted data from the data field in EncryptedPassportElement.
8+
* Can be one of the following types: PersonalDetails, IdDocumentData, ResidentialAddress
9+
*/
10+
abstract public class DecryptedData {
11+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.pengrad.telegrambot.passport;
2+
3+
import com.pengrad.telegrambot.passport.decrypt.Decrypt;
4+
5+
import java.io.Serializable;
6+
7+
/**
8+
* Stas Parshin
9+
* 30 July 2018
10+
*/
11+
public class EncryptedCredentials implements Serializable {
12+
private final static long serialVersionUID = 0L;
13+
14+
private String data;
15+
private String hash;
16+
private String secret;
17+
18+
public Credentials decrypt(String privateKey) throws Exception {
19+
return Decrypt.decryptCredentials(privateKey, data, hash, secret);
20+
}
21+
22+
public String data() {
23+
return data;
24+
}
25+
26+
public String hash() {
27+
return hash;
28+
}
29+
30+
public String secret() {
31+
return secret;
32+
}
33+
34+
@Override
35+
public boolean equals(Object o) {
36+
if (this == o) return true;
37+
if (o == null || getClass() != o.getClass()) return false;
38+
39+
EncryptedCredentials that = (EncryptedCredentials) o;
40+
41+
if (data != null ? !data.equals(that.data) : that.data != null) return false;
42+
if (hash != null ? !hash.equals(that.hash) : that.hash != null) return false;
43+
return secret != null ? secret.equals(that.secret) : that.secret == null;
44+
}
45+
46+
@Override
47+
public int hashCode() {
48+
int result = data != null ? data.hashCode() : 0;
49+
result = 31 * result + (hash != null ? hash.hashCode() : 0);
50+
result = 31 * result + (secret != null ? secret.hashCode() : 0);
51+
return result;
52+
}
53+
54+
@Override
55+
public String toString() {
56+
return "EncryptedCredentials{" +
57+
"data='" + data + '\'' +
58+
", hash='" + hash + '\'' +
59+
", secret='" + secret + '\'' +
60+
'}';
61+
}
62+
}

0 commit comments

Comments
 (0)