Skip to content

Commit dbd6cdb

Browse files
authored
(MAINT) Add the PE v1 /tokens endpoint (#90)
1 parent 3133343 commit dbd6cdb

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"lifetime": "1y",
3+
"description": "A token to be used with joy and care.",
4+
"client": "PE console"
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"token": "some token"
3+
}

pkg/rbac/token.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const (
66
requestAuthTokenURI = "/rbac-api/v1/auth/token" // #nosec - this is the uri to g et RBAC tokens
77
tokenAuthenticateURI = "/rbac-api/v2/auth/token/authenticate" // #nosec - this is the uri to authenticate RBAC tokens
88
tokenRevokeURI = "/rbac-api/v2/tokens/" // #nosec - this is the uri to revoke individual RBAC tokens
9+
tokenGenerateURI = "/rbac-api/v1/tokens" // #nosec - this is the uri to generate a token.
910
)
1011

1112
// GetRBACToken returns an auth token given user/password information
@@ -66,6 +67,27 @@ func (c *Client) RevokeRBACToken(token string) error {
6667
return nil
6768
}
6869

70+
// GenerateRBACToken returns an RBAC token or errors otherwise
71+
func (c *Client) GenerateRBACToken(token string, request TokenRequest) (string, error) {
72+
var payload Token
73+
74+
r, err := c.resty.R().
75+
SetHeader("X-Authentication", token).
76+
SetResult(&payload).
77+
SetBody(request).
78+
Post(tokenGenerateURI)
79+
if err != nil {
80+
return "", FormatError(r, err.Error())
81+
}
82+
if r.IsError() {
83+
if r.Error() != nil {
84+
return "", FormatError(r)
85+
}
86+
return "", FormatError(r)
87+
}
88+
return payload.Token, nil
89+
}
90+
6991
// Token is the returned auth token
7092
type Token struct {
7193
Token string `json:"token"`
@@ -95,3 +117,10 @@ type AuthenticateResponse struct {
95117
UserID string `json:"user_id"`
96118
DisplayName string `json:"display_name"`
97119
}
120+
121+
// TokenRequest will hold the details needed by the /tokens endpoint.
122+
type TokenRequest struct {
123+
Lifetime string `json:"lifetime,omitempty"`
124+
Description string `json:"description,omitempty"`
125+
Client string `json:"client,omitempty"`
126+
}

pkg/rbac/token_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,25 @@ func TestRevokeRBACToken(t *testing.T) {
6565
err = rbacClient.RevokeRBACToken(tokenValue)
6666
require.Equal(t, expectedError, err)
6767
}
68+
69+
func TestGenerateRBACToken(t *testing.T) {
70+
tokenValue := "some token"
71+
72+
tokenRequest := TokenRequest{
73+
Description: "A token to be used with joy and care.",
74+
Lifetime: "1y",
75+
Client: "PE console",
76+
}
77+
78+
// Test success
79+
setupPostResponder(t, tokenGenerateURI, "GenerateToken-request.json", "GenerateToken-response.json")
80+
81+
tokenResponse, err := rbacClient.GenerateRBACToken(tokenValue, tokenRequest)
82+
require.Nil(t, err)
83+
require.Equal(t, tokenValue, tokenResponse)
84+
85+
// Test error
86+
setUpBadRequestResponder(t, http.MethodPost, tokenGenerateURI)
87+
_, err = rbacClient.GenerateRBACToken(tokenValue, tokenRequest)
88+
require.Equal(t, expectedError, err)
89+
}

0 commit comments

Comments
 (0)