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