encryption

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2026 License: Apache-2.0 Imports: 14 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	// KeySize is the size of the AES key in bytes
	KeySize = 32 // AES-256
	// SaltSize is the size of the salt in bytes
	SaltSize = 16
	// Iterations is the number of iterations for PBKDF2
	Iterations = 10000
	// Prefix is used to identify encrypted tokens
	Prefix = "enc:"
)

Variables

View Source
var (
	// ErrInvalidEncryptedData is returned when attempting to decrypt invalid data
	ErrInvalidEncryptedData = errors.New("invalid encrypted data")
	// ErrInvalidPassword is returned when the password is invalid
	ErrInvalidPassword = errors.New("invalid password")
	// ErrNotEncrypted is returned when a token is not encrypted
	ErrNotEncrypted = errors.New("token is not encrypted")
)
View Source
var PromptForAPIToken = func() (string, error) {
	fmt.Print("Enter your Mist API token (input will not be displayed): ")

	tokenBytes, err := term.ReadPassword(int(os.Stdin.Fd()))
	if err != nil {
		return "", fmt.Errorf("failed to read API token: %w", err)
	}
	fmt.Println()

	token := strings.TrimSpace(string(tokenBytes))

	if token == "" {
		return "", fmt.Errorf("API token cannot be empty")
	}

	return token, nil
}

PromptForAPIToken prompts the user to enter an API token Implements requirement from encryption.txt to not echo the input

View Source
var PromptForExistingPassword = func() (string, error) {
	return PromptForPassword("Enter your password to decrypt the API token (input will not be displayed): ")
}

PromptForExistingPassword prompts the user to enter an existing password

View Source
var PromptForNewPassword = func() (string, error) {
	fmt.Println("Please set a password to encrypt your API token.")
	password, err := promptWithConfirmation(
		"Enter a password (input will not be displayed): ",
		"Confirm your password (input will not be displayed): ",
	)
	if err != nil {
		return "", err
	}

	fmt.Println("Password confirmed. Your API token will be encrypted and saved.")
	return password, nil
}

PromptForNewPassword prompts the user to enter a new password Implements requirement from encryption.txt #3 to verify password with double entry

Functions

func Decrypt

func Decrypt(encryptedToken, password string) (string, error)

Decrypt decrypts a token with the given password

func DeriveKey

func DeriveKey(password string, salt []byte) []byte

DeriveKey derives an encryption key from a password and salt

func Encrypt

func Encrypt(token, password string) (string, error)

Encrypt encrypts a token with the given password

func IsEncrypted

func IsEncrypted(token string) bool

IsEncrypted checks if a token is encrypted

func IsTerminal

func IsTerminal(fd int) bool

IsTerminal determines if the given file descriptor is a terminal

func PromptForPassword

func PromptForPassword(prompt string) (string, error)

PromptForPassword prompts for a password with the given prompt message

Types

type Config

type Config interface {
	GetAPIToken() string
	SetAPIToken(token string)
	SetKeyEncrypted(encrypted bool)
	GetAPIURL() string
	GetOrgID() string
	SetOrgID(id string)
	Save(configPath string) error
	SetCurrentToken(token string)
	GetCurrentToken() string
}

We'll define a simple Config interface to avoid direct dependency on config package

type DefaultIO

type DefaultIO struct{}

DefaultIO implements TokenManagerIO using standard methods

func NewDefaultIO

func NewDefaultIO() *DefaultIO

func (*DefaultIO) Print

func (io *DefaultIO) Print(msg string)

Print outputs a message without a newline

func (*DefaultIO) Println

func (io *DefaultIO) Println(msg string)

Println outputs a message with a newline

func (*DefaultIO) PromptPassword

func (io *DefaultIO) PromptPassword(prompt string) (string, error)

PromptPassword prompts for a password using the existing PromptForPassword function

func (*DefaultIO) PromptWithConfirm

func (io *DefaultIO) PromptWithConfirm(initialPrompt, confirmPrompt string) (string, error)

PromptWithConfirm prompts for a password with confirmation

func (*DefaultIO) Scanln

func (io *DefaultIO) Scanln(value *string) error

Scanln reads a line of input

type DefaultTokenValidator

type DefaultTokenValidator struct{}

DefaultTokenValidator provides a default implementation (to be replaced in main.go)

func (*DefaultTokenValidator) ValidateToken

func (v *DefaultTokenValidator) ValidateToken(ctx context.Context, apiBaseURL, token string) (*TokenValidationResult, error)

ValidateToken validates an API token against the Mist API This is a stub that will be implemented in main.go to avoid circular dependencies

type TokenManager

type TokenManager struct {
	Config    Config
	Validator TokenValidator
	APIURL    string
	Warnings  []string
	IO        TokenManagerIO
}

TokenManager handles token validation, encryption, and decryption workflows

func NewTokenManager

func NewTokenManager(cfg Config, validator TokenValidator) *TokenManager

NewTokenManager creates a new token manager

func (*TokenManager) ForceUpdateToken

func (tm *TokenManager) ForceUpdateToken(ctx context.Context) error

ForceUpdateToken forces the update of a token

func (*TokenManager) InitializeToken

func (tm *TokenManager) InitializeToken(ctx context.Context) error

InitializeToken handles the token validation and encryption workflow at application startup

func (*TokenManager) SetIO

func (tm *TokenManager) SetIO(io TokenManagerIO)

SetIO sets a custom IO implementation

type TokenManagerIO

type TokenManagerIO interface {
	// Print outputs a message without a newline
	Print(msg string)

	// Println outputs a message with a newline
	Println(msg string)

	// Scanln reads a line of input
	Scanln(value *string) error

	// PromptPassword prompts for a password (should be hidden during input)
	PromptPassword(prompt string) (string, error)

	// PromptWithConfirm prompts for a password with confirmation
	PromptWithConfirm(initialPrompt, confirmPrompt string) (string, error)
}

TokenManagerIO defines the interface for input/output operations in the token manager

type TokenValidationResult

type TokenValidationResult struct {
	Valid    bool
	Message  string
	UserName string
	OrgID    string
	OrgName  string
}

TokenValidationResult represents the result of a token validation operation

func ValidateAndEncryptToken

func ValidateAndEncryptToken(ctx context.Context, validator TokenValidator, apiBaseURL, token, password string) (string, *TokenValidationResult, error)

ValidateAndEncryptToken validates a token and encrypts it if valid

func ValidateEncryptedToken

func ValidateEncryptedToken(ctx context.Context, validator TokenValidator, apiBaseURL, encryptedToken, password string) (*TokenValidationResult, error)

ValidateEncryptedToken validates an encrypted token by decrypting and checking it

type TokenValidator

type TokenValidator interface {
	ValidateToken(ctx context.Context, apiBaseURL, token string) (*TokenValidationResult, error)
}

TokenValidator defines an interface for token validation

Jump to

Keyboard shortcuts

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