backupcodes

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 28, 2025 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// BackupCodeCount is the number of backup codes to generate
	BackupCodeCount = 10
	// BackupCodePartLength is the length of each part of the backup code (before and after hyphen)
	BackupCodePartLength = 5
)

Variables

This section is empty.

Functions

func GenerateEncryptionKey

func GenerateEncryptionKey() (string, error)

GenerateEncryptionKey generates a new 256-bit encryption key for backup codes

func NormalizeBackupCode

func NormalizeBackupCode(input string) string

NormalizeBackupCode normalizes user input by removing spaces and converting to expected format

func ValidateBackupCodeFormat

func ValidateBackupCodeFormat(code string) error

ValidateBackupCodeFormat validates that a backup code matches the expected format

Types

type BackupCode

type BackupCode struct {
	Code string `json:"code"`
}

BackupCode represents a single backup code

type BackupCodeEncryption

type BackupCodeEncryption struct {
	// contains filtered or unexported fields
}

BackupCodeEncryption handles encryption and decryption of backup codes

func NewBackupCodeEncryption

func NewBackupCodeEncryption() (*BackupCodeEncryption, error)

NewBackupCodeEncryption creates a new backup code encryption instance

func (*BackupCodeEncryption) Decrypt

func (e *BackupCodeEncryption) Decrypt(encodedCiphertext string) ([]byte, error)

Decrypt decrypts the given base64-encoded ciphertext using AES-256-GCM

func (*BackupCodeEncryption) Encrypt

func (e *BackupCodeEncryption) Encrypt(plaintext []byte) (string, error)

Encrypt encrypts the given plaintext using AES-256-GCM

type BackupCodeGenerator

type BackupCodeGenerator struct {
	// contains filtered or unexported fields
}

BackupCodeGenerator handles the generation of backup codes

func NewBackupCodeGenerator

func NewBackupCodeGenerator(db models.ServiceInterface) *BackupCodeGenerator

NewBackupCodeGenerator creates a new backup code generator

func (*BackupCodeGenerator) ConsumeBackupCode

func (g *BackupCodeGenerator) ConsumeBackupCode(ctx context.Context, userID int32, codeToConsume string, updatedBy string) (bool, error)

ConsumeBackupCode removes a backup code from the array and updates the count

func (*BackupCodeGenerator) GenerateAndStoreBackupCodes

func (g *BackupCodeGenerator) GenerateAndStoreBackupCodes(
	ctx context.Context,
	userID int32,
	updatedBy string,
) ([]string, error)

GenerateAndStoreBackupCodes generates backup codes and stores them in the database

func (*BackupCodeGenerator) GenerateBackupCodes

func (g *BackupCodeGenerator) GenerateBackupCodes() ([]string, error)

GenerateBackupCodes generates 10 unique backup codes in the format 'abcde-12345'

func (*BackupCodeGenerator) GetBackupCodes

func (g *BackupCodeGenerator) GetBackupCodes(ctx context.Context, userID int32) ([]BackupCode, error)

GetBackupCodes retrieves and decrypts backup codes for a user

func (*BackupCodeGenerator) GetBackupCodesCount

func (g *BackupCodeGenerator) GetBackupCodesCount(ctx context.Context, userID int32) (int, error)

GetBackupCodesCount returns the number of remaining backup codes without decryption

func (*BackupCodeGenerator) GetBackupCodesGeneratedAt

func (g *BackupCodeGenerator) GetBackupCodesGeneratedAt(ctx context.Context, userID int32) (string, error)

GetBackupCodesGeneratedAt returns when backup codes were generated without decryption

func (*BackupCodeGenerator) GetBackupCodesReadStatus

func (g *BackupCodeGenerator) GetBackupCodesReadStatus(ctx context.Context, userID int32) (bool, error)

GetBackupCodesReadStatus returns whether the user has seen their backup codes

func (*BackupCodeGenerator) UpdateBackupCodes

func (g *BackupCodeGenerator) UpdateBackupCodes(
	ctx context.Context,
	userID int32,
	codes []BackupCode,
	updatedBy string,
) error

UpdateBackupCodes stores updated backup codes (used when codes are consumed)

type EncryptionError

type EncryptionError struct {
	Operation string
	Err       error
}

EncryptionError represents an error during encryption/decryption

func (*EncryptionError) Error

func (e *EncryptionError) Error() string

func (*EncryptionError) Unwrap

func (e *EncryptionError) Unwrap() error

type Metadata

type Metadata struct {
	EncryptedBackupCodes string `json:"encrypted_backup_codes"`
	GeneratedAt          string `json:"generated_at"`
	CodesRemaining       int    `json:"codes_remaining"`
}

Metadata represents the JSON structure stored in the database

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL