Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame^] | 3 | import ( |
| 4 | "time" |
| 5 | |
| 6 | "github.com/mitchellh/mapstructure" |
| 7 | ) |
| 8 | |
| 9 | // RFC3339Milli describes the time format used by identity API responses. |
| 10 | const RFC3339Milli = "2006-01-02T15:04:05.999999Z" |
| 11 | |
Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 12 | // TokenCreateResult contains the document structure returned from a Create call. |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 13 | type TokenCreateResult struct { |
| 14 | response map[string]interface{} |
| 15 | tokenID string |
| 16 | } |
Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 17 | |
| 18 | // 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] | 19 | func (r *TokenCreateResult) TokenID() (string, error) { |
| 20 | return r.tokenID, nil |
Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 21 | } |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 22 | |
| 23 | // ExpiresAt retrieves the token expiration time. |
| 24 | func (r *TokenCreateResult) ExpiresAt() (time.Time, error) { |
| 25 | type tokenResp struct { |
| 26 | ExpiresAt string `mapstructure:"expires_at"` |
| 27 | } |
| 28 | |
| 29 | type response struct { |
| 30 | Token tokenResp `mapstructure:"token"` |
| 31 | } |
| 32 | |
| 33 | var resp response |
| 34 | err := mapstructure.Decode(r.response, &resp) |
| 35 | if err != nil { |
| 36 | return time.Time{}, err |
| 37 | } |
| 38 | |
| 39 | // Attempt to parse the timestamp. |
| 40 | ts, err := time.Parse(RFC3339Milli, resp.Token.ExpiresAt) |
| 41 | if err != nil { |
| 42 | return time.Time{}, err |
| 43 | } |
| 44 | |
| 45 | return ts, nil |
| 46 | } |