Implement an own authorizer
This commit is contained in:
parent
d6f6207ebb
commit
a89629ab20
2
go.mod
2
go.mod
|
@ -14,5 +14,3 @@ require (
|
||||||
)
|
)
|
||||||
|
|
||||||
replace gosrc.io/xmpp => github.com/bodqhrohro/go-xmpp v0.1.4-0.20191106203535-f3b463f3b26c
|
replace gosrc.io/xmpp => github.com/bodqhrohro/go-xmpp v0.1.4-0.20191106203535-f3b463f3b26c
|
||||||
|
|
||||||
replace github.com/zelenin/go-tdlib => github.com/bodqhrohro/go-tdlib v0.1.2-0.20191121233100-48d2382034fb
|
|
||||||
|
|
|
@ -35,7 +35,7 @@ func stringToLogConstant(c string) int32 {
|
||||||
// Client stores the metadata for lazily invoked TDlib instance
|
// Client stores the metadata for lazily invoked TDlib instance
|
||||||
type Client struct {
|
type Client struct {
|
||||||
client *client.Client
|
client *client.Client
|
||||||
authorizer *client.ClientAuthorizerType
|
authorizer *clientAuthorizer
|
||||||
xmpp *xmpp.Component
|
xmpp *xmpp.Component
|
||||||
jid string
|
jid string
|
||||||
parameters *client.TdlibParameters
|
parameters *client.TdlibParameters
|
||||||
|
@ -93,7 +93,7 @@ func updateHandler(tdlibClient *client.Client) {
|
||||||
|
|
||||||
for update := range listener.Updates {
|
for update := range listener.Updates {
|
||||||
if update.GetClass() == client.ClassUpdate {
|
if update.GetClass() == client.ClassUpdate {
|
||||||
fmt.Printf("%#v", update)
|
fmt.Printf("%#v\n", update)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,78 @@ import (
|
||||||
"github.com/zelenin/go-tdlib/client"
|
"github.com/zelenin/go-tdlib/client"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type clientAuthorizer struct {
|
||||||
|
TdlibParameters chan *client.TdlibParameters
|
||||||
|
PhoneNumber chan string
|
||||||
|
Code chan string
|
||||||
|
State chan client.AuthorizationState
|
||||||
|
Password chan string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stateHandler *clientAuthorizer) Handle(c *client.Client, state client.AuthorizationState) error {
|
||||||
|
stateHandler.State <- state
|
||||||
|
|
||||||
|
switch state.AuthorizationStateType() {
|
||||||
|
case client.TypeAuthorizationStateWaitTdlibParameters:
|
||||||
|
_, err := c.SetTdlibParameters(&client.SetTdlibParametersRequest{
|
||||||
|
Parameters: <-stateHandler.TdlibParameters,
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
|
||||||
|
case client.TypeAuthorizationStateWaitEncryptionKey:
|
||||||
|
_, err := c.CheckDatabaseEncryptionKey(&client.CheckDatabaseEncryptionKeyRequest{})
|
||||||
|
return err
|
||||||
|
|
||||||
|
case client.TypeAuthorizationStateWaitPhoneNumber:
|
||||||
|
_, err := c.SetAuthenticationPhoneNumber(&client.SetAuthenticationPhoneNumberRequest{
|
||||||
|
PhoneNumber: <-stateHandler.PhoneNumber,
|
||||||
|
Settings: &client.PhoneNumberAuthenticationSettings{
|
||||||
|
AllowFlashCall: false,
|
||||||
|
IsCurrentPhoneNumber: false,
|
||||||
|
AllowSmsRetrieverApi: false,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
|
||||||
|
case client.TypeAuthorizationStateWaitCode:
|
||||||
|
_, err := c.CheckAuthenticationCode(&client.CheckAuthenticationCodeRequest{
|
||||||
|
Code: <-stateHandler.Code,
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
|
||||||
|
case client.TypeAuthorizationStateWaitRegistration:
|
||||||
|
return client.ErrNotSupportedAuthorizationState
|
||||||
|
|
||||||
|
case client.TypeAuthorizationStateWaitPassword:
|
||||||
|
_, err := c.CheckAuthenticationPassword(&client.CheckAuthenticationPasswordRequest{
|
||||||
|
Password: <-stateHandler.Password,
|
||||||
|
})
|
||||||
|
return err
|
||||||
|
|
||||||
|
case client.TypeAuthorizationStateReady:
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case client.TypeAuthorizationStateLoggingOut:
|
||||||
|
return client.ErrNotSupportedAuthorizationState
|
||||||
|
|
||||||
|
case client.TypeAuthorizationStateClosing:
|
||||||
|
return client.ErrNotSupportedAuthorizationState
|
||||||
|
|
||||||
|
case client.TypeAuthorizationStateClosed:
|
||||||
|
return client.ErrNotSupportedAuthorizationState
|
||||||
|
}
|
||||||
|
|
||||||
|
return client.ErrNotSupportedAuthorizationState
|
||||||
|
}
|
||||||
|
|
||||||
|
func (stateHandler *clientAuthorizer) Close() {
|
||||||
|
close(stateHandler.TdlibParameters)
|
||||||
|
close(stateHandler.PhoneNumber)
|
||||||
|
close(stateHandler.Code)
|
||||||
|
close(stateHandler.State)
|
||||||
|
close(stateHandler.Password)
|
||||||
|
}
|
||||||
|
|
||||||
// Connect starts TDlib connection
|
// Connect starts TDlib connection
|
||||||
func (c *Client) Connect() error {
|
func (c *Client) Connect() error {
|
||||||
if c.online {
|
if c.online {
|
||||||
|
@ -17,7 +89,13 @@ func (c *Client) Connect() error {
|
||||||
|
|
||||||
log.Warn("Connecting to Telegram network...")
|
log.Warn("Connecting to Telegram network...")
|
||||||
|
|
||||||
c.authorizer = client.ClientAuthorizer()
|
c.authorizer = &clientAuthorizer{
|
||||||
|
TdlibParameters: make(chan *client.TdlibParameters, 1),
|
||||||
|
PhoneNumber: make(chan string, 1),
|
||||||
|
Code: make(chan string, 1),
|
||||||
|
State: make(chan client.AuthorizationState, 10),
|
||||||
|
Password: make(chan string, 1),
|
||||||
|
}
|
||||||
|
|
||||||
go c.interactor()
|
go c.interactor()
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue