| 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 |  | 
| Jamie Hannaford | a253adf | 2014-10-08 17:14:24 +0200 | [diff] [blame^] | 44 | return &token, err | 
| Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 45 | } | 
|  | 46 |  | 
|  | 47 | // CreateResult is the deferred response from a Create call. | 
|  | 48 | type CreateResult struct { | 
|  | 49 | commonResult | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | // createErr quickly creates a CreateResult that reports an error. | 
|  | 53 | func createErr(err error) CreateResult { | 
|  | 54 | return CreateResult{ | 
|  | 55 | commonResult: commonResult{ | 
|  | 56 | CommonResult: gophercloud.CommonResult{Err: err}, | 
|  | 57 | header:       nil, | 
|  | 58 | }, | 
|  | 59 | } | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 | // GetResult is the deferred response from a Get call. | 
|  | 63 | type GetResult struct { | 
|  | 64 | commonResult | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | // Token is a string that grants a user access to a controlled set of services in an OpenStack provider. | 
|  | 68 | // Each Token is valid for a set length of time. | 
|  | 69 | type Token struct { | 
|  | 70 | // ID is the issued token. | 
|  | 71 | ID string | 
|  | 72 |  | 
|  | 73 | // ExpiresAt is the timestamp at which this token will no longer be accepted. | 
|  | 74 | ExpiresAt time.Time | 
| Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 75 | } |