@@ -15,6 +15,7 @@ const (
1515 CredTypeOIDC = "oidc"
1616)
1717
18+ // Credentials holds the authentication material persisted to disk.
1819type Credentials struct {
1920 Type string `json:"type"`
2021 APIKey string `json:"api_key,omitempty"`
@@ -23,11 +24,15 @@ type Credentials struct {
2324 ExpiresAt time.Time `json:"expires_at,omitempty"`
2425}
2526
27+ // DefaultCredentialsPath returns ~/.mantle/credentials, the default location
28+ // where authentication material is stored.
2629func DefaultCredentialsPath () string {
2730 home , _ := os .UserHomeDir ()
2831 return filepath .Join (home , ".mantle" , "credentials" )
2932}
3033
34+ // SaveCredentials marshals cred to JSON and writes it to path with
35+ // 0600 permissions, creating parent directories as needed.
3136func SaveCredentials (path string , cred * Credentials ) error {
3237 if err := os .MkdirAll (filepath .Dir (path ), 0700 ); err != nil {
3338 return fmt .Errorf ("creating credentials directory: %w" , err )
@@ -42,6 +47,7 @@ func SaveCredentials(path string, cred *Credentials) error {
4247 return nil
4348}
4449
50+ // LoadCredentials reads and parses the JSON credentials file at path.
4551func LoadCredentials (path string ) (* Credentials , error ) {
4652 data , err := os .ReadFile (path )
4753 if err != nil {
@@ -54,6 +60,8 @@ func LoadCredentials(path string) (*Credentials, error) {
5460 return & cred , nil
5561}
5662
63+ // DeleteCredentials removes the credentials file at path. It is not an error
64+ // if the file does not exist.
5765func DeleteCredentials (path string ) error {
5866 if err := os .Remove (path ); err != nil && ! os .IsNotExist (err ) {
5967 return fmt .Errorf ("removing credentials: %w" , err )
0 commit comments