blob: 8e0f018b378f679acb262190c41e9bc8a556cde9 [file] [log] [blame]
Ash Wilsona1920082014-08-28 14:24:17 -04001package tokens
2
Ash Wilson46d913f2014-08-29 11:00:11 -04003import (
4 "time"
5
6 "github.com/mitchellh/mapstructure"
7)
8
9// RFC3339Milli describes the time format used by identity API responses.
10const RFC3339Milli = "2006-01-02T15:04:05.999999Z"
11
Ash Wilsona1920082014-08-28 14:24:17 -040012// TokenCreateResult contains the document structure returned from a Create call.
Ash Wilson4a52e2a2014-08-29 09:28:00 -040013type TokenCreateResult struct {
14 response map[string]interface{}
15 tokenID string
16}
Ash Wilsona1920082014-08-28 14:24:17 -040017
18// TokenID retrieves a token generated by a Create call from an token creation response.
Ash Wilson4a52e2a2014-08-29 09:28:00 -040019func (r *TokenCreateResult) TokenID() (string, error) {
20 return r.tokenID, nil
Ash Wilsona1920082014-08-28 14:24:17 -040021}
Ash Wilsone058e342014-08-29 10:31:41 -040022
23// ExpiresAt retrieves the token expiration time.
24func (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}