Skip to content

Commit

Permalink
Improve [2FA] Error Message (#15)
Browse files Browse the repository at this point in the history
- [+] feat(error.go): add global error variables for common error cases
- [+] refactor(middleware.go): replace inline errors with global error variables
- [+] refactor(storage.go): replace inline errors with global error variables
- [+] refactor(storage.go): wrap errors using fmt.Errorf for ErrorFailedToDeleteInfo and ErrorFailedToResetStorage
  • Loading branch information
H0llyW00dzZ authored May 23, 2024
1 parent 91cfbb2 commit 574a30a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
20 changes: 20 additions & 0 deletions error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2024 H0llyW00dz All rights reserved.
//
// License: BSD 3-Clause License

package twofa

import "errors"

// Global error variables
var (
ErrorFailedToRetrieveInfo = errors.New("failed to retrieve 2FA information")
ErrorFailedToUnmarshalInfo = errors.New("failed to unmarshal 2FA information")
ErrorFailedToMarshalInfo = errors.New("failed to marshal updated 2FA information")
ErrorFailedToStoreInfo = errors.New("failed to store updated 2FA information")
ErrorFailedToDeleteInfo = errors.New("failed to delete 2FA information")
ErrorFailedToResetStorage = errors.New("failed to reset storage")
ErrorFailedToCloseStorage = errors.New("failed to close storage")
ErrorContextKeyNotSet = errors.New("ContextKey is not set")
ErrorFailedToRetrieveContextKey = errors.New("failed to retrieve context key")
)
5 changes: 2 additions & 3 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package twofa

import (
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -110,12 +109,12 @@ func (m *Middleware) isPathSkipped(path string) bool {
// getContextKey retrieves the context key from c.Locals using the provided ContextKey.
func (m *Middleware) getContextKey(c *fiber.Ctx) (string, error) {
if m.Config.ContextKey == "" {
return "", fmt.Errorf("ContextKey is not set")
return "", ErrorContextKeyNotSet
}

contextKeyValue, ok := c.Locals(m.Config.ContextKey).(string)
if !ok {
return "", fmt.Errorf("failed to retrieve context key")
return "", ErrorFailedToRetrieveContextKey
}

return contextKeyValue, nil
Expand Down
14 changes: 7 additions & 7 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (i *Info) SetExpirationTime(expiration time.Time) {
func (m *Middleware) getInfoFromStorage(contextKey string) (*Info, error) {
rawInfo, err := m.Config.Storage.Get(contextKey)
if err != nil {
return nil, fmt.Errorf("failed to retrieve 2FA information")
return nil, ErrorFailedToRetrieveInfo
}

if rawInfo == nil {
Expand All @@ -64,7 +64,7 @@ func (m *Middleware) getInfoFromStorage(contextKey string) (*Info, error) {

var info Info
if err := m.Config.JSONUnmarshal(rawInfo, &info); err != nil {
return nil, fmt.Errorf("failed to unmarshal 2FA information")
return nil, ErrorFailedToUnmarshalInfo
}

return &info, nil
Expand All @@ -74,12 +74,12 @@ func (m *Middleware) getInfoFromStorage(contextKey string) (*Info, error) {
func (m *Middleware) updateInfoInStorage(contextKey string, info *Info) error {
updatedRawInfo, err := m.Config.JSONMarshal(info)
if err != nil {
return fmt.Errorf("failed to marshal updated 2FA information")
return ErrorFailedToMarshalInfo
}

err = m.Config.Storage.Set(contextKey, updatedRawInfo, time.Duration(m.Config.CookieMaxAge)*time.Second)
if err != nil {
return fmt.Errorf("failed to store updated 2FA information")
return ErrorFailedToStoreInfo
}

return nil
Expand All @@ -89,7 +89,7 @@ func (m *Middleware) updateInfoInStorage(contextKey string, info *Info) error {
func (m *Middleware) deleteInfoFromStorage(contextKey string) error {
err := m.Config.Storage.Delete(contextKey)
if err != nil {
return fmt.Errorf("failed to delete 2FA information: %w", err)
return fmt.Errorf("%w: %v", ErrorFailedToDeleteInfo, err)
}

return nil
Expand All @@ -99,7 +99,7 @@ func (m *Middleware) deleteInfoFromStorage(contextKey string) error {
func (m *Middleware) resetStorage() error {
err := m.Config.Storage.Reset()
if err != nil {
return fmt.Errorf("failed to reset storage: %w", err)
return fmt.Errorf("%w: %v", ErrorFailedToResetStorage, err)
}

return nil
Expand All @@ -109,7 +109,7 @@ func (m *Middleware) resetStorage() error {
func (m *Middleware) closeStorage() error {
err := m.Config.Storage.Close()
if err != nil {
return fmt.Errorf("failed to close storage: %w", err)
return ErrorFailedToCloseStorage
}

return nil
Expand Down

0 comments on commit 574a30a

Please sign in to comment.