forked from Telegram-Mini-Apps/init-data-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_third_party.go
More file actions
122 lines (109 loc) · 3.23 KB
/
Copy pathvalidate_third_party.go
File metadata and controls
122 lines (109 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package initdata
import (
"crypto/ed25519"
"encoding/base64"
"encoding/hex"
"fmt"
"net/url"
"sort"
"strconv"
"strings"
"time"
)
var (
_telegramProdPublicKey, _ = hex.DecodeString("e7bf03a2fa4602af4580703d88dda5bb59f32ed8b02a56c187fe7d34caed242d")
_telegramTestPublicKey, _ = hex.DecodeString("40055058a4ee38156a06562e52eece92a771bcd8346a8c4615cb7376eddf72ec")
)
// ValidateThirdPartyWithEnv validates passed init data assuming that it was signed by Telegram.
// This method expects initData to be passed in the exact raw format as it could be found
// in window.Telegram.WebApp.initData.
//
// Returns error if something is wrong with the passed init data. Nil otherwise.
//
// initData - init data passed from application;
// botID - init data Telegram Bot issuer identifier;
// expIn - maximum init data lifetime. It is strongly recommended to use this
// parameter. In case, exp duration is less than or equal to 0, function does
// not check if parameters are expired.
// isTest - true if the init data was issued in Telegram production environment;
func ValidateThirdPartyWithEnv(
initData string,
botID int64,
expIn time.Duration,
isTest bool,
) error {
// Parse passed init data as query string.
q, err := url.ParseQuery(initData)
if err != nil {
return ErrUnexpectedFormat
}
var (
// Init data creation time.
authDate time.Time
// Init data signature.
signature []byte
// All found key-value pairs.
pairs = make([]string, 0, len(q))
)
// Iterate over all key-value pairs of parsed parameters.
for k, v := range q {
// hash is ignored during this validation.
if k == "hash" {
continue
}
// Store found signature.
if k == "signature" {
signature, _ = base64.URLEncoding.DecodeString(
// Signature is base64-encoded. We append padding by ourselves as long Telegram's server
// incorrectly creates a base64 string, but GoLang intolerant to this and requires strict
// format compliance.
v[0] + strings.Repeat("=", 4-len(v[0])%4),
)
continue
}
if k == "auth_date" {
if i, err := strconv.Atoi(v[0]); err == nil {
authDate = time.Unix(int64(i), 0)
}
}
// Append new pair.
pairs = append(pairs, k+"="+v[0])
}
// Signature is always required.
if len(signature) == 0 {
return ErrSignMissing
}
// In case, expiration time is passed, we do additional parameters check.
if expIn > 0 {
// In case, auth date is zero, it means, we can not check if parameters
// are expired.
if authDate.IsZero() {
return ErrAuthDateMissing
}
// Check if init data is expired.
if authDate.Add(expIn).Before(time.Now()) {
return ErrExpired
}
}
// According to docs, we sort all the pairs in alphabetical order.
sort.Strings(pairs)
var publicKey []byte
if isTest {
publicKey = _telegramTestPublicKey
} else {
publicKey = _telegramProdPublicKey
}
if !ed25519.Verify(
publicKey,
[]byte(fmt.Sprintf("%d:WebAppData\n%s", botID, strings.Join(pairs, "\n"))),
signature,
) {
return ErrSignInvalid
}
return nil
}
// ValidateThirdParty performs validation described in the Validate3rdWithEnv function using
// production environment.
func ValidateThirdParty(initData string, botID int64, expIn time.Duration) error {
return ValidateThirdPartyWithEnv(initData, botID, expIn, false)
}