|
| 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 | +} |
0 commit comments