blob: c970c6792e385448afe757cc1215c39f8fb128b8 [file] [log] [blame]
Ash Wilsona1920082014-08-28 14:24:17 -04001package tokens
2
Ash Wilson46d913f2014-08-29 11:00:11 -04003import (
Ash Wilsonf8d546a2014-09-30 17:43:25 -04004 "net/http"
Ash Wilson46d913f2014-08-29 11:00:11 -04005 "time"
6
7 "github.com/mitchellh/mapstructure"
Ash Wilsonf8d546a2014-09-30 17:43:25 -04008 "github.com/rackspace/gophercloud"
Ash Wilson46d913f2014-08-29 11:00:11 -04009)
10
11// RFC3339Milli describes the time format used by identity API responses.
12const RFC3339Milli = "2006-01-02T15:04:05.999999Z"
13
Ash Wilsonf8d546a2014-09-30 17:43:25 -040014// commonResult is the deferred result of a Create or a Get call.
15type commonResult struct {
16 gophercloud.CommonResult
Ash Wilson884b5cf2014-10-02 11:08:58 -040017
18 // header stores the headers from the original HTTP response because token responses are returned in an X-Subject-Token header.
Ash Wilsonf8d546a2014-09-30 17:43:25 -040019 header http.Header
Ash Wilson4a52e2a2014-08-29 09:28:00 -040020}
Ash Wilsona1920082014-08-28 14:24:17 -040021
Ash Wilsonf8d546a2014-09-30 17:43:25 -040022// Extract interprets a commonResult as a Token.
23func (r commonResult) Extract() (*Token, error) {
24 if r.Err != nil {
25 return nil, r.Err
Ash Wilsone058e342014-08-29 10:31:41 -040026 }
27
Ash Wilsonf8d546a2014-09-30 17:43:25 -040028 var response struct {
29 Token struct {
30 ExpiresAt string `mapstructure:"expires_at"`
31 } `mapstructure:"token"`
Ash Wilsone058e342014-08-29 10:31:41 -040032 }
33
Ash Wilsonf8d546a2014-09-30 17:43:25 -040034 var token Token
35
36 // Parse the token itself from the stored headers.
37 token.ID = r.header.Get("X-Subject-Token")
38
39 err := mapstructure.Decode(r.Resp, &response)
Ash Wilsone058e342014-08-29 10:31:41 -040040 if err != nil {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040041 return nil, err
Ash Wilsone058e342014-08-29 10:31:41 -040042 }
43
44 // Attempt to parse the timestamp.
Ash Wilsonf8d546a2014-09-30 17:43:25 -040045 token.ExpiresAt, err = time.Parse(RFC3339Milli, response.Token.ExpiresAt)
Ash Wilsone058e342014-08-29 10:31:41 -040046 if err != nil {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040047 return nil, err
Ash Wilsone058e342014-08-29 10:31:41 -040048 }
49
Ash Wilsonf8d546a2014-09-30 17:43:25 -040050 return &token, nil
51}
52
53// CreateResult is the deferred response from a Create call.
54type CreateResult struct {
55 commonResult
56}
57
58// createErr quickly creates a CreateResult that reports an error.
59func createErr(err error) CreateResult {
60 return CreateResult{
61 commonResult: commonResult{
62 CommonResult: gophercloud.CommonResult{Err: err},
63 header: nil,
64 },
65 }
66}
67
68// GetResult is the deferred response from a Get call.
69type GetResult struct {
70 commonResult
71}
72
73// Token is a string that grants a user access to a controlled set of services in an OpenStack provider.
74// Each Token is valid for a set length of time.
75type Token struct {
76 // ID is the issued token.
77 ID string
78
79 // ExpiresAt is the timestamp at which this token will no longer be accepted.
80 ExpiresAt time.Time
Ash Wilsone058e342014-08-29 10:31:41 -040081}