Skip to content

Commit 5eb1ec4

Browse files
authored
Add Telegram Login auth params checking. Closes #167 (#171)
1 parent 82049f1 commit 5eb1ec4

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.pengrad.telegrambot.login;
2+
3+
import javax.crypto.Mac;
4+
import javax.crypto.spec.SecretKeySpec;
5+
import java.math.BigInteger;
6+
import java.net.URI;
7+
import java.security.InvalidKeyException;
8+
import java.security.MessageDigest;
9+
import java.security.NoSuchAlgorithmException;
10+
import java.util.Date;
11+
import java.util.Iterator;
12+
import java.util.TreeSet;
13+
14+
/**
15+
* Stas Parshin
16+
* 18 June 2019
17+
*/
18+
public class CheckTelegramAuth {
19+
20+
private final String botToken, dataCheck, hash;
21+
private final long authDate;
22+
23+
public static CheckTelegramAuth fromUrl(String botToken, String authUrl) {
24+
return new CheckTelegramAuth(botToken, URI.create(authUrl).getQuery());
25+
}
26+
27+
private CheckTelegramAuth(String botToken, String authQueryParams) {
28+
String hash = null;
29+
long authDate = 0;
30+
String[] params = authQueryParams.split("&");
31+
TreeSet<String> set = new TreeSet<String>();
32+
for (String p : params) {
33+
if (p.startsWith("hash=")) {
34+
hash = p.substring(5);
35+
} else {
36+
set.add(p);
37+
}
38+
if (p.startsWith("auth_date=")) {
39+
authDate = Long.parseLong(p.substring(10));
40+
}
41+
}
42+
this.hash = hash;
43+
this.authDate = authDate;
44+
this.dataCheck = join(set, "\n");
45+
this.botToken = botToken;
46+
}
47+
48+
public Date authDate() {
49+
return new Date(authDate * 1000L);
50+
}
51+
52+
public boolean isFromTelegram() throws Exception {
53+
byte[] secret = sha256(botToken.getBytes());
54+
String result = hmacSha256(secret, dataCheck);
55+
return result.equals(hash);
56+
}
57+
58+
private static byte[] sha256(byte[] string) throws NoSuchAlgorithmException {
59+
MessageDigest md = MessageDigest.getInstance("SHA-256");
60+
return md.digest(string);
61+
}
62+
63+
private static String hmacSha256(byte[] key, String data) throws NoSuchAlgorithmException, InvalidKeyException {
64+
Mac hmacSha256 = Mac.getInstance("HmacSHA256");
65+
SecretKeySpec secret_key = new SecretKeySpec(key, "HmacSHA256");
66+
hmacSha256.init(secret_key);
67+
byte[] result = hmacSha256.doFinal(data.getBytes());
68+
return hex(result);
69+
}
70+
71+
private static String hex(byte[] str) {
72+
return String.format("%040x", new BigInteger(1, str));
73+
}
74+
75+
private static String join(Iterable<String> elements, CharSequence separator) {
76+
StringBuilder builder = new StringBuilder();
77+
Iterator<String> it = elements.iterator();
78+
if (it.hasNext()) {
79+
builder.append(it.next());
80+
while (it.hasNext()) {
81+
builder.append(separator).append(it.next());
82+
}
83+
}
84+
return builder.toString();
85+
}
86+
}

library/src/test/java/com/pengrad/telegrambot/UpdatesListenerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*/
2525
public class UpdatesListenerTest {
2626

27-
private static String token() {
27+
public static String token() {
2828
String token;
2929
try {
3030
Properties properties = new Properties();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.pengrad.telegrambot.login;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Date;
6+
7+
import static com.pengrad.telegrambot.UpdatesListenerTest.token;
8+
import static org.junit.Assert.assertEquals;
9+
import static org.junit.Assert.assertTrue;
10+
11+
/**
12+
* Stas Parshin
13+
* 18 June 2019
14+
*/
15+
public class CheckTelegramAuthTest {
16+
17+
@Test
18+
public void login() throws Exception {
19+
String data = "google.com/?id=51314083&first_name=Stas&last_name=Parshin&username=pengrad&photo_url=https://t.me/i/userpic/320/pengrad.jpg&auth_date=1560837746&hash=b00e1b82fdea0718efc02ee645286fbb0c986526dba9b4bb4d51753960feda04";
20+
CheckTelegramAuth checkTelegramAuth = CheckTelegramAuth.fromUrl(token(), data);
21+
assertTrue(checkTelegramAuth.isFromTelegram());
22+
assertEquals(new Date(1560837746000L), checkTelegramAuth.authDate());
23+
}
24+
25+
}

0 commit comments

Comments
 (0)