Documentation
¶
Overview ¶
Package token provides encrypted token persistence for the krci CLI.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrInvalidCiphertext indicates data shorter than a nonce and tag, or data // that does not decrypt under a loaded key. ErrInvalidCiphertext = errors.New("invalid ciphertext") // ErrKeyNotFound indicates no encryption key is stored. ErrKeyNotFound = errors.New("encryption key not found") )
var ErrKeyringAccess = errors.New("unable to access OS keyring")
ErrKeyringAccess indicates the OS keyring is not available.
var ErrNoToken = errors.New("no stored token")
ErrNoToken indicates no usable stored token.
Functions ¶
This section is empty.
Types ¶
type EncryptedFileStore ¶
type EncryptedFileStore struct {
// contains filtered or unexported fields
}
EncryptedFileStore persists tokens as AES-256-GCM encrypted JSON files. Save, Load, and Clear are safe for concurrent use; each holds mu for its whole duration.
func NewEncryptedStore ¶
func NewEncryptedStore(path string, enc Encryptor, w io.Writer) *EncryptedFileStore
NewEncryptedStore creates a token store that encrypts data at rest. Token-recovery warnings are written to w, which must be non-nil. Panics if w is nil.
func (*EncryptedFileStore) Clear ¶
func (s *EncryptedFileStore) Clear() error
Clear removes the stored token file.
func (*EncryptedFileStore) Load ¶
func (s *EncryptedFileStore) Load() (*StoredToken, error)
Load reads and decrypts the token from disk.
- ErrInvalidCiphertext: the file is reported on the store's writer and removed; once removed, Load returns ErrNoToken silently.
- ErrKeyNotFound: the file is kept and reported on the writer once per store; Load returns ErrNoToken and the next Save replaces the file.
- Any other Decrypt failure, such as a key that cannot be loaded: the file is kept and the error returned.
func (*EncryptedFileStore) Save ¶
func (s *EncryptedFileStore) Save(tok *StoredToken) error
Save encrypts and writes the token to disk atomically (temp file + fsync + rename).
type Encryptor ¶
type Encryptor interface {
Encrypt(plaintext []byte) ([]byte, error)
Decrypt(ciphertext []byte) ([]byte, error)
}
Encryptor encrypts and decrypts token data using AES-256-GCM. Decrypt wraps ErrInvalidCiphertext only for data too short to decrypt or data that does not decrypt under the loaded key, and ErrKeyNotFound only when no key is stored. Decrypt does not create a key.
type Store ¶
type Store interface {
Save(tok *StoredToken) error
Load() (*StoredToken, error)
Clear() error
}
Store persists and retrieves encrypted OAuth tokens.
type StoredToken ¶
type StoredToken struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
IDToken string `json:"id_token"`
// ExpiresAt is the access-token expiry.
ExpiresAt time.Time `json:"expires_at"`
IssuerURL string `json:"issuer_url"`
ClientID string `json:"client_id"`
// PortalURL is the portal the token was bound to at login or refresh. Empty
// means unbound; the provider rejects unbound tokens.
PortalURL string `json:"portal_url,omitempty"`
}
StoredToken holds the persisted OAuth token data.
func NewStoredToken ¶
func NewStoredToken(tok *oauth2.Token, issuerURL, clientID, portalURL string) *StoredToken
NewStoredToken creates a StoredToken from an oauth2.Token and metadata.