Skip to content

Commit

Permalink
optimize web auth api example for concurrent auths
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Apr 30, 2024
1 parent 35f1f2d commit 384a337
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 52 deletions.
14 changes: 4 additions & 10 deletions examples/auth-using-api-base/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,21 @@ import (
)

func main() {

wa := web.GetWebAuth()
// start web api
go web.Start()

clientType := gotgproto.ClientType{
// put your phone here or just leave it empty
// if you leave it empty, you will be asked to enter your phone number
Phone: "",
}
go web.Start(wa)
client, err := gotgproto.NewClient(
// Get AppID from https://my.telegram.org/apps
123456,
// Get ApiHash from https://my.telegram.org/apps
"API_HASH_HERE",
// ClientType, as we defined above
clientType,
gotgproto.ClientType{},
// Optional parameters of client
&gotgproto.ClientOpts{

// custom authenticator using web api
AuthConversator: web.GetWebAuth(),
AuthConversator: wa,
Session: sessionMaker.SqlSession(sqlite.Open("webbot")),
},
)
Expand Down
32 changes: 16 additions & 16 deletions examples/auth-using-api-base/web/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ import (
)

// Start a web server and wait
func Start() {
http.HandleFunc("/", setInfo)
http.HandleFunc("/getAuthStatus", getAuthStatus)
func Start(wa *webAuth) {
http.HandleFunc("/", wa.setInfo)
http.HandleFunc("/getAuthStatus", wa.getAuthStatus)
http.ListenAndServe(":9997", nil)
}

func getAuthStatus(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, authStatus.Event)
func (wa *webAuth) getAuthStatus(w http.ResponseWriter, req *http.Request) {
fmt.Fprint(w, wa.authStatus.Event)
}

// setInfo handle user info, set phone, code or passwd
func setInfo(w http.ResponseWriter, req *http.Request) {
func (wa *webAuth) setInfo(w http.ResponseWriter, req *http.Request) {
action := req.URL.Query().Get("set")

switch action {
Expand All @@ -28,26 +28,26 @@ func setInfo(w http.ResponseWriter, req *http.Request) {
fmt.Println("Rec phone")
num := req.URL.Query().Get("phone")
phone := "+" + num
ReceivePhone(phone)
for authStatus.Event == gotgproto.AuthStatusPhoneAsked ||
authStatus.Event == gotgproto.AuthStatusPhoneRetrial {
wa.ReceivePhone(phone)
for wa.authStatus.Event == gotgproto.AuthStatusPhoneAsked ||
wa.authStatus.Event == gotgproto.AuthStatusPhoneRetrial {
continue
}
case "code":
fmt.Println("Rec code")
code := req.URL.Query().Get("code")
ReceiveCode(code)
for authStatus.Event == gotgproto.AuthStatusPhoneCodeAsked ||
authStatus.Event == gotgproto.AuthStatusPhoneCodeRetrial {
wa.ReceiveCode(code)
for wa.authStatus.Event == gotgproto.AuthStatusPhoneCodeAsked ||
wa.authStatus.Event == gotgproto.AuthStatusPhoneCodeRetrial {
continue
}
case "passwd":
passwd := req.URL.Query().Get("passwd")
ReceivePasswd(passwd)
for authStatus.Event == gotgproto.AuthStatusPasswordAsked ||
authStatus.Event == gotgproto.AuthStatusPasswordRetrial {
wa.ReceivePasswd(passwd)
for wa.authStatus.Event == gotgproto.AuthStatusPasswordAsked ||
wa.authStatus.Event == gotgproto.AuthStatusPasswordRetrial {
continue
}
}
w.Write([]byte(authStatus.Event))
w.Write([]byte(wa.authStatus.Event))
}
49 changes: 23 additions & 26 deletions examples/auth-using-api-base/web/webauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,65 +6,62 @@ import (
"github.com/celestix/gotgproto"
)

var authStatus gotgproto.AuthStatus

type webAuth struct{}

var (
phoneChan = make(chan string)
codeChan = make(chan string)
passwdChan = make(chan string)
)
type webAuth struct {
phoneChan chan string
codeChan chan string
passwdChan chan string
authStatus gotgproto.AuthStatus
}

func GetWebAuth() gotgproto.AuthConversator {
func GetWebAuth() *webAuth {
return &webAuth{}
}

func (w *webAuth) AskPhoneNumber() (string, error) {
if authStatus.Event == gotgproto.AuthStatusPhoneRetrial {
if w.authStatus.Event == gotgproto.AuthStatusPhoneRetrial {
fmt.Println("The phone number you just entered seems to be incorrect,")
fmt.Println("Attempts Left:", authStatus.AttemptsLeft)
fmt.Println("Attempts Left:", w.authStatus.AttemptsLeft)
fmt.Println("Please try again....")
}
fmt.Println("waiting for phone...")
code := <-phoneChan
code := <-w.phoneChan
return code, nil
}

func (w *webAuth) AskCode() (string, error) {
if authStatus.Event == gotgproto.AuthStatusPhoneCodeRetrial {
if w.authStatus.Event == gotgproto.AuthStatusPhoneCodeRetrial {
fmt.Println("The OTP you just entered seems to be incorrect,")
fmt.Println("Attempts Left:", authStatus.AttemptsLeft)
fmt.Println("Attempts Left:", w.authStatus.AttemptsLeft)
fmt.Println("Please try again....")
}
fmt.Println("waiting for code...")
code := <-codeChan
code := <-w.codeChan
return code, nil
}

func (w *webAuth) AskPassword() (string, error) {
if authStatus.Event == gotgproto.AuthStatusPasswordRetrial {
if w.authStatus.Event == gotgproto.AuthStatusPasswordRetrial {
fmt.Println("The 2FA password you just entered seems to be incorrect,")
fmt.Println("Attempts Left:", authStatus.AttemptsLeft)
fmt.Println("Attempts Left:", w.authStatus.AttemptsLeft)
fmt.Println("Please try again....")
}
fmt.Println("waiting for 2fa password...")
code := <-passwdChan
code := <-w.passwdChan
return code, nil
}

func (w *webAuth) AuthStatus(authStatusIp gotgproto.AuthStatus) {
authStatus = authStatusIp
w.authStatus = authStatusIp
}

func ReceivePhone(phone string) {
phoneChan <- phone
func (w *webAuth) ReceivePhone(phone string) {
w.phoneChan <- phone
}

func ReceiveCode(code string) {
codeChan <- code
func (w *webAuth) ReceiveCode(code string) {
w.codeChan <- code
}

func ReceivePasswd(passwd string) {
passwdChan <- passwd
func (w *webAuth) ReceivePasswd(passwd string) {
w.passwdChan <- passwd
}

0 comments on commit 384a337

Please sign in to comment.