Skip to content

Commit

Permalink
chore: support for Telegram 2FA
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanjacobson committed Aug 13, 2024
1 parent b28bd92 commit 02e5384
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
3 changes: 2 additions & 1 deletion pkg/api/handlers_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -809,13 +809,14 @@ func (api *API) CompleteAuth() gin.HandlerFunc {
PhoneNumber string `json:"phone_number"`
Code string `json:"code"`
PhoneCodeHash string `json:"phone_code_hash"`
Password string `json:"password"`
}
if err := c.ShouldBindJSON(&reqBody); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid request body"})
return
}

auth, err := telegram.CompleteAuthentication(context.Background(), reqBody.PhoneNumber, reqBody.Code, reqBody.PhoneCodeHash)
auth, err := telegram.CompleteAuthentication(context.Background(), reqBody.PhoneNumber, reqBody.Code, reqBody.PhoneCodeHash, reqBody.Password)
if err != nil {
// Check if 2FA is required
if err.Error() == "2FA required" {
Expand Down
17 changes: 6 additions & 11 deletions pkg/scrapers/telegram/telegram_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"path/filepath"
"strconv"
"strings"
"sync"

"github.com/gotd/contrib/bg"
Expand Down Expand Up @@ -128,18 +129,12 @@ func CompleteAuthentication(ctx context.Context, phoneNumber, code, phoneCodeHas
err = client.Run(ctx, func(ctx context.Context) error {
// Use the provided code and phoneCodeHash to authenticate
auth, err := client.Auth().SignIn(ctx, phoneNumber, code, phoneCodeHash)

if err != nil {
var e *tg.Error
if errors.As(err, &e) { // Check if err is *tg.Error
if e.Text == "" { // This is just an example, replace with actual type for password needed
// Now, you need to sign in with the password (2FA)
auth, err = client.Auth().Password(ctx, password)
if err != nil {
log.Printf("Error during 2FA SignIn: %v", err)
return err
}
} else {
log.Printf("Error during SignIn: %v", err)
if strings.Contains(err.Error(), "ErrPasswordAuthNeeded") {
auth, err = client.Auth().Password(ctx, password)
if err != nil {
log.Printf("Error during 2FA SignIn: %v", err)
return err
}
} else {
Expand Down

0 comments on commit 02e5384

Please sign in to comment.