forked from go-pkgz/auth
/
telegram.go
484 lines (407 loc) · 12.9 KB
/
telegram.go
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
package provider
//go:generate moq -out telegram_moq_test.go . TelegramAPI
import (
"context"
"crypto/sha1"
"encoding/json"
"fmt"
"io"
"net/http"
neturl "net/url"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/go-pkgz/repeater"
"github.com/go-pkgz/rest"
"github.com/golang-jwt/jwt"
"github.com/efureev/sauth/logger"
authtoken "github.com/efureev/sauth/token"
)
// TelegramHandler implements login via telegram
type TelegramHandler struct {
logger.L
ProviderName string
ErrorMsg, SuccessMsg string
TokenService TokenService
AvatarSaver AvatarSaver
Telegram TelegramAPI
run int32 // non-zero if Run goroutine has started
username string // bot username
requests struct {
sync.RWMutex
data map[string]tgAuthRequest
}
}
type tgAuthRequest struct {
confirmed bool // whether login request has been confirmed and user info set
expires time.Time
user *authtoken.User
}
// TelegramAPI is used for interacting with telegram API
type TelegramAPI interface {
GetUpdates(ctx context.Context) (*telegramUpdate, error)
Avatar(ctx context.Context, userID int) (string, error)
Send(ctx context.Context, id int, text string) error
BotInfo(ctx context.Context) (*botInfo, error)
}
// changed in tests
var apiPollInterval = time.Second * 5 // interval to check updates from Telegram API and answer to users
var expiredCleanupInterval = time.Minute * 5 // interval to check and clean up expired notification requests
// Run starts processing login requests sent in Telegram
// Blocks caller
func (th *TelegramHandler) Run(ctx context.Context) error {
// Initialization
atomic.AddInt32(&th.run, 1)
info, err := th.Telegram.BotInfo(ctx)
if err != nil {
return fmt.Errorf("failed to fetch bot info: %w", err)
}
th.username = info.Username
th.requests.Lock()
th.requests.data = make(map[string]tgAuthRequest)
th.requests.Unlock()
processUpdatedTicker := time.NewTicker(apiPollInterval)
cleanupTicker := time.NewTicker(expiredCleanupInterval)
for {
select {
case <-ctx.Done():
processUpdatedTicker.Stop()
cleanupTicker.Stop()
atomic.AddInt32(&th.run, -1)
return ctx.Err()
case <-processUpdatedTicker.C:
updates, err := th.Telegram.GetUpdates(ctx)
if err != nil {
th.Logf("Error while getting telegram updates: %v", err)
continue
}
th.processUpdates(ctx, updates)
case <-cleanupTicker.C:
now := time.Now()
th.requests.Lock()
for key, req := range th.requests.data {
if now.After(req.expires) {
delete(th.requests.data, key)
}
}
th.requests.Unlock()
}
}
}
// telegramUpdate contains update information, which is used from whole telegram API response
type telegramUpdate struct {
Result []struct {
UpdateID int `json:"update_id"`
Message struct {
Chat struct {
ID int `json:"id"`
Name string `json:"first_name"`
Type string `json:"type"`
} `json:"chat"`
Text string `json:"text"`
} `json:"message"`
} `json:"result"`
}
// ProcessUpdate is alternative to Run, it processes provided plain text update from Telegram
// so that caller could get updates and send it not only there but to multiple sources
func (th *TelegramHandler) ProcessUpdate(ctx context.Context, textUpdate string) error {
if atomic.LoadInt32(&th.run) != 0 {
return fmt.Errorf("run goroutine should not be used with ProcessUpdate")
}
defer func() {
// as Run goroutine is not running, clean up old requests on each update
// even if we hit json decode error
now := time.Now()
th.requests.Lock()
for key, req := range th.requests.data {
if now.After(req.expires) {
delete(th.requests.data, key)
}
}
th.requests.Unlock()
}()
// initialize requests.data as usually it's initialized in Run
th.requests.Lock()
if th.requests.data == nil {
th.requests.data = make(map[string]tgAuthRequest)
}
th.requests.Unlock()
var updates telegramUpdate
if err := json.Unmarshal([]byte(textUpdate), &updates); err != nil {
return fmt.Errorf("failed to decode provided telegram update: %w", err)
}
th.processUpdates(ctx, &updates)
return nil
}
// processUpdates processes a batch of updates from telegram servers
// Returns offset for subsequent calls
func (th *TelegramHandler) processUpdates(ctx context.Context, updates *telegramUpdate) {
for _, update := range updates.Result {
if update.Message.Chat.Type != "private" {
continue
}
if !strings.HasPrefix(update.Message.Text, "/start ") {
continue
}
token := strings.TrimPrefix(update.Message.Text, "/start ")
th.requests.RLock()
authRequest, ok := th.requests.data[token]
if !ok { // No such token
th.requests.RUnlock()
err := th.Telegram.Send(ctx, update.Message.Chat.ID, th.ErrorMsg)
if err != nil {
th.Logf("failed to notify telegram peer: %v", err)
}
continue
}
th.requests.RUnlock()
avatarURL, err := th.Telegram.Avatar(ctx, update.Message.Chat.ID)
if err != nil {
th.Logf("failed to get user avatar: %v", err)
continue
}
id := th.ProviderName + "_" + authtoken.HashID(sha1.New(), fmt.Sprint(update.Message.Chat.ID))
authRequest.confirmed = true
authRequest.user = &authtoken.User{
ID: id,
Name: update.Message.Chat.Name,
Picture: avatarURL,
}
th.requests.Lock()
th.requests.data[token] = authRequest
th.requests.Unlock()
err = th.Telegram.Send(ctx, update.Message.Chat.ID, th.SuccessMsg)
if err != nil {
th.Logf("failed to notify telegram peer: %v", err)
}
}
}
// addToken adds token
func (th *TelegramHandler) addToken(token string, expires time.Time) error {
th.requests.Lock()
if th.requests.data == nil {
th.requests.Unlock()
return fmt.Errorf("run goroutine is not running")
}
th.requests.data[token] = tgAuthRequest{
expires: expires,
}
th.requests.Unlock()
return nil
}
// checkToken verifies incoming token, returns the user address if it's confirmed and empty string otherwise
func (th *TelegramHandler) checkToken(token string) (*authtoken.User, error) {
th.requests.RLock()
authRequest, ok := th.requests.data[token]
th.requests.RUnlock()
if !ok {
return nil, fmt.Errorf("request is not found")
}
if time.Now().After(authRequest.expires) {
th.requests.Lock()
delete(th.requests.data, token)
th.requests.Unlock()
return nil, fmt.Errorf("request expired")
}
if !authRequest.confirmed {
return nil, fmt.Errorf("request is not verified yet")
}
return authRequest.user, nil
}
// Name of the provider
func (th *TelegramHandler) Name() string { return th.ProviderName }
// String representation of the provider
func (th *TelegramHandler) String() string { return th.Name() }
// Default token lifetime. Changed in tests
var tgAuthRequestLifetime = time.Minute * 10
// LoginHandler generates and verifies login requests
func (th *TelegramHandler) LoginHandler(w http.ResponseWriter, r *http.Request) {
queryToken := r.URL.Query().Get("token")
if queryToken == "" {
// GET /login (No token supplied)
// Generate and send token
token, err := randToken()
if err != nil {
rest.SendErrorJSON(w, r, th.L, http.StatusInternalServerError, err, "failed to generate code")
return
}
err = th.addToken(token, time.Now().Add(tgAuthRequestLifetime))
if err != nil {
rest.SendErrorJSON(w, r, th.L, http.StatusInternalServerError, err, "failed to process login request")
return
}
// verify that we have a username, which is not set if Run was not used
if th.username == "" {
info, err := th.Telegram.BotInfo(r.Context())
if err != nil {
rest.SendErrorJSON(w, r, th.L, http.StatusInternalServerError, err, "failed to fetch bot username")
return
}
th.username = info.Username
}
rest.RenderJSON(w, struct {
Token string `json:"token"`
Bot string `json:"bot"`
}{token, th.username})
return
}
// GET /login?token=blah
authUser, err := th.checkToken(queryToken)
if err != nil {
rest.SendErrorJSON(w, r, nil, http.StatusNotFound, err, err.Error())
return
}
u, err := setAvatar(th.AvatarSaver, *authUser, &http.Client{Timeout: 5 * time.Second})
if err != nil {
rest.SendErrorJSON(w, r, th.L, http.StatusInternalServerError, err, "failed to save avatar to proxy")
return
}
claims := authtoken.Claims{
User: &u,
StandardClaims: jwt.StandardClaims{
Audience: r.URL.Query().Get("site"),
Id: queryToken,
Issuer: th.ProviderName,
ExpiresAt: time.Now().Add(30 * time.Minute).Unix(),
NotBefore: time.Now().Add(-1 * time.Minute).Unix(),
},
SessionOnly: false, // TODO
}
if _, err := th.TokenService.Set(w, claims); err != nil {
rest.SendErrorJSON(w, r, th.L, http.StatusInternalServerError, err, "failed to set token")
return
}
rest.RenderJSON(w, claims.User)
// Delete request
th.requests.Lock()
defer th.requests.Unlock()
delete(th.requests.data, queryToken)
}
// AuthHandler does nothing since we don't have any callbacks
func (th *TelegramHandler) AuthHandler(_ http.ResponseWriter, _ *http.Request) {}
// LogoutHandler - GET /logout
func (th *TelegramHandler) LogoutHandler(w http.ResponseWriter, _ *http.Request) {
th.TokenService.Reset(w)
}
// tgAPI implements TelegramAPI
type tgAPI struct {
logger.L
token string
client *http.Client
// Identifier of the first update to be requested.
// Should be equal to LastSeenUpdateID + 1
// See https://core.telegram.org/bots/api#getupdates
updateOffset int
}
// NewTelegramAPI returns initialized TelegramAPI implementation
func NewTelegramAPI(token string, client *http.Client) TelegramAPI {
return &tgAPI{
client: client,
token: token,
}
}
// GetUpdates fetches incoming updates
func (tg *tgAPI) GetUpdates(ctx context.Context) (*telegramUpdate, error) {
url := `getUpdates?allowed_updates=["message"]`
if tg.updateOffset != 0 {
url += fmt.Sprintf("&offset=%d", tg.updateOffset)
}
var result telegramUpdate
err := tg.request(ctx, url, &result)
if err != nil {
return nil, fmt.Errorf("failed to fetch updates: %w", err)
}
for _, u := range result.Result {
if u.UpdateID >= tg.updateOffset {
tg.updateOffset = u.UpdateID + 1
}
}
return &result, err
}
// Send sends a message to telegram peer
func (tg *tgAPI) Send(ctx context.Context, id int, msg string) error {
url := fmt.Sprintf("sendMessage?chat_id=%d&text=%s", id, neturl.PathEscape(msg))
return tg.request(ctx, url, &struct{}{})
}
// Avatar returns URL to user avatar
func (tg *tgAPI) Avatar(ctx context.Context, id int) (string, error) {
// Get profile pictures
url := fmt.Sprintf(`getUserProfilePhotos?user_id=%d`, id)
var profilePhotos = struct {
Result struct {
Photos [][]struct {
ID string `json:"file_id"`
} `json:"photos"`
} `json:"result"`
}{}
if err := tg.request(ctx, url, &profilePhotos); err != nil {
return "", err
}
// User does not have profile picture set or it is hidden in privacy settings
if len(profilePhotos.Result.Photos) == 0 || len(profilePhotos.Result.Photos[0]) == 0 {
return "", nil
}
// Get max possible picture size
last := len(profilePhotos.Result.Photos[0]) - 1
fileID := profilePhotos.Result.Photos[0][last].ID
url = fmt.Sprintf(`getFile?file_id=%s`, fileID)
var fileMetadata = struct {
Result struct {
Path string `json:"file_path"`
} `json:"result"`
}{}
if err := tg.request(ctx, url, &fileMetadata); err != nil {
return "", err
}
avatarURL := fmt.Sprintf("https://api.telegram.org/file/bot%s/%s", tg.token, fileMetadata.Result.Path)
return avatarURL, nil
}
// botInfo structure contains information about telegram bot, which is used from whole telegram API response
type botInfo struct {
Username string `json:"username"`
}
// BotInfo returns info about configured bot
func (tg *tgAPI) BotInfo(ctx context.Context) (*botInfo, error) {
var resp = struct {
Result *botInfo `json:"result"`
}{}
err := tg.request(ctx, "getMe", &resp)
if err != nil {
return nil, err
}
if resp.Result == nil {
return nil, fmt.Errorf("received empty result")
}
return resp.Result, nil
}
func (tg *tgAPI) request(ctx context.Context, method string, data interface{}) error {
return repeater.NewDefault(3, time.Millisecond*50).Do(ctx, func() error {
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s", tg.token, method)
req, err := http.NewRequestWithContext(ctx, "GET", url, http.NoBody)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}
resp, err := tg.client.Do(req)
if err != nil {
return fmt.Errorf("failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return tg.parseError(resp.Body, resp.StatusCode)
}
if err = json.NewDecoder(resp.Body).Decode(data); err != nil {
return fmt.Errorf("failed to decode json response: %w", err)
}
return nil
})
}
func (tg *tgAPI) parseError(r io.Reader, statusCode int) error {
tgErr := struct {
Description string `json:"description"`
}{}
if err := json.NewDecoder(r).Decode(&tgErr); err != nil {
return fmt.Errorf("unexpected telegram API status code %d", statusCode)
}
return fmt.Errorf("unexpected telegram API status code %d, error: %q", statusCode, tgErr.Description)
}