Documentation
¶
Index ¶
- Constants
- Variables
- func Decrypt(encryptedToken, password string) (string, error)
- func DeriveKey(password string, salt []byte) []byte
- func Encrypt(token, password string) (string, error)
- func IsEncrypted(token string) bool
- func IsTerminal(fd int) bool
- func PromptForPassword(prompt string) (string, error)
- type Config
- type DefaultIO
- type DefaultTokenValidator
- type TokenManager
- type TokenManagerIO
- type TokenValidationResult
- type TokenValidator
Constants ¶
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 ¶
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") )
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
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
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 IsTerminal ¶
IsTerminal determines if the given file descriptor is a terminal
func PromptForPassword ¶
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) PromptPassword ¶
PromptPassword prompts for a password using the existing PromptForPassword function
func (*DefaultIO) PromptWithConfirm ¶
PromptWithConfirm prompts for a password with confirmation
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