MCPcopy
hub / github.com/tinyauthapp/tinyauth / generateTotpCmd

Function generateTotpCmd

cmd/tinyauth/generate_totp.go:30–117  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

28}
29
30func generateTotpCmd() *cli.Command {
31 tCfg := NewGenerateTotpConfig()
32
33 loaders := []cli.ResourceLoader{
34 &cli.FlagLoader{},
35 }
36
37 return &cli.Command{
38 Name: "generate",
39 Description: "Generate a TOTP secret",
40 Configuration: tCfg,
41 Resources: loaders,
42 Run: func(_ []string) error {
43 tlog.NewSimpleLogger().Init()
44
45 if tCfg.Interactive {
46 form := huh.NewForm(
47 huh.NewGroup(
48 huh.NewInput().Title("Current user (username:hash)").Value(&tCfg.User).Validate((func(s string) error {
49 if s == "" {
50 return errors.New("user cannot be empty")
51 }
52 return nil
53 })),
54 ),
55 )
56
57 theme := new(themeBase)
58 err := form.WithTheme(theme).Run()
59
60 if err != nil {
61 return fmt.Errorf("failed to run interactive prompt: %w", err)
62 }
63 }
64
65 user, err := utils.ParseUser(tCfg.User)
66
67 if err != nil {
68 return fmt.Errorf("failed to parse user: %w", err)
69 }
70
71 docker := false
72 if strings.Contains(tCfg.User, "$$") {
73 docker = true
74 }
75
76 if user.TotpSecret != "" {
77 return fmt.Errorf("user already has a TOTP secret")
78 }
79
80 key, err := totp.Generate(totp.GenerateOpts{
81 Issuer: "Tinyauth",
82 AccountName: user.Username,
83 })
84
85 if err != nil {
86 return fmt.Errorf("failed to generate TOTP secret: %w", err)
87 }

Callers 1

mainFunction · 0.85

Calls 4

NewSimpleLoggerFunction · 0.92
ParseUserFunction · 0.92
NewGenerateTotpConfigFunction · 0.85
InitMethod · 0.45

Tested by

no test coverage detected