Skip to content

Commit c5ba452

Browse files
authored
(MAINT) Add token authenticate endpoint (#87)
1 parent be082e2 commit c5ba452

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"token": "blah",
3+
"update_last_activity?":false
4+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"description": "abc",
3+
"role_ids": [1,2,3],
4+
"user_id": "abc"
5+
}

pkg/rbac/token.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package rbac
22

33
const (
4-
requestAuthTokenURI = "/rbac-api/v1/auth/token" // #nosec - this is the uri to get RBAC tokens
4+
requestAuthTokenURI = "/rbac-api/v1/auth/token" // #nosec - this is the uri to g et RBAC tokens
5+
tokenAuthenticateURI = "/rbac-api/v2/auth/token/authenticate" // #nosec - this is the uri to authenticate RBAC tokens
56
)
67

78
// GetRBACToken returns an auth token given user/password information
@@ -23,6 +24,27 @@ func (c *Client) GetRBACToken(authRequest *RequestKeys) (*Token, error) {
2324
return &payload, nil
2425
}
2526

27+
// AuthenticateRBACToken returns a response with the token details or errors otherwise.
28+
func (c *Client) AuthenticateRBACToken(token string) (*AuthenticateResponse, error) {
29+
authenticateRequest := &AuthenticateRequest{Token: token}
30+
31+
payload := AuthenticateResponse{}
32+
r, err := c.resty.R().
33+
SetResult(&payload).
34+
SetBody(authenticateRequest).
35+
Post(tokenAuthenticateURI)
36+
if err != nil {
37+
return nil, FormatError(r, err.Error())
38+
}
39+
if r.IsError() {
40+
if r.Error() != nil {
41+
return nil, FormatError(r)
42+
}
43+
return nil, FormatError(r)
44+
}
45+
return &payload, nil
46+
}
47+
2648
// Token is the returned auth token
2749
type Token struct {
2850
Token string `json:"token"`
@@ -37,3 +59,18 @@ type RequestKeys struct {
3759
Client string `json:"client,omitempty"`
3860
Label string `json:"label,omitempty"`
3961
}
62+
63+
// AuthenticateRequest will hold the request needed for an authenticate.
64+
type AuthenticateRequest struct {
65+
Token string `json:"token"`
66+
UpdateLastActivity bool `json:"update_last_activity?"`
67+
}
68+
69+
// AuthenticateResponse will hold the response from an authenticate.
70+
type AuthenticateResponse struct {
71+
Description string `json:"description"`
72+
Login string `json:"login"`
73+
RoleIDs []int `json:"role_ids"`
74+
UserID string `json:"user_id"`
75+
DisplayName string `json:"display_name"`
76+
}

pkg/rbac/token_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,24 @@ func TestGetRBACToken(t *testing.T) {
2828
require.Nil(t, actual)
2929
require.Equal(t, expectedError, err)
3030
}
31+
32+
func TestAuthenticateRBACToken(t *testing.T) {
33+
// Test success
34+
setupPostResponder(t, tokenAuthenticateURI, "AuthenticateRBACToken-request.json",
35+
"AuthenticateRBACToken-response.json")
36+
37+
expectedResponse := &AuthenticateResponse{
38+
UserID: "abc",
39+
Description: "abc",
40+
RoleIDs: []int{1, 2, 3},
41+
}
42+
actual, err := rbacClient.AuthenticateRBACToken("blah")
43+
require.Nil(t, err)
44+
require.Equal(t, expectedResponse, actual)
45+
46+
// Test error
47+
setUpBadRequestResponder(t, http.MethodPost, tokenAuthenticateURI)
48+
actual, err = rbacClient.AuthenticateRBACToken("blah")
49+
require.Nil(t, actual)
50+
require.Equal(t, expectedError, err)
51+
}

0 commit comments

Comments
 (0)