Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 3 | import ( |
| 4 | "time" |
| 5 | |
| 6 | "github.com/mitchellh/mapstructure" |
Ash Wilson | a6b0831 | 2014-10-02 15:27:45 -0400 | [diff] [blame] | 7 | "github.com/rackspace/gophercloud" |
Ash Wilson | 46d913f | 2014-08-29 11:00:11 -0400 | [diff] [blame] | 8 | ) |
| 9 | |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 10 | // Endpoint represents a single API endpoint offered by a service. |
| 11 | // It matches either a public, internal or admin URL. |
| 12 | // If supported, it contains a region specifier, again if provided. |
| 13 | // The significance of the Region field will depend upon your provider. |
| 14 | type Endpoint struct { |
| 15 | ID string `mapstructure:"id"` |
| 16 | Region string `mapstructure:"region"` |
| 17 | Interface string `mapstructure:"interface"` |
| 18 | URL string `mapstructure:"url"` |
| 19 | } |
| 20 | |
| 21 | // CatalogEntry provides a type-safe interface to an Identity API V3 service catalog listing. |
| 22 | // Each class of service, such as cloud DNS or block storage services, could have multiple |
| 23 | // CatalogEntry representing it (one by interface type, e.g public, admin or internal). |
| 24 | // |
| 25 | // Note: when looking for the desired service, try, whenever possible, to key off the type field. |
| 26 | // Otherwise, you'll tie the representation of the service to a specific provider. |
| 27 | type CatalogEntry struct { |
| 28 | |
| 29 | // Service ID |
| 30 | ID string `mapstructure:"id"` |
| 31 | |
| 32 | // Name will contain the provider-specified name for the service. |
| 33 | Name string `mapstructure:"name"` |
| 34 | |
| 35 | // Type will contain a type string if OpenStack defines a type for the service. |
| 36 | // Otherwise, for provider-specific services, the provider may assign their own type strings. |
| 37 | Type string `mapstructure:"type"` |
| 38 | |
| 39 | // Endpoints will let the caller iterate over all the different endpoints that may exist for |
| 40 | // the service. |
| 41 | Endpoints []Endpoint `mapstructure:"endpoints"` |
| 42 | } |
| 43 | |
| 44 | // ServiceCatalog provides a view into the service catalog from a previous, successful authentication. |
| 45 | type ServiceCatalog struct { |
| 46 | Entries []CatalogEntry |
| 47 | } |
| 48 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 49 | // commonResult is the deferred result of a Create or a Get call. |
| 50 | type commonResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 51 | gophercloud.Result |
Ash Wilson | 4a52e2a | 2014-08-29 09:28:00 -0400 | [diff] [blame] | 52 | } |
Ash Wilson | a192008 | 2014-08-28 14:24:17 -0400 | [diff] [blame] | 53 | |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 54 | // Extract is a shortcut for ExtractToken. |
| 55 | // This function is deprecated and still present for backward compatibility. |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 56 | func (r commonResult) Extract() (*Token, error) { |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 57 | return r.ExtractToken() |
| 58 | } |
| 59 | |
| 60 | // ExtractToken interprets a commonResult as a Token. |
| 61 | func (r commonResult) ExtractToken() (*Token, error) { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 62 | if r.Err != nil { |
| 63 | return nil, r.Err |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 64 | } |
| 65 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 66 | var response struct { |
| 67 | Token struct { |
| 68 | ExpiresAt string `mapstructure:"expires_at"` |
| 69 | } `mapstructure:"token"` |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 72 | var token Token |
| 73 | |
| 74 | // Parse the token itself from the stored headers. |
Ash Wilson | 72e4d2c | 2014-10-20 10:27:30 -0400 | [diff] [blame] | 75 | token.ID = r.Header.Get("X-Subject-Token") |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 76 | |
Ash Wilson | d3dc254 | 2014-10-20 10:10:48 -0400 | [diff] [blame] | 77 | err := mapstructure.Decode(r.Body, &response) |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 78 | if err != nil { |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 79 | return nil, err |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | // Attempt to parse the timestamp. |
Ash Wilson | f25ae37 | 2014-10-06 14:40:29 -0400 | [diff] [blame] | 83 | token.ExpiresAt, err = time.Parse(gophercloud.RFC3339Milli, response.Token.ExpiresAt) |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 84 | |
Jamie Hannaford | a253adf | 2014-10-08 17:14:24 +0200 | [diff] [blame] | 85 | return &token, err |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 86 | } |
| 87 | |
Guillaume Giamarchi | c043a3d | 2015-04-01 01:19:55 +0200 | [diff] [blame] | 88 | // ExtractServiceCatalog returns the ServiceCatalog that was generated along with the user's Token. |
| 89 | func (result CreateResult) ExtractServiceCatalog() (*ServiceCatalog, error) { |
| 90 | if result.Err != nil { |
| 91 | return nil, result.Err |
| 92 | } |
| 93 | |
| 94 | var response struct { |
| 95 | Token struct { |
| 96 | Entries []CatalogEntry `mapstructure:"catalog"` |
| 97 | } `mapstructure:"token"` |
| 98 | } |
| 99 | |
| 100 | err := mapstructure.Decode(result.Body, &response) |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | |
| 105 | return &ServiceCatalog{Entries: response.Token.Entries}, nil |
| 106 | } |
| 107 | |
| 108 | // CreateResult defers the interpretation of a created token. |
| 109 | // Use ExtractToken() to interpret it as a Token, or ExtractServiceCatalog() to interpret it as a service catalog. |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 110 | type CreateResult struct { |
| 111 | commonResult |
| 112 | } |
| 113 | |
| 114 | // createErr quickly creates a CreateResult that reports an error. |
| 115 | func createErr(err error) CreateResult { |
| 116 | return CreateResult{ |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 117 | commonResult: commonResult{Result: gophercloud.Result{Err: err}}, |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | // GetResult is the deferred response from a Get call. |
| 122 | type GetResult struct { |
| 123 | commonResult |
| 124 | } |
| 125 | |
Jamie Hannaford | f38dd2e | 2014-10-27 11:36:54 +0100 | [diff] [blame] | 126 | // RevokeResult is the deferred response from a Revoke call. |
| 127 | type RevokeResult struct { |
| 128 | commonResult |
| 129 | } |
| 130 | |
Ash Wilson | f8d546a | 2014-09-30 17:43:25 -0400 | [diff] [blame] | 131 | // Token is a string that grants a user access to a controlled set of services in an OpenStack provider. |
| 132 | // Each Token is valid for a set length of time. |
| 133 | type Token struct { |
| 134 | // ID is the issued token. |
| 135 | ID string |
| 136 | |
| 137 | // ExpiresAt is the timestamp at which this token will no longer be accepted. |
| 138 | ExpiresAt time.Time |
Ash Wilson | e058e34 | 2014-08-29 10:31:41 -0400 | [diff] [blame] | 139 | } |