kflib

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: BSD-3-Clause Imports: 32 Imported by: 0

Documentation

Overview

Package kflib is a support library for the KeyFish tool.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoChange is reported by Edit if the resulting value did not change.
	ErrNoChange = errors.New("input was not changed")

	// ErrUserReject is reported by Edit if the user rejected the changed file.
	ErrUserReject = errors.New("the user rejected the edits")
)

Functions

func ConfirmPassphrase

func ConfirmPassphrase(prompt string) (string, error)

ConfirmPassphrase prompts the user at the terminal for a passphrase with echo disabled, then prompts again for confirmation and reports an error if the two copies are not equal.

func Edit

func Edit[T any](ctx context.Context, value T) (T, error)

Edit invokes an editor with the specified object rendered as YAML. The editor is selected by the EDITOR environment variable. When the editor exits, the user is prompted to confirm any changes. If they do, the results are unmarshaled back into a new value, which is returned; otherwise an error is reported.

If the edit did not change the input, Edit returns (value, ErrNoChange). If the user rejected the changes, Edit returns (value, ErrUserReject).

func GenerateHashpass added in v0.1.1

func GenerateHashpass(db *kfdb.DB, rec *kfdb.Record, tag string) (string, error)

GenerateHashpass hashpass password for the specified record in the given database. It reports an error if no hashpass secret is available. will be

func GenerateOTP

func GenerateOTP(url *otpauth.URL, offset int) (string, error)

GenerateOTP returns a TOTP code based on url. The time code is shifted by offset steps (based on the size of the window specified by url).

func GetPassphrase

func GetPassphrase(prompt string) (string, error)

GetPassphrase prompts the user at the terminal for a passphrase with echo disabled. An empty passprase is permitted; the caller must check for that case if an empty passphrase is not wanted.

func HashedChars added in v0.1.1

func HashedChars(length int, charset Charset, passphrase, seed, salt string) string

HashedChars creates a new HKDF password of the given length using the specified character types. A minimum length of 8 is enforced.

The passphrase is a strong secret passphrase. The seed is not secret, but must be fixed for a given context. The salt is optional, if non-empty it is mixed in to the HKDF as additional context.

func OpenDB

func OpenDB(dbPath string) (*kfdb.Store, error)

OpenDB opens the specified database store.

func OpenDBWithPassphrase added in v0.1.3

func OpenDBWithPassphrase(dbPath, passphrase string) (*kfdb.Store, error)

OpenDBWithPassphrase opens the specified database store using the provided access key passphrase instead of prompting at the terminal.

func RandomChars

func RandomChars(length int, charset Charset) string

RandomChars creates a new randomly-generated password of the given length and using the specified character types. A minimum length of 8 is enforced.

func RandomWords

func RandomWords(numWords int, joiner string) string

RandomWords creates a new randomly-generated password comprising the specified number of wordlist entries. The words are separated by the specified joiner. A minimum of 3 words is enforced.

func SaveDB

func SaveDB(s *kfdb.Store, dbPath string) error

SaveDB writes the specified database store to dbPath.

Types

type Charset

type Charset int

Charset is a bit mask specifying which letters to use in a character-based password. A Charset always includes letters.

const (
	// Letters denotes the capital and lowercase ASCII English letters.
	Letters Charset = 0

	// Digits denotes the set of ASCII decimal digits.
	Digits Charset = 1

	// Symbols denotes a set of ASCII punctuation symbols.
	Symbols Charset = 2

	// AllChars denotes a combination of letters, digits, and symbols.
	AllChars = Letters | Digits | Symbols
)

type DBWatcher

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

DBWatcher is a database connected with a file path watcher, that reloads the file when it is modified.

func NewDBWatcher added in v0.1.3

func NewDBWatcher(s *kfdb.Store, dbPath, passphrase string) (*DBWatcher, error)

NewDBWatcher creates a watcher that automatically reloads the specified store from its original path when that path is modified.

func (*DBWatcher) Run

func (w *DBWatcher) Run(ctx context.Context)

Run monitors for changes to the database path in w, and updates it when the underlying file is modified. Run should be run in a separate goroutine. It exits when the watcher closes, or ctx ends.

func (*DBWatcher) Store

func (w *DBWatcher) Store() *kfdb.Store

Store returns the current database. If an update is available, Store tries to load it, but in case of error it falls back to the existing value.

type FindResult

type FindResult struct {
	Tag    string       // the tag from the query, if present
	Index  int          // offset of the record in the database
	Record *kfdb.Record // the record matched by the label
}

FindResult is the result of a successful call to FindRecord.

func FindRecord

func FindRecord(db *kfdb.DB, query string, all bool) (FindResult, error)

FindRecord finds the unique record matching the specified query. An exact match for a label is preferred; otherwise FindRecord will look for a full or partial match on host names, or other substrings in the title and notes. An error is reported if query matches no records, or more than 1. If all is true, all records are considered; otherwise archived records are skipped.

If the query begins with a tag (tag@label), the tag is removed and returned along with the result.

type FoundRecord

type FoundRecord struct {
	Quality MatchQuality `json:"quality"` // how this record was matched
	Index   int          `json:"index"`   // the index of the record in the database
	Record  *kfdb.Record `json:"record"`  // the record itself
}

FoundRecord is a single record reported by FindRecords.

func FindRecords

func FindRecords(recs []*kfdb.Record, query string) []FoundRecord

FindRecords finds candidate records matching the specified query. If the query begins with a tag (tag@label), the tag is removed. Results are returned in order of quality from highest to lowest, with ties broken by index.

func PickBest added in v0.2.0

func PickBest(found []FoundRecord) (FoundRecord, bool)

PickBest reports whether there is a unique "best" match in a slice of found records, and if so returns that specific record. The records must be ordered in decreasing order of match quality.

type MatchQuality

type MatchQuality int

MatchQuality indicates how good a match a query is for a record.

const (
	// MatchNone means the query does not match the record at all.
	MatchNone MatchQuality = iota

	// MatchLabel means the query matches the record's label.
	MatchLabel

	// MatchHost means the query is an exact host match for the record.
	MatchHost

	// MatchHostPartial means the query is a partial host match for the record.
	MatchHostPartial

	// MatchTitle means the query is a case-insensitive substring match for the
	// title or label of the record.
	MatchTitle

	// MatchDetail means the query is a case-insensitive substring match for the
	// label of one of the details of the record.
	MatchDetail

	// MatchSubstring means the query is a case-insensitive substring match for
	// one of the text fields or host entries of the record.
	MatchSubstring
)

func MatchRecord

func MatchRecord(query string, r *kfdb.Record) MatchQuality

MatchRecord reports how good a match query is for the specified record.

Jump to

Keyboard shortcuts

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