2015-04-05 00:53:59 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
// TokenAuth is used to perform token backend operations on Vault.
|
|
|
|
type TokenAuth struct {
|
|
|
|
c *Client
|
|
|
|
}
|
|
|
|
|
2015-09-29 07:35:16 +00:00
|
|
|
// Token is used to return the client for logical-backend API calls.
|
2015-04-05 00:54:16 +00:00
|
|
|
func (a *Auth) Token() *TokenAuth {
|
2015-04-05 00:53:59 +00:00
|
|
|
return &TokenAuth{c: a.c}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) Create(opts *TokenCreateRequest) (*Secret, error) {
|
|
|
|
r := c.c.NewRequest("POST", "/v1/auth/token/create")
|
|
|
|
if err := r.SetJSONBody(opts); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := c.c.RawRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return ParseSecret(resp.Body)
|
|
|
|
}
|
|
|
|
|
2015-04-20 01:04:01 +00:00
|
|
|
func (c *TokenAuth) Renew(token string, increment int) (*Secret, error) {
|
2015-04-05 00:53:59 +00:00
|
|
|
r := c.c.NewRequest("PUT", "/v1/auth/token/renew/"+token)
|
2015-04-20 01:04:01 +00:00
|
|
|
|
|
|
|
body := map[string]interface{}{"increment": increment}
|
|
|
|
if err := r.SetJSONBody(body); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-05 00:53:59 +00:00
|
|
|
resp, err := c.c.RawRequest(r)
|
|
|
|
if err != nil {
|
2015-04-20 01:04:01 +00:00
|
|
|
return nil, err
|
2015-04-05 00:53:59 +00:00
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2015-04-20 01:04:01 +00:00
|
|
|
return ParseSecret(resp.Body)
|
2015-04-05 00:53:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) RevokeOrphan(token string) error {
|
|
|
|
r := c.c.NewRequest("PUT", "/v1/auth/token/revoke-orphan/"+token)
|
|
|
|
resp, err := c.c.RawRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) RevokePrefix(token string) error {
|
|
|
|
r := c.c.NewRequest("PUT", "/v1/auth/token/revoke-prefix/"+token)
|
|
|
|
resp, err := c.c.RawRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *TokenAuth) RevokeTree(token string) error {
|
|
|
|
r := c.c.NewRequest("PUT", "/v1/auth/token/revoke/"+token)
|
|
|
|
resp, err := c.c.RawRequest(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TokenCreateRequest is the options structure for creating a token.
|
|
|
|
type TokenCreateRequest struct {
|
2015-04-20 01:08:08 +00:00
|
|
|
ID string `json:"id,omitempty"`
|
|
|
|
Policies []string `json:"policies,omitempty"`
|
|
|
|
Metadata map[string]string `json:"meta,omitempty"`
|
|
|
|
Lease string `json:"lease,omitempty"`
|
|
|
|
NoParent bool `json:"no_parent,omitempty"`
|
|
|
|
DisplayName string `json:"display_name"`
|
|
|
|
NumUses int `json:"num_uses"`
|
2015-04-05 00:53:59 +00:00
|
|
|
}
|