blob: e96da514c1e8d3a786d073fe970b4024dafc2f89 [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 Wilsona6b08312014-10-02 15:27:45 -04008 "github.com/rackspace/gophercloud"
Ash Wilson46d913f2014-08-29 11:00:11 -04009)
10
Ash Wilsonf8d546a2014-09-30 17:43:25 -040011// commonResult is the deferred result of a Create or a Get call.
12type commonResult struct {
13 gophercloud.CommonResult
Ash Wilson884b5cf2014-10-02 11:08:58 -040014
15 // 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 -040016 header http.Header
Ash Wilson4a52e2a2014-08-29 09:28:00 -040017}
Ash Wilsona1920082014-08-28 14:24:17 -040018
Ash Wilsonf8d546a2014-09-30 17:43:25 -040019// Extract interprets a commonResult as a Token.
20func (r commonResult) Extract() (*Token, error) {
21 if r.Err != nil {
22 return nil, r.Err
Ash Wilsone058e342014-08-29 10:31:41 -040023 }
24
Ash Wilsonf8d546a2014-09-30 17:43:25 -040025 var response struct {
26 Token struct {
27 ExpiresAt string `mapstructure:"expires_at"`
28 } `mapstructure:"token"`
Ash Wilsone058e342014-08-29 10:31:41 -040029 }
30
Ash Wilsonf8d546a2014-09-30 17:43:25 -040031 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 Wilsone058e342014-08-29 10:31:41 -040037 if err != nil {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040038 return nil, err
Ash Wilsone058e342014-08-29 10:31:41 -040039 }
40
41 // Attempt to parse the timestamp.
Ash Wilsonf25ae372014-10-06 14:40:29 -040042 token.ExpiresAt, err = time.Parse(gophercloud.RFC3339Milli, response.Token.ExpiresAt)
Ash Wilsone058e342014-08-29 10:31:41 -040043 if err != nil {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040044 return nil, err
Ash Wilsone058e342014-08-29 10:31:41 -040045 }
46
Ash Wilsonf8d546a2014-09-30 17:43:25 -040047 return &token, nil
48}
49
50// CreateResult is the deferred response from a Create call.
51type CreateResult struct {
52 commonResult
53}
54
55// createErr quickly creates a CreateResult that reports an error.
56func 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.
66type 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.
72type 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 Wilsone058e342014-08-29 10:31:41 -040078}