go-tdlib/client/authorization.go

223 lines
5.4 KiB
Go
Raw Normal View History

2018-08-30 14:55:42 +00:00
package client
import (
2018-10-23 12:38:10 +00:00
"errors"
"fmt"
"time"
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()
2018-10-23 12:38:10 +00:00
for {
state, err := client.GetAuthorizationState()
if err != nil {
return err
}
err = authorizationStateHandler.Handle(client, state)
if err != nil {
return err
}
if state.AuthorizationStateType() == TypeAuthorizationStateReady {
// dirty hack for db flush after authorization
time.Sleep(1 * time.Second)
return nil
}
}
2018-08-30 14:55:42 +00:00
}
type clientAuthorizer struct {
2018-10-23 12:38:10 +00:00
TdlibParameters chan *TdlibParameters
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{
TdlibParameters: make(chan *TdlibParameters, 1),
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:
_, err := client.SetTdlibParameters(&SetTdlibParametersRequest{
Parameters: <-stateHandler.TdlibParameters,
})
return err
case TypeAuthorizationStateWaitEncryptionKey:
_, err := client.CheckDatabaseEncryptionKey(&CheckDatabaseEncryptionKeyRequest{})
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:
return ErrNotSupportedAuthorizationState
case TypeAuthorizationStateClosed:
return ErrNotSupportedAuthorizationState
}
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: ")
var password string
fmt.Scanln(&password)
clientAuthorizer.Password <- password
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateReady:
return
}
}
}
2018-08-30 14:55:42 +00:00
}
type botAuthorizer struct {
2018-10-23 12:38:10 +00:00
TdlibParameters chan *TdlibParameters
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{
TdlibParameters: make(chan *TdlibParameters, 1),
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:
_, err := client.SetTdlibParameters(&SetTdlibParametersRequest{
Parameters: <-stateHandler.TdlibParameters,
})
return err
2018-08-30 14:55:42 +00:00
2018-10-23 12:38:10 +00:00
case TypeAuthorizationStateWaitEncryptionKey:
_, err := client.CheckDatabaseEncryptionKey(&CheckDatabaseEncryptionKeyRequest{})
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)
}