blob: 9f0d6233ccadab2911810c530a9c5eba98cc9bd5 [file] [log] [blame]
Ash Wilsona1920082014-08-28 14:24:17 -04001package tokens
2
3// TokenCreateResult contains the document structure returned from a Create call.
Ash Wilson4a52e2a2014-08-29 09:28:00 -04004type TokenCreateResult struct {
5 response map[string]interface{}
6 tokenID string
7}
Ash Wilsona1920082014-08-28 14:24:17 -04008
9// TokenID retrieves a token generated by a Create call from an token creation response.
Ash Wilson4a52e2a2014-08-29 09:28:00 -040010func (r *TokenCreateResult) TokenID() (string, error) {
11 return r.tokenID, nil
Ash Wilsona1920082014-08-28 14:24:17 -040012}
Ash Wilsone058e342014-08-29 10:31:41 -040013
14// ExpiresAt retrieves the token expiration time.
15func (r *TokenCreateResult) ExpiresAt() (time.Time, error) {
16 type tokenResp struct {
17 ExpiresAt string `mapstructure:"expires_at"`
18 }
19
20 type response struct {
21 Token tokenResp `mapstructure:"token"`
22 }
23
24 var resp response
25 err := mapstructure.Decode(r.response, &resp)
26 if err != nil {
27 return time.Time{}, err
28 }
29
30 // Attempt to parse the timestamp.
31 ts, err := time.Parse(RFC3339Milli, resp.Token.ExpiresAt)
32 if err != nil {
33 return time.Time{}, err
34 }
35
36 return ts, nil
37}