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 ( |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame^] | 4 | "net/http" |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 5 | "time" |
| 6 | |
| 7 | "github.com/mitchellh/mapstructure" |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame^] | 8 | "github.com/rackspace/gophercloud" |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 9 | ) |
| 10 | |
| 11 | // RFC3339Milli describes the time format used by identity API responses. |
| 12 | const RFC3339Milli = "2006-01-02T15:04:05.999999Z" |
| 13 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame^] | 14 | // commonResult is the deferred result of a Create or a Get call. |
| 15 | type commonResult struct { |
| 16 | gophercloud.CommonResult |
| 17 | header http.Header |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 18 | } |
Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 19 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame^] | 20 | // Extract interprets a commonResult as a Token. |
| 21 | func (r commonResult) Extract() (*Token, error) { |
| 22 | if r.Err != nil { |
| 23 | return nil, r.Err |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 24 | } |
| 25 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame^] | 26 | var response struct { |
| 27 | Token struct { |
| 28 | ExpiresAt string `mapstructure:"expires_at"` |
| 29 | } `mapstructure:"token"` |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 30 | } |
| 31 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame^] | 32 | var token Token |
| 33 | |
| 34 | // Parse the token itself from the stored headers. |
| 35 | token.ID = r.header.Get("X-Subject-Token") |
| 36 | |
| 37 | err := mapstructure.Decode(r.Resp, &response) |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 38 | if err != nil { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame^] | 39 | return nil, err |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | // Attempt to parse the timestamp. |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame^] | 43 | token.ExpiresAt, err = time.Parse(RFC3339Milli, response.Token.ExpiresAt) |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 44 | if err != nil { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame^] | 45 | return nil, err |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 46 | } |
| 47 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame^] | 48 | return &token, nil |
| 49 | } |
| 50 | |
| 51 | // CreateResult is the deferred response from a Create call. |
| 52 | type CreateResult struct { |
| 53 | commonResult |
| 54 | } |
| 55 | |
| 56 | // createErr quickly creates a CreateResult that reports an error. |
| 57 | func createErr(err error) CreateResult { |
| 58 | return CreateResult{ |
| 59 | commonResult: commonResult{ |
| 60 | CommonResult: gophercloud.CommonResult{Err: err}, |
| 61 | header: nil, |
| 62 | }, |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // GetResult is the deferred response from a Get call. |
| 67 | type GetResult struct { |
| 68 | commonResult |
| 69 | } |
| 70 | |
| 71 | // Token is a string that grants a user access to a controlled set of services in an OpenStack provider. |
| 72 | // Each Token is valid for a set length of time. |
| 73 | type Token struct { |
| 74 | // ID is the issued token. |
| 75 | ID string |
| 76 | |
| 77 | // ExpiresAt is the timestamp at which this token will no longer be accepted. |
| 78 | ExpiresAt time.Time |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 79 | } |