Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | |
| 6 | "github.com/mitchellh/mapstructure" |
| 7 | "github.com/rackspace/gophercloud" |
| 8 | "github.com/rackspace/gophercloud/openstack/identity/v2/tenants" |
| 9 | ) |
| 10 | |
| 11 | // Token provides only the most basic information related to an authentication token. |
| 12 | type Token struct { |
| 13 | // ID provides the primary means of identifying a user to the OpenStack API. |
| 14 | // OpenStack defines this field as an opaque value, so do not depend on its content. |
| 15 | // It is safe, however, to compare for equality. |
| 16 | ID string |
| 17 | |
| 18 | // ExpiresAt provides a timestamp in ISO 8601 format, indicating when the authentication token becomes invalid. |
| 19 | // After this point in time, future API requests made using this authentication token will respond with errors. |
| 20 | // Either the caller will need to reauthenticate manually, or more preferably, the caller should exploit automatic re-authentication. |
| 21 | // See the AuthOptions structure for more details. |
| 22 | ExpiresAt time.Time |
| 23 | |
| 24 | // Tenant provides information about the tenant to which this token grants access. |
| 25 | Tenant tenants.Tenant |
| 26 | } |
| 27 | |
| 28 | // CreateResult defers the interpretation of a created token. |
| 29 | // Use ExtractToken() to interpret it as a Token, or ExtractServiceCatalog() to interpret it as a service catalog. |
| 30 | type CreateResult struct { |
| 31 | gophercloud.CommonResult |
| 32 | } |
| 33 | |
| 34 | // ExtractToken returns the just-created Token from a CreateResult. |
| 35 | func (result CreateResult) ExtractToken() (*Token, error) { |
| 36 | var response struct { |
| 37 | Access struct { |
| 38 | Token struct { |
| 39 | Expires string `mapstructure:"expires"` |
| 40 | ID string `mapstructure:"id"` |
| 41 | Tenant tenants.Tenant `mapstructure:"tenant"` |
| 42 | } `mapstructure:"token"` |
| 43 | } `mapstructure:"access"` |
| 44 | } |
| 45 | |
| 46 | err := mapstructure.Decode(result.Resp, &response) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | |
| 51 | expiresTs, err := time.Parse(gophercloud.RFC3339Milli, response.Access.Token.Expires) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | |
| 56 | return &Token{ |
| 57 | ID: response.Access.Token.ID, |
| 58 | ExpiresAt: expiresTs, |
| 59 | Tenant: response.Access.Token.Tenant, |
| 60 | }, nil |
| 61 | } |
| 62 | |
| 63 | // createErr quickly packs an error in a CreateResult. |
| 64 | func createErr(err error) CreateResult { |
| 65 | return CreateResult{gophercloud.CommonResult{Err: err}} |
| 66 | } |