mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-04-01 03:17:55 +00:00
* chore(deps): bump github.com/charmbracelet/huh from 0.8.0 to 1.0.0 Bumps [github.com/charmbracelet/huh](https://github.com/charmbracelet/huh) from 0.8.0 to 1.0.0. - [Release notes](https://github.com/charmbracelet/huh/releases) - [Commits](https://github.com/charmbracelet/huh/compare/v0.8.0...v1.0.0) --- updated-dependencies: - dependency-name: github.com/charmbracelet/huh dependency-version: 1.0.0 dependency-type: direct:production update-type: version-update:semver-major ... Signed-off-by: dependabot[bot] <support@github.com> * chore: breaking changes for huh form * chore: bump go version everywhere * chore: go mod tidy --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: Stavros <steveiliop56@gmail.com>
118 lines
2.6 KiB
Go
118 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/steveiliop56/tinyauth/internal/utils"
|
|
"github.com/steveiliop56/tinyauth/internal/utils/tlog"
|
|
|
|
"charm.land/huh/v2"
|
|
"github.com/mdp/qrterminal/v3"
|
|
"github.com/pquerna/otp/totp"
|
|
"github.com/traefik/paerser/cli"
|
|
)
|
|
|
|
type GenerateTotpConfig struct {
|
|
Interactive bool `description:"Generate a TOTP secret interactively."`
|
|
User string `description:"Your current user (username:hash)."`
|
|
}
|
|
|
|
func NewGenerateTotpConfig() *GenerateTotpConfig {
|
|
return &GenerateTotpConfig{
|
|
Interactive: false,
|
|
User: "",
|
|
}
|
|
}
|
|
|
|
func generateTotpCmd() *cli.Command {
|
|
tCfg := NewGenerateTotpConfig()
|
|
|
|
loaders := []cli.ResourceLoader{
|
|
&cli.FlagLoader{},
|
|
}
|
|
|
|
return &cli.Command{
|
|
Name: "generate",
|
|
Description: "Generate a TOTP secret",
|
|
Configuration: tCfg,
|
|
Resources: loaders,
|
|
Run: func(_ []string) error {
|
|
tlog.NewSimpleLogger().Init()
|
|
|
|
if tCfg.Interactive {
|
|
form := huh.NewForm(
|
|
huh.NewGroup(
|
|
huh.NewInput().Title("Current user (username:hash)").Value(&tCfg.User).Validate((func(s string) error {
|
|
if s == "" {
|
|
return errors.New("user cannot be empty")
|
|
}
|
|
return nil
|
|
})),
|
|
),
|
|
)
|
|
|
|
theme := new(themeBase)
|
|
err := form.WithTheme(theme).Run()
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to run interactive prompt: %w", err)
|
|
}
|
|
}
|
|
|
|
user, err := utils.ParseUser(tCfg.User)
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to parse user: %w", err)
|
|
}
|
|
|
|
docker := false
|
|
if strings.Contains(tCfg.User, "$$") {
|
|
docker = true
|
|
}
|
|
|
|
if user.TotpSecret != "" {
|
|
return fmt.Errorf("user already has a TOTP secret")
|
|
}
|
|
|
|
key, err := totp.Generate(totp.GenerateOpts{
|
|
Issuer: "Tinyauth",
|
|
AccountName: user.Username,
|
|
})
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("failed to generate TOTP secret: %w", err)
|
|
}
|
|
|
|
secret := key.Secret()
|
|
|
|
tlog.App.Info().Str("secret", secret).Msg("Generated TOTP secret")
|
|
|
|
tlog.App.Info().Msg("Generated QR code")
|
|
|
|
config := qrterminal.Config{
|
|
Level: qrterminal.L,
|
|
Writer: os.Stdout,
|
|
BlackChar: qrterminal.BLACK,
|
|
WhiteChar: qrterminal.WHITE,
|
|
QuietZone: 2,
|
|
}
|
|
|
|
qrterminal.GenerateWithConfig(key.URL(), config)
|
|
|
|
user.TotpSecret = secret
|
|
|
|
// If using docker escape re-escape it
|
|
if docker {
|
|
user.Password = strings.ReplaceAll(user.Password, "$", "$$")
|
|
}
|
|
|
|
tlog.App.Info().Str("user", fmt.Sprintf("%s:%s:%s", user.Username, user.Password, user.TotpSecret)).Msg("Add the totp secret to your authenticator app then use the verify command to ensure everything is working correctly.")
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|