Skip to content

Commit 8f101fd

Browse files
committed
create/update access token options
1 parent 9b0a502 commit 8f101fd

File tree

1 file changed

+48
-8
lines changed

1 file changed

+48
-8
lines changed

access_tokens.go

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@ import (
1111

1212
// AccessToken - auth tokens, can be created for the agents
1313
type AccessToken struct {
14-
ID string `json:"id"`
15-
CreatedAt time.Time `json:"created_at"`
16-
UpdatedAt time.Time `json:"updated_at"`
17-
LastLogin string `json:"last_login"`
18-
Description string `json:"description"`
19-
20-
Active bool `json:"active"`
14+
ID string `json:"id"` // read-only
15+
CreatedAt time.Time `json:"created_at"` // read-only
16+
UpdatedAt time.Time `json:"updated_at"` // read-only
17+
LastLogin string `json:"last_login"` // read-only
18+
Description string `json:"description"`
19+
Scopes AccessTokenScopes `json:"scopes"`
20+
// APIAccess allows to enable/disabled API access. Tokens that have disabled
21+
// access can be used to subscribe to webhooks or tunnel connections.
22+
// Defaults to "enabled"
23+
APIAccess AccessTokenAPIAccess `json:"api_access"`
24+
Active bool `json:"active"`
2125
}
2226

2327
// MarshalJSON helper to marshal unix time
@@ -58,8 +62,26 @@ type AccessTokenCreateResponse struct {
5862
Secret string `json:"secret"`
5963
}
6064

65+
// AccessTokenAPIAccess - enables/disables API access for the token
66+
type AccessTokenAPIAccess string
67+
68+
// Available API access token status
69+
const (
70+
AccessTokenAPIAccessEnabled AccessTokenAPIAccess = "enabled"
71+
AccessTokenAPIAccessDisabled AccessTokenAPIAccess = "disabled"
72+
)
73+
6174
// AccessTokenCreateOptions - used to create an access token
6275
type AccessTokenCreateOptions struct {
76+
Description string `json:"description"`
77+
Scopes AccessTokenScopes `json:"scopes"`
78+
APIAccess AccessTokenAPIAccess `json:"api_access"`
79+
}
80+
81+
// AccessTokenScopes define optional limits for tokens
82+
type AccessTokenScopes struct {
83+
Tunnels []string `json:"tunnels"`
84+
Buckets []string `json:"buckets"`
6385
}
6486

6587
// AccessTokenListOptions - TODO
@@ -109,6 +131,24 @@ func (api *API) DeleteAccessToken(options *AccessTokenDeleteOptions) error {
109131
return fmt.Errorf("invalid access token ID '%s'", options.ID)
110132
}
111133

112-
_, err := api.makeRequest("DELETE", "/buckets/"+options.ID, nil)
134+
_, err := api.makeRequest(http.MethodDelete, "/tokens/"+options.ID, nil)
113135
return err
114136
}
137+
138+
// UpdateAccessToken updates access token scopes, description and enabled/disable API access
139+
func (api *API) UpdateAccessToken(options *AccessToken) (*AccessToken, error) {
140+
if !IsUUID(options.ID) {
141+
return nil, fmt.Errorf("invalid access token ID '%s'", options.ID)
142+
}
143+
144+
resp, err := api.makeRequest(http.MethodPut, "/tokens/"+options.ID, options)
145+
if err != nil {
146+
return nil, err
147+
}
148+
149+
var result AccessToken
150+
if err := json.Unmarshal(resp, &result); err != nil {
151+
return nil, err
152+
}
153+
return &result, nil
154+
}

0 commit comments

Comments
 (0)