mirror of
https://github.com/steveiliop56/tinyauth.git
synced 2025-10-28 04:35:40 +00:00
refactor: log errors
This commit is contained in:
@@ -1,7 +1,6 @@
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
cmd "tinyauth/cmd/user"
|
cmd "tinyauth/cmd/user"
|
||||||
"tinyauth/internal/api"
|
"tinyauth/internal/api"
|
||||||
"tinyauth/internal/auth"
|
"tinyauth/internal/auth"
|
||||||
@@ -38,7 +37,6 @@ var rootCmd = &cobra.Command{
|
|||||||
|
|
||||||
if config.UsersFile == "" && config.Users == "" {
|
if config.UsersFile == "" && config.Users == "" {
|
||||||
log.Fatal().Msg("No users provided")
|
log.Fatal().Msg("No users provided")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
usersString := config.Users
|
usersString := config.Users
|
||||||
@@ -108,14 +106,12 @@ func Execute() {
|
|||||||
err := rootCmd.Execute()
|
err := rootCmd.Execute()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg("Failed to execute command")
|
log.Fatal().Err(err).Msg("Failed to execute command")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func HandleError(err error, msg string) {
|
func HandleError(err error, msg string) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal().Err(err).Msg(msg)
|
log.Fatal().Err(err).Msg(msg)
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package create
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/charmbracelet/huh"
|
"github.com/charmbracelet/huh"
|
||||||
@@ -18,9 +17,9 @@ var password string
|
|||||||
var docker bool
|
var docker bool
|
||||||
|
|
||||||
var CreateCmd = &cobra.Command{
|
var CreateCmd = &cobra.Command{
|
||||||
Use: "create",
|
Use: "create",
|
||||||
Short: "Create a user",
|
Short: "Create a user",
|
||||||
Long: `Create a user either interactively or by passing flags.`,
|
Long: `Create a user either interactively or by passing flags.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if interactive {
|
if interactive {
|
||||||
form := huh.NewForm(
|
form := huh.NewForm(
|
||||||
@@ -47,13 +46,11 @@ var CreateCmd = &cobra.Command{
|
|||||||
|
|
||||||
if formErr != nil {
|
if formErr != nil {
|
||||||
log.Fatal().Err(formErr).Msg("Form failed")
|
log.Fatal().Err(formErr).Msg("Form failed")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if username == "" || password == "" {
|
if username == "" || password == "" {
|
||||||
log.Error().Msg("Username and password cannot be empty")
|
log.Error().Msg("Username and password cannot be empty")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info().Str("username", username).Str("password", password).Bool("docker", docker).Msg("Creating user")
|
log.Info().Str("username", username).Str("password", password).Bool("docker", docker).Msg("Creating user")
|
||||||
@@ -62,7 +59,6 @@ var CreateCmd = &cobra.Command{
|
|||||||
|
|
||||||
if passwordErr != nil {
|
if passwordErr != nil {
|
||||||
log.Fatal().Err(passwordErr).Msg("Failed to hash password")
|
log.Fatal().Err(passwordErr).Msg("Failed to hash password")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
passwordString := string(passwordByte)
|
passwordString := string(passwordByte)
|
||||||
@@ -80,4 +76,4 @@ func init() {
|
|||||||
CreateCmd.Flags().BoolVar(&docker, "docker", false, "Format output for docker")
|
CreateCmd.Flags().BoolVar(&docker, "docker", false, "Format output for docker")
|
||||||
CreateCmd.Flags().StringVar(&username, "username", "", "Username")
|
CreateCmd.Flags().StringVar(&username, "username", "", "Username")
|
||||||
CreateCmd.Flags().StringVar(&password, "password", "", "Password")
|
CreateCmd.Flags().StringVar(&password, "password", "", "Password")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package verify
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/charmbracelet/huh"
|
"github.com/charmbracelet/huh"
|
||||||
@@ -18,9 +17,9 @@ var docker bool
|
|||||||
var user string
|
var user string
|
||||||
|
|
||||||
var VerifyCmd = &cobra.Command{
|
var VerifyCmd = &cobra.Command{
|
||||||
Use: "verify",
|
Use: "verify",
|
||||||
Short: "Verify a user is set up correctly",
|
Short: "Verify a user is set up correctly",
|
||||||
Long: `Verify a user is set up correctly meaning that it has a correct password.`,
|
Long: `Verify a user is set up correctly meaning that it has a correct password.`,
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
if interactive {
|
if interactive {
|
||||||
form := huh.NewForm(
|
form := huh.NewForm(
|
||||||
@@ -53,23 +52,19 @@ var VerifyCmd = &cobra.Command{
|
|||||||
|
|
||||||
if formErr != nil {
|
if formErr != nil {
|
||||||
log.Fatal().Err(formErr).Msg("Form failed")
|
log.Fatal().Err(formErr).Msg("Form failed")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if username == "" || password == "" || user == "" {
|
if username == "" || password == "" || user == "" {
|
||||||
log.Error().Msg("Username, password and user cannot be empty")
|
log.Fatal().Msg("Username, password and user cannot be empty")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
log.Info().Str("user", user).Str("username", username).Str("password", password).Bool("docker", docker).Msg("Verifying user")
|
log.Info().Str("user", user).Str("username", username).Str("password", password).Bool("docker", docker).Msg("Verifying user")
|
||||||
|
|
||||||
userSplit := strings.Split(user, ":")
|
userSplit := strings.Split(user, ":")
|
||||||
|
|
||||||
if userSplit[1] == "" {
|
if userSplit[1] == "" {
|
||||||
log.Error().Msg("User is not formatted correctly")
|
log.Fatal().Msg("User is not formatted correctly")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if docker {
|
if docker {
|
||||||
@@ -79,8 +74,7 @@ var VerifyCmd = &cobra.Command{
|
|||||||
verifyErr := bcrypt.CompareHashAndPassword([]byte(userSplit[1]), []byte(password))
|
verifyErr := bcrypt.CompareHashAndPassword([]byte(userSplit[1]), []byte(password))
|
||||||
|
|
||||||
if verifyErr != nil || username != userSplit[0] {
|
if verifyErr != nil || username != userSplit[0] {
|
||||||
log.Error().Msg("Username or password incorrect")
|
log.Fatal().Msg("Username or password incorrect")
|
||||||
os.Exit(1)
|
|
||||||
} else {
|
} else {
|
||||||
log.Info().Msg("Verification successful")
|
log.Info().Msg("Verification successful")
|
||||||
}
|
}
|
||||||
@@ -93,4 +87,4 @@ func init() {
|
|||||||
VerifyCmd.Flags().StringVar(&username, "username", "", "Username")
|
VerifyCmd.Flags().StringVar(&username, "username", "", "Username")
|
||||||
VerifyCmd.Flags().StringVar(&password, "password", "", "Password")
|
VerifyCmd.Flags().StringVar(&password, "password", "", "Password")
|
||||||
VerifyCmd.Flags().StringVar(&user, "user", "", "Hash (user:hash combination)")
|
VerifyCmd.Flags().StringVar(&user, "user", "", "Hash (user:hash combination)")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ func (api *API) Init() {
|
|||||||
|
|
||||||
if distErr != nil {
|
if distErr != nil {
|
||||||
log.Fatal().Err(distErr).Msg("Failed to get UI assets")
|
log.Fatal().Err(distErr).Msg("Failed to get UI assets")
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fileServer := http.FileServer(http.FS(dist))
|
fileServer := http.FileServer(http.FS(dist))
|
||||||
@@ -101,6 +100,7 @@ func (api *API) SetupRoutes() {
|
|||||||
userContext, userContextErr := api.Hooks.UseUserContext(c)
|
userContext, userContextErr := api.Hooks.UseUserContext(c)
|
||||||
|
|
||||||
if userContextErr != nil {
|
if userContextErr != nil {
|
||||||
|
log.Error().Err(userContextErr).Msg("Failed to get user context")
|
||||||
c.JSON(500, gin.H{
|
c.JSON(500, gin.H{
|
||||||
"status": 500,
|
"status": 500,
|
||||||
"message": "Internal Server Error",
|
"message": "Internal Server Error",
|
||||||
@@ -124,6 +124,7 @@ func (api *API) SetupRoutes() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if queryErr != nil {
|
if queryErr != nil {
|
||||||
|
log.Error().Err(queryErr).Msg("Failed to build query")
|
||||||
c.JSON(501, gin.H{
|
c.JSON(501, gin.H{
|
||||||
"status": 501,
|
"status": 501,
|
||||||
"message": "Internal Server Error",
|
"message": "Internal Server Error",
|
||||||
@@ -140,6 +141,7 @@ func (api *API) SetupRoutes() {
|
|||||||
err := c.BindJSON(&login)
|
err := c.BindJSON(&login)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Error().Err(err).Msg("Failed to bind JSON")
|
||||||
c.JSON(400, gin.H{
|
c.JSON(400, gin.H{
|
||||||
"status": 400,
|
"status": 400,
|
||||||
"message": "Bad Request",
|
"message": "Bad Request",
|
||||||
@@ -192,6 +194,7 @@ func (api *API) SetupRoutes() {
|
|||||||
userContext, userContextErr := api.Hooks.UseUserContext(c)
|
userContext, userContextErr := api.Hooks.UseUserContext(c)
|
||||||
|
|
||||||
if userContextErr != nil {
|
if userContextErr != nil {
|
||||||
|
log.Error().Err(userContextErr).Msg("Failed to get user context")
|
||||||
c.JSON(500, gin.H{
|
c.JSON(500, gin.H{
|
||||||
"status": 500,
|
"status": 500,
|
||||||
"message": "Internal Server Error",
|
"message": "Internal Server Error",
|
||||||
@@ -238,6 +241,7 @@ func (api *API) SetupRoutes() {
|
|||||||
bindErr := c.BindUri(&request)
|
bindErr := c.BindUri(&request)
|
||||||
|
|
||||||
if bindErr != nil {
|
if bindErr != nil {
|
||||||
|
log.Error().Err(bindErr).Msg("Failed to bind URI")
|
||||||
c.JSON(400, gin.H{
|
c.JSON(400, gin.H{
|
||||||
"status": 400,
|
"status": 400,
|
||||||
"message": "Bad Request",
|
"message": "Bad Request",
|
||||||
@@ -276,6 +280,7 @@ func (api *API) SetupRoutes() {
|
|||||||
bindErr := c.BindUri(&providerName)
|
bindErr := c.BindUri(&providerName)
|
||||||
|
|
||||||
if bindErr != nil {
|
if bindErr != nil {
|
||||||
|
log.Error().Err(bindErr).Msg("Failed to bind URI")
|
||||||
c.JSON(400, gin.H{
|
c.JSON(400, gin.H{
|
||||||
"status": 400,
|
"status": 400,
|
||||||
"message": "Bad Request",
|
"message": "Bad Request",
|
||||||
@@ -306,6 +311,7 @@ func (api *API) SetupRoutes() {
|
|||||||
token, tokenErr := provider.ExchangeToken(code)
|
token, tokenErr := provider.ExchangeToken(code)
|
||||||
|
|
||||||
if tokenErr != nil {
|
if tokenErr != nil {
|
||||||
|
log.Error().Err(tokenErr).Msg("Failed to exchange token")
|
||||||
c.JSON(500, gin.H{
|
c.JSON(500, gin.H{
|
||||||
"status": 500,
|
"status": 500,
|
||||||
"message": "Internal Server Error",
|
"message": "Internal Server Error",
|
||||||
@@ -333,6 +339,7 @@ func (api *API) SetupRoutes() {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if queryErr != nil {
|
if queryErr != nil {
|
||||||
|
log.Error().Err(queryErr).Msg("Failed to build query")
|
||||||
c.JSON(501, gin.H{
|
c.JSON(501, gin.H{
|
||||||
"status": 501,
|
"status": 501,
|
||||||
"message": "Internal Server Error",
|
"message": "Internal Server Error",
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -33,7 +32,6 @@ func (oauth *OAuth) GetAuthURL() string {
|
|||||||
func (oauth *OAuth) ExchangeToken(code string) (string, error) {
|
func (oauth *OAuth) ExchangeToken(code string) (string, error) {
|
||||||
token, err := oauth.Config.Exchange(oauth.Context, code, oauth2.VerifierOption(oauth.Verifier))
|
token, err := oauth.Config.Exchange(oauth.Context, code, oauth2.VerifierOption(oauth.Verifier))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Failed to exchange code")
|
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
oauth.Token = token
|
oauth.Token = token
|
||||||
|
|||||||
2
main.go
2
main.go
@@ -14,7 +14,7 @@ func main() {
|
|||||||
// Logger
|
// Logger
|
||||||
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).With().Timestamp().Logger()
|
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stderr, TimeFormat: time.RFC3339}).With().Timestamp().Logger()
|
||||||
log.Info().Str("version", assets.Version).Msg("Starting tinyauth")
|
log.Info().Str("version", assets.Version).Msg("Starting tinyauth")
|
||||||
|
|
||||||
// Run cmd
|
// Run cmd
|
||||||
cmd.Execute()
|
cmd.Execute()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user