Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | // TokenCreateResult contains the document structure returned from a Create call. |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 4 | type TokenCreateResult struct { |
| 5 | response map[string]interface{} |
| 6 | tokenID string |
| 7 | } |
Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 8 | |
| 9 | // TokenID retrieves a token generated by a Create call from an token creation response. |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 10 | func (r *TokenCreateResult) TokenID() (string, error) { |
| 11 | return r.tokenID, nil |
Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 12 | } |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame^] | 13 | |
| 14 | // ExpiresAt retrieves the token expiration time. |
| 15 | func (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 | } |