Skip to content

Commit

Permalink
add a flag to disable automatic auth flow
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Apr 25, 2024
1 parent 109139d commit d12907c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
15 changes: 0 additions & 15 deletions authHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,6 @@ import (
"github.com/pkg/errors"
)

func ifAuthNecessary(ctx context.Context, c *auth.Client, conversator AuthConversator, phone string, sendOpts auth.SendCodeOptions) error {
auth, err := c.Status(ctx)
if err != nil {
return errors.Wrap(err, "get auth status")
}
if auth.Authorized {
return nil
}
if err := authFlow(ctx, c, conversator, phone, sendOpts); err != nil {
return errors.Wrap(err, "auth flow")
}
return nil

}

type Flow auth.Flow

func (f Flow) handleSignUp(ctx context.Context, client auth.FlowClient, phone, hash string, s *auth.SignUpRequired) error {
Expand Down
27 changes: 25 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ type Client struct {
// PeerStorage is the storage for all the peers.
// It is recommended to use storage.NewPeerStorage function for this field.
PeerStorage *storage.PeerStorage
// NoAutoAuth is a flag to disable automatic authentication
// if the current session is invalid.
NoAutoAuth bool

authConversator AuthConversator
clientType ClientType
Expand Down Expand Up @@ -176,6 +179,9 @@ type ClientOpts struct {
// If < 0, compression will be disabled.
// If == 0, default value will be used.
CompressThreshold int
// NoAutoAuth is a flag to disable automatic authentication
// if the current session is invalid.
NoAutoAuth bool
}

// NewClient creates a new gotgproto client and logs in to telegram.
Expand Down Expand Up @@ -221,6 +227,7 @@ func NewClient(appId int, apiHash string, cType ClientType, opts *ClientOpts) (*
Logger: opts.Logger,
SystemLangCode: opts.SystemLangCode,
ClientLangCode: opts.ClientLangCode,
NoAutoAuth: opts.NoAutoAuth,
authConversator: opts.AuthConversator,
Dispatcher: d,
PeerStorage: peerStorage,
Expand Down Expand Up @@ -276,8 +283,24 @@ func (c *Client) login() error {
authClient := c.Auth()

if c.clientType.BotToken == "" {
if err := ifAuthNecessary(c.ctx, authClient, c.authConversator, c.clientType.Phone, auth.SendCodeOptions{}); err != nil {
return err
status, err := authClient.Status(c.ctx)
if err != nil {
return errors.Wrap(err, "get auth status")
}
if status.Authorized {
return nil
}
if c.NoAutoAuth {
return intErrors.ErrSessionUnauthorized
}
err = authFlow(
c.ctx, authClient,
c.authConversator,
c.clientType.Phone,
auth.SendCodeOptions{},
)
if err != nil {
return errors.Wrap(err, "auth flow")
}
} else {
status, err := authClient.Status(c.ctx)
Expand Down
1 change: 1 addition & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "errors"

var (
ErrClientAlreadyRunning = errors.New("client is already running")
ErrSessionUnauthorized = errors.New("session is unauthorized")
)

var (
Expand Down

0 comments on commit d12907c

Please sign in to comment.