This repository has been archived by the owner on Feb 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
account.go
74 lines (60 loc) · 1.97 KB
/
account.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package api
import (
"time"
"strings"
"github.com/secrethub/secrethub-go/internals/api/uuid"
)
// Errors
var (
ErrInvalidAccountName = errAPI.Code("invalid_account_name").Error("An account name either needs to be an username or a servicename")
ErrInvalidKeyID = errAPI.Code("invalid_key_id").Error("id of the provided account key is invalid")
ServiceNamePrefix = "s-"
)
// Account represents an account on SecretHub.
type Account struct {
AccountID uuid.UUID `json:"account_id"`
Name AccountName `json:"name"`
PublicKey []byte `json:"public_key"`
AccountType string `json:"account_type"`
CreatedAt time.Time `json:"created_at"`
}
// AccountName represents the name of either a user or a service.
type AccountName string
// NewAccountName validates an account's name and returns it as a typed AccountName when valid.
func NewAccountName(name string) (AccountName, error) {
err := ValidateAccountName(name)
if err != nil {
return "", err
}
return AccountName(name), err
}
// IsService returns true if the AccountName contains the name of a service.
func (n AccountName) IsService() bool {
return strings.HasPrefix(strings.ToLower(string(n)), ServiceNamePrefix)
}
// IsUser returns true if the AccountName contains the name of a user.
func (n AccountName) IsUser() bool {
return !n.IsService()
}
// Validate checks whether an AccountName is valid.
func (n AccountName) Validate() error {
return ValidateAccountName(string(n))
}
// Set sets the AccountName to the value.
func (n *AccountName) Set(value string) error {
accountName, err := NewAccountName(value)
if err != nil {
return err
}
*n = accountName
return nil
}
// String returns the account's name as a string to be used for printing.
func (n AccountName) String() string {
return string(n)
}
// Value returns the account's name as a string to be used in communication
// with the client and in transportation to the server.
func (n AccountName) Value() string {
return string(n)
}