-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
48 lines (40 loc) · 1.13 KB
/
client.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
package buddysecrets
import (
"github.com/buddy/api-go-sdk/buddy"
"time"
)
const (
clientLifetime = 30 * time.Minute
)
type client struct {
apiClient *buddy.Client
expiration time.Time
}
func (c *client) Valid() bool {
return c != nil && time.Now().Before(c.expiration)
}
func (c *client) CreateToken(name string, expiresIn int, ipRestrictions []string, workspaceRestrictions []string, scopes []string) (*buddy.Token, error) {
ops := buddy.TokenOps{
Name: &name,
IpRestrictions: &ipRestrictions,
WorkspaceRestrictions: &workspaceRestrictions,
Scopes: &scopes,
ExpiresIn: &expiresIn,
}
token, _, err := c.apiClient.TokenService.Create(&ops)
if err != nil {
return nil, err
}
return token, nil
}
func (c *client) DeleteToken(tokenId string) error {
_, err := c.apiClient.TokenService.Delete(tokenId)
return err
}
func (c *client) GetRootToken() (*buddy.Token, error) {
token, _, err := c.apiClient.TokenService.GetMe()
return token, err
}
func NewApiClient(config *buddyConfig) (*buddy.Client, error) {
return buddy.NewClient(config.Token, config.BaseUrl, config.Insecure)
}