package auth
import (
"context"
"github.com/go-faster/errors"
"github.com/gotd/td/tg"
"github.com/gotd/td/tgerr"
)
var ErrPasswordInvalid = errors .New ("invalid password" )
func (c *Client ) Password (ctx context .Context , password string ) (*tg .AuthAuthorization , error ) {
p , err := c .api .AccountGetPassword (ctx )
if err != nil {
return nil , errors .Wrap (err , "get SRP parameters" )
}
a , err := PasswordHash ([]byte (password ), p .SRPID , p .SRPB , p .SecureRandom , p .CurrentAlgo )
if err != nil {
return nil , errors .Wrap (err , "compute password hash" )
}
auth , err := c .api .AuthCheckPassword (ctx , &tg .InputCheckPasswordSRP {
SRPID : p .SRPID ,
A : a .A ,
M1 : a .M1 ,
})
if tg .IsPasswordHashInvalid (err ) {
return nil , ErrPasswordInvalid
}
if err != nil {
return nil , errors .Wrap (err , "check password" )
}
result , err := checkResult (auth )
if err != nil {
return nil , errors .Wrap (err , "check" )
}
return result , nil
}
type SendCodeOptions struct {
AllowFlashCall bool
CurrentNumber bool
AllowAppHash bool
}
func (c *Client ) SendCode (ctx context .Context , phone string , options SendCodeOptions ) (tg .AuthSentCodeClass , error ) {
var settings tg .CodeSettings
if options .AllowAppHash {
settings .SetAllowAppHash (true )
}
if options .AllowFlashCall {
settings .SetAllowFlashcall (true )
}
if options .CurrentNumber {
settings .SetCurrentNumber (true )
}
sentCode , err := c .api .AuthSendCode (ctx , &tg .AuthSendCodeRequest {
PhoneNumber : phone ,
APIID : c .appID ,
APIHash : c .appHash ,
Settings : settings ,
})
if err != nil {
return nil , errors .Wrap (err , "send code" )
}
return sentCode , nil
}
var ErrPasswordAuthNeeded = errors .New ("2FA required" )
func (c *Client ) SignIn (ctx context .Context , phone , code , codeHash string ) (*tg .AuthAuthorization , error ) {
auth , err := c .api .AuthSignIn (ctx , &tg .AuthSignInRequest {
PhoneNumber : phone ,
PhoneCodeHash : codeHash ,
PhoneCode : code ,
})
if tgerr .Is (err , "SESSION_PASSWORD_NEEDED" ) {
return nil , ErrPasswordAuthNeeded
}
if err != nil {
return nil , errors .Wrap (err , "sign in" )
}
result , err := checkResult (auth )
if err != nil {
return nil , errors .Wrap (err , "check" )
}
return result , nil
}
func (c *Client ) AcceptTOS (ctx context .Context , id tg .DataJSON ) error {
_ , err := c .api .HelpAcceptTermsOfService (ctx , id )
return err
}
type SignUp struct {
PhoneNumber string
PhoneCodeHash string
FirstName string
LastName string
}
func (c *Client ) SignUp (ctx context .Context , s SignUp ) (*tg .AuthAuthorization , error ) {
auth , err := c .api .AuthSignUp (ctx , &tg .AuthSignUpRequest {
LastName : s .LastName ,
PhoneCodeHash : s .PhoneCodeHash ,
PhoneNumber : s .PhoneNumber ,
FirstName : s .FirstName ,
})
if err != nil {
return nil , errors .Wrap (err , "request" )
}
result , err := checkResult (auth )
if err != nil {
return nil , errors .Wrap (err , "check" )
}
return result , nil
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .