blob: fad369ee65adad452d3d98f3d44566ddbe1115ab [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
17 header http.Header
Ash Wilson4a52e2a2014-08-29 09:28:00 -040018}
Ash Wilsona1920082014-08-28 14:24:17 -040019
Ash Wilsonf8d546a2014-09-30 17:43:25 -040020// Extract interprets a commonResult as a Token.
21func (r commonResult) Extract() (*Token, error) {
22 if r.Err != nil {
23 return nil, r.Err
Ash Wilsone058e342014-08-29 10:31:41 -040024 }
25
Ash Wilsonf8d546a2014-09-30 17:43:25 -040026 var response struct {
27 Token struct {
28 ExpiresAt string `mapstructure:"expires_at"`
29 } `mapstructure:"token"`
Ash Wilsone058e342014-08-29 10:31:41 -040030 }
31
Ash Wilsonf8d546a2014-09-30 17:43:25 -040032 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 Wilsone058e342014-08-29 10:31:41 -040038 if err != nil {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040039 return nil, err
Ash Wilsone058e342014-08-29 10:31:41 -040040 }
41
42 // Attempt to parse the timestamp.
Ash Wilsonf8d546a2014-09-30 17:43:25 -040043 token.ExpiresAt, err = time.Parse(RFC3339Milli, response.Token.ExpiresAt)
Ash Wilsone058e342014-08-29 10:31:41 -040044 if err != nil {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040045 return nil, err
Ash Wilsone058e342014-08-29 10:31:41 -040046 }
47
Ash Wilsonf8d546a2014-09-30 17:43:25 -040048 return &token, nil
49}
50
51// CreateResult is the deferred response from a Create call.
52type CreateResult struct {
53 commonResult
54}
55
56// createErr quickly creates a CreateResult that reports an error.
57func 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.
67type 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.
73type 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 Wilsone058e342014-08-29 10:31:41 -040079}