go-tdlib/client/authorization.go

224 lines
5.4 KiB
Go
Raw Permalink Normal View History

2018-08-30 14:55:42 +00:00
package client
import (
2018-10-23 12:38:10 +00:00
"errors"
"fmt"
"syscall"
2018-10-23 12:38:10 +00:00
"time"
"golang.org/x/crypto/ssh/terminal"
2018-08-30 14:55:42 +00:00
)
var ErrNotSupportedAuthorizationState = errors.New("not supported state")
type AuthorizationStateHandler interface {
2018-10-23 12:38:10 +00:00
Handle(client *Client, state AuthorizationState) error
2018-10-24 14:54:34 +00:00
Close()
2018-08-30 14:55:42 +00:00
}
func Authorize(client *Client, authorizationStateHandler AuthorizationStateHandler) error {
2018-10-24 14:54:34 +00:00
defer authorizationStateHandler.Close()
2019-12-30 15:52:13 +00:00
var authorizationError error
2018-10-23 12:38:10 +00:00
for {
state, err := client.GetAuthorizationState()
if err != nil {
return err
}
2019-12-30 15:52:13 +00:00
if state.AuthorizationStateType() == TypeAuthorizationStateClosed {
return authorizationError
2018-10-23 12:38:10 +00:00
}
if state.AuthorizationStateType() == TypeAuthorizationStateReady {
// dirty hack for db flush after authorization
time.Sleep(1 * time.Second)
return nil
}
2019-12-30 15:52:13 +00:00
err = authorizationStateHandler.Handle(client, state)
if err != nil {
authorizationError = err
client.Close()
}
2018-10-23 12:38:10 +00:00
}
2018-08-30 14:55:42 +00:00
}
type clientAuthorizer struct {
2022-09-18 01:49:37 +00:00
TdlibParameters chan *SetTdlibParametersRequest
2018-10-23 12:38:10 +00:00
PhoneNumber chan string
Code chan string
State chan AuthorizationState
2018-10-23 16:45:57 +00:00
Password chan string
2018-08-30 14:55:42 +00:00
}
func ClientAuthorizer() *clientAuthorizer {
2018-10-23 12:38:10 +00:00
return &clientAuthorizer{
2022-09-18 01:49:37 +00:00
TdlibParameters: make(chan *SetTdlibParametersRequest, 1),
2018-10-23 12:38:10 +00:00
PhoneNumber: make(chan string, 1),
Code: make(chan string, 1),
State: make(chan AuthorizationState, 10),
2018-10-23 16:45:57 +00:00
Password: make(chan string, 1),
2018-10-23 12:38:10 +00:00
}
2018-08-30 14:55:42 +00:00
}
func (stateHandler *clientAuthorizer) Handle(client *Client, state AuthorizationState) error {
2018-10-23 12:38:10 +00:00
stateHandler.State <- state
switch state.AuthorizationStateType() {
case TypeAuthorizationStateWaitTdlibParameters:
2022-09-18 01:49:37 +00:00
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters)
2018-10-23 12:38:10 +00:00
return err
case TypeAuthorizationStateWaitPhoneNumber:
_, err := client.SetAuthenticationPhoneNumber(&SetAuthenticationPhoneNumberRequest{
2019-09-10 12:36:54 +00:00
PhoneNumber: <-stateHandler.PhoneNumber,
Settings: &PhoneNumberAuthenticationSettings{
AllowFlashCall: false,
IsCurrentPhoneNumber: false,
AllowSmsRetrieverApi: false,
},
2018-10-23 12:38:10 +00:00
})
return err
case TypeAuthorizationStateWaitCode:
_, err := client.CheckAuthenticationCode(&CheckAuthenticationCodeRequest{
2019-09-10 12:36:54 +00:00
Code: <-stateHandler.Code,
2018-10-23 12:38:10 +00:00
})
return err
2019-09-10 12:36:54 +00:00
case TypeAuthorizationStateWaitRegistration:
return ErrNotSupportedAuthorizationState
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateWaitPassword:
2018-10-23 16:45:57 +00:00
_, err := client.CheckAuthenticationPassword(&CheckAuthenticationPasswordRequest{
Password: <-stateHandler.Password,
})
return err
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateReady:
return nil
case TypeAuthorizationStateLoggingOut:
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateClosing:
2019-12-30 15:52:13 +00:00
return nil
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateClosed:
2019-12-30 15:52:13 +00:00
return nil
2018-10-23 12:38:10 +00:00
}
return ErrNotSupportedAuthorizationState
2018-08-30 14:55:42 +00:00
}
2018-10-24 14:54:34 +00:00
func (stateHandler *clientAuthorizer) Close() {
close(stateHandler.TdlibParameters)
close(stateHandler.PhoneNumber)
close(stateHandler.Code)
close(stateHandler.State)
close(stateHandler.Password)
}
2018-10-10 13:35:28 +00:00
func CliInteractor(clientAuthorizer *clientAuthorizer) {
2018-10-23 12:38:10 +00:00
for {
select {
2018-10-25 12:20:19 +00:00
case state, ok := <-clientAuthorizer.State:
if !ok {
return
}
2018-10-23 12:38:10 +00:00
switch state.AuthorizationStateType() {
case TypeAuthorizationStateWaitPhoneNumber:
fmt.Println("Enter phone number: ")
var phoneNumber string
fmt.Scanln(&phoneNumber)
clientAuthorizer.PhoneNumber <- phoneNumber
case TypeAuthorizationStateWaitCode:
var code string
fmt.Println("Enter code: ")
fmt.Scanln(&code)
clientAuthorizer.Code <- code
2018-10-23 16:45:57 +00:00
case TypeAuthorizationStateWaitPassword:
fmt.Println("Enter password: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
fmt.Println(err)
continue
}
2018-10-23 16:45:57 +00:00
clientAuthorizer.Password <- string(bytePassword)
2018-10-23 16:45:57 +00:00
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateReady:
return
}
}
}
2018-08-30 14:55:42 +00:00
}
type botAuthorizer struct {
2022-09-18 01:49:37 +00:00
TdlibParameters chan *SetTdlibParametersRequest
2018-10-23 12:38:10 +00:00
Token chan string
State chan AuthorizationState
2018-08-30 14:55:42 +00:00
}
func BotAuthorizer(token string) *botAuthorizer {
2018-10-23 12:38:10 +00:00
botAuthorizer := &botAuthorizer{
2022-09-18 01:49:37 +00:00
TdlibParameters: make(chan *SetTdlibParametersRequest, 1),
2018-10-23 12:38:10 +00:00
Token: make(chan string, 1),
State: make(chan AuthorizationState, 10),
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
botAuthorizer.Token <- token
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
return botAuthorizer
2018-08-30 14:55:42 +00:00
}
func (stateHandler *botAuthorizer) Handle(client *Client, state AuthorizationState) error {
2018-10-23 12:38:10 +00:00
stateHandler.State <- state
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
switch state.AuthorizationStateType() {
case TypeAuthorizationStateWaitTdlibParameters:
2022-09-18 01:49:37 +00:00
_, err := client.SetTdlibParameters(<-stateHandler.TdlibParameters)
2018-10-23 12:38:10 +00:00
return err
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateWaitPhoneNumber:
_, err := client.CheckAuthenticationBotToken(&CheckAuthenticationBotTokenRequest{
Token: <-stateHandler.Token,
})
return err
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateWaitCode:
return ErrNotSupportedAuthorizationState
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateWaitPassword:
return ErrNotSupportedAuthorizationState
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateReady:
return nil
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateLoggingOut:
return ErrNotSupportedAuthorizationState
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateClosing:
return ErrNotSupportedAuthorizationState
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateClosed:
return ErrNotSupportedAuthorizationState
}
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
return ErrNotSupportedAuthorizationState
2018-08-30 14:55:42 +00:00
}
2018-10-24 14:54:34 +00:00
func (stateHandler *botAuthorizer) Close() {
close(stateHandler.TdlibParameters)
close(stateHandler.Token)
close(stateHandler.State)
}