Skip to content

Commit

Permalink
feat(login): select login mode with type flag
Browse files Browse the repository at this point in the history
  • Loading branch information
iyear committed Dec 18, 2023
1 parent ff0d446 commit 8127fb7
Show file tree
Hide file tree
Showing 6 changed files with 210 additions and 80 deletions.
69 changes: 0 additions & 69 deletions app/login/auth.go

This file was deleted.

62 changes: 61 additions & 1 deletion app/login/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package login
import (
"context"
"crypto/rand"
"strings"
"time"

"github.com/AlecAivazis/survey/v2"
"github.com/cenkalti/backoff/v4"
"github.com/fatih/color"
"github.com/go-faster/errors"
"github.com/gotd/td/telegram"
"github.com/gotd/td/telegram/auth"
"github.com/gotd/td/tg"
"github.com/spf13/viper"

"github.com/iyear/tdl/pkg/consts"
Expand Down Expand Up @@ -71,8 +74,65 @@ func Code(ctx context.Context) error {
return err
}

color.Blue("Login successfully! ID: %d, Username: %s", user.ID, user.Username)
color.Green("Login successfully! ID: %d, Username: %s", user.ID, user.Username)

return nil
})
}

// noSignUp can be embedded to prevent signing up.
type noSignUp struct{}

func (c noSignUp) SignUp(_ context.Context) (auth.UserInfo, error) {
return auth.UserInfo{}, errors.New("don't support sign up Telegram account")
}

func (c noSignUp) AcceptTermsOfService(_ context.Context, tos tg.HelpTermsOfService) error {
return &auth.SignUpRequired{TermsOfService: tos}
}

// termAuth implements authentication via terminal.
type termAuth struct {
noSignUp
}

func (a termAuth) Phone(_ context.Context) (string, error) {
phone := ""
prompt := &survey.Input{
Message: "Enter your phone number:",
Default: "+86 12345678900",
}

if err := survey.AskOne(prompt, &phone, survey.WithValidator(survey.Required)); err != nil {
return "", err
}

color.Blue("Sending Code...")
return strings.TrimSpace(phone), nil
}

func (a termAuth) Password(_ context.Context) (string, error) {
pwd := ""
prompt := &survey.Password{
Message: "Enter 2FA Password:",
}

if err := survey.AskOne(prompt, &pwd, survey.WithValidator(survey.Required)); err != nil {
return "", err
}

return strings.TrimSpace(pwd), nil
}

func (a termAuth) Code(_ context.Context, _ *tg.AuthSentCode) (string, error) {
code := ""
prompt := &survey.Input{
Message: "Enter Code:",
}

if err := survey.AskOne(prompt, &code, survey.WithValidator(survey.Required)); err != nil {
return "", err
}

return strings.TrimSpace(code), nil
}
7 changes: 1 addition & 6 deletions app/login/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,7 @@ import (

const tdata = "tdata"

type Options struct {
Desktop string
Passcode string
}

func Desktop(ctx context.Context, opts *Options) error {
func Desktop(ctx context.Context, opts Options) error {
ns := viper.GetString(consts.FlagNamespace)

kvd, err := kv.From(ctx).Open(ns)
Expand Down
32 changes: 32 additions & 0 deletions app/login/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package login

import (
"context"

"github.com/go-faster/errors"
)

//go:generate go-enum --values --names --flag --nocase

// Type
// ENUM(desktop, code, qr)
type Type int

type Options struct {
Type Type
Desktop string
Passcode string
}

func Run(ctx context.Context, opts Options) error {
switch opts.Type {
case TypeDesktop:
return Desktop(ctx, opts)
case TypeCode:
return Code(ctx)
case TypeQr:
return QR(ctx)
default:
return errors.Errorf("unsupported login type: %s", opts.Type)
}
}
106 changes: 106 additions & 0 deletions app/login/login_enum.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions cmd/login.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmd

import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/spf13/cobra"

Expand All @@ -20,24 +23,27 @@ func NewLogin() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
color.Yellow("WARN: If data exists in the namespace, data will be overwritten")

// Legacy flag
if code {
return login.Code(logger.Named(cmd.Context(), "login"))
}

return login.Desktop(cmd.Context(), &opts)
return login.Run(logger.Named(cmd.Context(), "login"), opts)
},
}

const (
desktop = "desktop"
)
const desktop = "desktop"

cmd.Flags().VarP(&opts.Type, "type", "T", fmt.Sprintf("login mode: [%s]", strings.Join(login.TypeNames(), ", ")))
cmd.Flags().StringVarP(&opts.Desktop, desktop, "d", "", "official desktop client path, and automatically find possible paths if empty")
cmd.Flags().StringVarP(&opts.Passcode, "passcode", "p", "", "passcode for desktop client, keep empty if no passcode")

// Deprecated
cmd.Flags().BoolVar(&code, "code", false, "login with code, instead of importing session from desktop client")

// completion and validation
_ = cmd.MarkFlagDirname(desktop)
_ = cmd.Flags().MarkDeprecated("code", "use `-T code` instead")

return cmd
}

0 comments on commit 8127fb7

Please sign in to comment.