A secure and flexible keystore implementation for managing authentication tokens and cryptographic keys in Go applications.
- Secure storage of authentication tokens with expiration
- ECDSA private key management
- Configurable storage location
- Thread-safe operations
- Comprehensive error handling
- Full test coverage
go get github.com/theblitlabs/go-keystore
package main
import (
"fmt"
"github.com/theblitlabs/go-keystore"
)
func main() {
// Create a new keystore with default configuration
ks, err := keystore.NewKeystore(keystore.Config{})
if err != nil {
panic(err)
}
// Save an authentication token
err = ks.SaveToken("your-auth-token")
if err != nil {
panic(err)
}
// Load the token
token, err := ks.LoadToken()
if err != nil {
panic(err)
}
fmt.Printf("Loaded token: %s\n", token)
}
ks, err := keystore.NewKeystore(keystore.Config{
DirPath: "/custom/path/to/keystore",
FileName: "custom-keystore.json",
})
// Save a private key
err = ks.SavePrivateKey("your-private-key-hex")
if err != nil {
panic(err)
}
// Load the private key
privateKey, err := ks.LoadPrivateKey()
if err != nil {
panic(err)
}
The package provides specific error types for common scenarios:
ErrEmptyToken
: Returned when attempting to save an empty tokenErrNoKeystore
: Returned when no keystore file existsErrTokenExpired
: Returned when the stored token has expiredErrInvalidToken
: Returned when the stored token is invalidErrNoPrivateKey
: Returned when no private key exists in the keystore
- Files are stored with 0600 permissions (user read/write only)
- Directories are created with 0700 permissions
- Tokens automatically expire after 1 hour (configurable)
- Private keys are validated before storage
Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - see LICENSE file for details