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
|
|
|
|
FirstName chan string
|
|
|
|
LastName chan string
|
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),
|
|
|
|
FirstName: make(chan string, 1),
|
|
|
|
LastName: make(chan string, 1),
|
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{
|
|
|
|
PhoneNumber: <-stateHandler.PhoneNumber,
|
|
|
|
AllowFlashCall: false,
|
|
|
|
IsCurrentPhoneNumber: false,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
|
|
|
|
case TypeAuthorizationStateWaitCode:
|
|
|
|
_, err := client.CheckAuthenticationCode(&CheckAuthenticationCodeRequest{
|
|
|
|
Code: <-stateHandler.Code,
|
|
|
|
FirstName: <-stateHandler.FirstName,
|
|
|
|
LastName: <-stateHandler.LastName,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
|
|
|
|
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.FirstName)
|
|
|
|
close(stateHandler.LastName)
|
|
|
|
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 {
|
|
|
|
case state := <-clientAuthorizer.State:
|
|
|
|
switch state.AuthorizationStateType() {
|
|
|
|
case TypeAuthorizationStateWaitPhoneNumber:
|
|
|
|
fmt.Println("Enter phone number: ")
|
|
|
|
var phoneNumber string
|
|
|
|
fmt.Scanln(&phoneNumber)
|
|
|
|
|
|
|
|
clientAuthorizer.PhoneNumber <- phoneNumber
|
|
|
|
|
|
|
|
case TypeAuthorizationStateWaitCode:
|
|
|
|
var code string
|
|
|
|
var firstName string
|
|
|
|
var lastName string
|
|
|
|
|
|
|
|
fmt.Println("Enter code: ")
|
|
|
|
fmt.Scanln(&code)
|
|
|
|
|
|
|
|
if !state.(*AuthorizationStateWaitCode).IsRegistered {
|
|
|
|
fmt.Println("Phone number is not registered.")
|
|
|
|
|
|
|
|
fmt.Println("Enter first name: ")
|
|
|
|
fmt.Scanln(&firstName)
|
|
|
|
|
|
|
|
fmt.Println("Enter last name: ")
|
|
|
|
fmt.Scanln(&lastName)
|
|
|
|
}
|
|
|
|
|
|
|
|
clientAuthorizer.Code <- code
|
|
|
|
clientAuthorizer.FirstName <- firstName
|
|
|
|
clientAuthorizer.LastName <- lastName
|
|
|
|
|
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)
|
|
|
|
}
|