/
storage.go
39 lines (31 loc) · 864 Bytes
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package account
import (
"context"
"fmt"
"github.com/hashicorp/vault/sdk/logical"
"github.com/nats-io/jwt/v2"
)
func storagePath(name string) string {
return "accounts/" + name
}
type Account struct {
Name string `json:"name"`
Nkey string `json:"nkey"`
Revocations jwt.RevocationList `json:"revocations,omitempty"`
DefaultTtl int `json:"default_ttl,omitempty"`
MaxTtl int `json:"max_ttl,omitempty"`
}
func getAccount(ctx context.Context, s logical.Storage, name string) (*Account, error) {
entry, err := s.Get(ctx, storagePath(name))
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
config := new(Account)
if err := entry.DecodeJSON(&config); err != nil {
return nil, fmt.Errorf("error reading account: %w", err)
}
return config, nil
}