blob: d1fff4c2a5fde8740b0157a46369b37e26c83bff [file] [log] [blame]
Ash Wilsona1920082014-08-28 14:24:17 -04001package tokens
2
Ash Wilson46d913f2014-08-29 11:00:11 -04003import (
4 "time"
5
6 "github.com/mitchellh/mapstructure"
Ash Wilsona6b08312014-10-02 15:27:45 -04007 "github.com/rackspace/gophercloud"
Ash Wilson46d913f2014-08-29 11:00:11 -04008)
9
Ash Wilsonf8d546a2014-09-30 17:43:25 -040010// commonResult is the deferred result of a Create or a Get call.
11type commonResult struct {
Ash Wilsonf548aad2014-10-20 08:35:34 -040012 gophercloud.Result
Ash Wilson4a52e2a2014-08-29 09:28:00 -040013}
Ash Wilsona1920082014-08-28 14:24:17 -040014
Ash Wilsonf8d546a2014-09-30 17:43:25 -040015// Extract interprets a commonResult as a Token.
16func (r commonResult) Extract() (*Token, error) {
17 if r.Err != nil {
18 return nil, r.Err
Ash Wilsone058e342014-08-29 10:31:41 -040019 }
20
Ash Wilsonf8d546a2014-09-30 17:43:25 -040021 var response struct {
22 Token struct {
23 ExpiresAt string `mapstructure:"expires_at"`
24 } `mapstructure:"token"`
Ash Wilsone058e342014-08-29 10:31:41 -040025 }
26
Ash Wilsonf8d546a2014-09-30 17:43:25 -040027 var token Token
28
29 // Parse the token itself from the stored headers.
Ash Wilson72e4d2c2014-10-20 10:27:30 -040030 token.ID = r.Header.Get("X-Subject-Token")
Ash Wilsonf8d546a2014-09-30 17:43:25 -040031
Ash Wilsond3dc2542014-10-20 10:10:48 -040032 err := mapstructure.Decode(r.Body, &response)
Ash Wilsone058e342014-08-29 10:31:41 -040033 if err != nil {
Ash Wilsonf8d546a2014-09-30 17:43:25 -040034 return nil, err
Ash Wilsone058e342014-08-29 10:31:41 -040035 }
36
37 // Attempt to parse the timestamp.
Ash Wilsonf25ae372014-10-06 14:40:29 -040038 token.ExpiresAt, err = time.Parse(gophercloud.RFC3339Milli, response.Token.ExpiresAt)
Ash Wilsone058e342014-08-29 10:31:41 -040039
Jamie Hannaforda253adf2014-10-08 17:14:24 +020040 return &token, err
Ash Wilsonf8d546a2014-09-30 17:43:25 -040041}
42
43// CreateResult is the deferred response from a Create call.
44type CreateResult struct {
45 commonResult
46}
47
48// createErr quickly creates a CreateResult that reports an error.
49func createErr(err error) CreateResult {
50 return CreateResult{
Ash Wilsonf548aad2014-10-20 08:35:34 -040051 commonResult: commonResult{Result: gophercloud.Result{Err: err}},
Ash Wilsonf8d546a2014-09-30 17:43:25 -040052 }
53}
54
55// GetResult is the deferred response from a Get call.
56type GetResult struct {
57 commonResult
58}
59
Jamie Hannafordf38dd2e2014-10-27 11:36:54 +010060// RevokeResult is the deferred response from a Revoke call.
61type RevokeResult struct {
62 commonResult
63}
64
Ash Wilsonf8d546a2014-09-30 17:43:25 -040065// Token is a string that grants a user access to a controlled set of services in an OpenStack provider.
66// Each Token is valid for a set length of time.
67type Token struct {
68 // ID is the issued token.
69 ID string
70
71 // ExpiresAt is the timestamp at which this token will no longer be accepted.
72 ExpiresAt time.Time
Ash Wilsone058e342014-08-29 10:31:41 -040073}