mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2026-01-15 20:02:30 +00:00
* Refactor logging to use centralized logger utility - Removed direct usage of zerolog in multiple files and replaced it with a centralized logging utility in the `utils` package. - Introduced `Loggers` struct to manage different loggers (Audit, HTTP, App) with configurable levels and outputs. - Updated all relevant files to utilize the new logging structure, ensuring consistent logging practices across the application. - Enhanced error handling and logging messages for better traceability and debugging. * refactor: update logging implementation to use new logger structure * Refactor logging to use tlog package - Replaced instances of utils logging with tlog in various controllers, services, and middleware. - Introduced audit logging for login success, login failure, and logout events. - Created tlog package with structured logging capabilities using zerolog. - Added tests for the new tlog logger functionality. * refactor: update logging configuration in environment files * fix: adding coderabbit suggestions * fix: ensure correct audit caller * fix: include reason in audit login failure logs
119 lines
2.8 KiB
Go
119 lines
2.8 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/steveiliop56/tinyauth/internal/utils"
|
|
"github.com/steveiliop56/tinyauth/internal/utils/tlog"
|
|
|
|
"github.com/charmbracelet/huh"
|
|
"github.com/pquerna/otp/totp"
|
|
"github.com/traefik/paerser/cli"
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
type VerifyUserConfig struct {
|
|
Interactive bool `description:"Validate a user interactively."`
|
|
Username string `description:"Username."`
|
|
Password string `description:"Password."`
|
|
Totp string `description:"TOTP code."`
|
|
User string `description:"Hash (username:hash:totp)."`
|
|
}
|
|
|
|
func NewVerifyUserConfig() *VerifyUserConfig {
|
|
return &VerifyUserConfig{
|
|
Interactive: false,
|
|
Username: "",
|
|
Password: "",
|
|
Totp: "",
|
|
User: "",
|
|
}
|
|
}
|
|
|
|
func verifyUserCmd() *cli.Command {
|
|
tCfg := NewVerifyUserConfig()
|
|
|
|
loaders := []cli.ResourceLoader{
|
|
&cli.FlagLoader{},
|
|
}
|
|
|
|
return &cli.Command{
|
|
Name: "verify",
|
|
Description: "Verify a user is set up correctly.",
|
|
Configuration: tCfg,
|
|
Resources: loaders,
|
|
Run: func(_ []string) error {
|
|
tlog.NewSimpleLogger().Init()
|
|
|
|
if tCfg.Interactive {
|
|
form := huh.NewForm(
|
|
huh.NewGroup(
|
|
huh.NewInput().Title("User (username:hash:totp)").Value(&tCfg.User).Validate((func(s string) error {
|
|
if s == "" {
|
|
return errors.New("user cannot be empty")
|
|
}
|
|
return nil
|
|
})),
|
|
huh.NewInput().Title("Username").Value(&tCfg.Username).Validate((func(s string) error {
|
|
if s == "" {
|
|
return errors.New("username cannot be empty")
|
|
}
|
|
return nil
|
|
})),
|
|
huh.NewInput().Title("Password").Value(&tCfg.Password).Validate((func(s string) error {
|
|
if s == "" {
|
|
return errors.New("password cannot be empty")
|
|
}
|
|
return nil
|
|
})),
|
|
huh.NewInput().Title("TOTP Code (optional)").Value(&tCfg.Totp),
|
|
),
|
|
)
|
|
|
|
var baseTheme *huh.Theme = huh.ThemeBase()
|
|
|
|
err := form.WithTheme(baseTheme).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)
|
|
}
|
|
|
|
if user.Username != tCfg.Username {
|
|
return fmt.Errorf("username is incorrect")
|
|
}
|
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(tCfg.Password))
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("password is incorrect: %w", err)
|
|
}
|
|
|
|
if user.TotpSecret == "" {
|
|
if tCfg.Totp != "" {
|
|
tlog.App.Warn().Msg("User does not have TOTP secret")
|
|
}
|
|
tlog.App.Info().Msg("User verified")
|
|
return nil
|
|
}
|
|
|
|
ok := totp.Validate(tCfg.Totp, user.TotpSecret)
|
|
|
|
if !ok {
|
|
return fmt.Errorf("TOTP code incorrect")
|
|
}
|
|
|
|
tlog.App.Info().Msg("User verified")
|
|
|
|
return nil
|
|
},
|
|
}
|
|
}
|