|
6 | 6 | requestAuthTokenURI = "/rbac-api/v1/auth/token" // #nosec - this is the uri to g et RBAC tokens |
7 | 7 | tokenAuthenticateURI = "/rbac-api/v2/auth/token/authenticate" // #nosec - this is the uri to authenticate RBAC tokens |
8 | 8 | 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. |
9 | 10 | ) |
10 | 11 |
|
11 | 12 | // GetRBACToken returns an auth token given user/password information |
@@ -66,6 +67,27 @@ func (c *Client) RevokeRBACToken(token string) error { |
66 | 67 | return nil |
67 | 68 | } |
68 | 69 |
|
| 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 | + |
69 | 91 | // Token is the returned auth token |
70 | 92 | type Token struct { |
71 | 93 | Token string `json:"token"` |
@@ -95,3 +117,10 @@ type AuthenticateResponse struct { |
95 | 117 | UserID string `json:"user_id"` |
96 | 118 | DisplayName string `json:"display_name"` |
97 | 119 | } |
| 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 | +} |
0 commit comments