Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 1 | package tokens |
| 2 | |
| 3 | import ( |
| 4 | "time" |
| 5 | |
Jon Perritt | 27249f4 | 2016-02-18 10:35:59 -0600 | [diff] [blame] | 6 | "github.com/gophercloud/gophercloud" |
| 7 | "github.com/gophercloud/gophercloud/openstack/identity/v2/tenants" |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 8 | ) |
| 9 | |
| 10 | // Token provides only the most basic information related to an authentication token. |
| 11 | type Token struct { |
| 12 | // ID provides the primary means of identifying a user to the OpenStack API. |
| 13 | // OpenStack defines this field as an opaque value, so do not depend on its content. |
| 14 | // It is safe, however, to compare for equality. |
| 15 | ID string |
| 16 | |
| 17 | // ExpiresAt provides a timestamp in ISO 8601 format, indicating when the authentication token becomes invalid. |
| 18 | // After this point in time, future API requests made using this authentication token will respond with errors. |
| 19 | // Either the caller will need to reauthenticate manually, or more preferably, the caller should exploit automatic re-authentication. |
| 20 | // See the AuthOptions structure for more details. |
| 21 | ExpiresAt time.Time |
| 22 | |
| 23 | // Tenant provides information about the tenant to which this token grants access. |
| 24 | Tenant tenants.Tenant |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 25 | } |
hzlouchao | b764089 | 2015-11-04 21:37:20 +0800 | [diff] [blame] | 26 | |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 27 | // Role is a role for a user. |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 28 | type Role struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 29 | Name string `json:"name"` |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 30 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 31 | |
| 32 | // User is an OpenStack user. |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 33 | type User struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 34 | ID string `json:"id"` |
| 35 | Name string `json:"name"` |
| 36 | UserName string `json:"username"` |
| 37 | Roles []Role `json:"roles"` |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Ash Wilson | ab48bbc | 2014-10-03 09:57:03 -0400 | [diff] [blame] | 40 | // Endpoint represents a single API endpoint offered by a service. |
| 41 | // It provides the public and internal URLs, if supported, along with a region specifier, again if provided. |
| 42 | // The significance of the Region field will depend upon your provider. |
| 43 | // |
| 44 | // In addition, the interface offered by the service will have version information associated with it |
| 45 | // through the VersionId, VersionInfo, and VersionList fields, if provided or supported. |
| 46 | // |
| 47 | // In all cases, fields which aren't supported by the provider and service combined will assume a zero-value (""). |
| 48 | type Endpoint struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 49 | TenantID string `json:"tenantId"` |
| 50 | PublicURL string `json:"publicURL"` |
| 51 | InternalURL string `json:"internalURL"` |
| 52 | AdminURL string `json:"adminURL"` |
| 53 | Region string `json:"region"` |
| 54 | VersionID string `json:"versionId"` |
| 55 | VersionInfo string `json:"versionInfo"` |
| 56 | VersionList string `json:"versionList"` |
Ash Wilson | ab48bbc | 2014-10-03 09:57:03 -0400 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | // CatalogEntry provides a type-safe interface to an Identity API V2 service catalog listing. |
| 60 | // Each class of service, such as cloud DNS or block storage services, will have a single |
| 61 | // CatalogEntry representing it. |
| 62 | // |
| 63 | // Note: when looking for the desired service, try, whenever possible, to key off the type field. |
| 64 | // Otherwise, you'll tie the representation of the service to a specific provider. |
| 65 | type CatalogEntry struct { |
| 66 | // Name will contain the provider-specified name for the service. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 67 | Name string `json:"name"` |
Ash Wilson | ab48bbc | 2014-10-03 09:57:03 -0400 | [diff] [blame] | 68 | |
| 69 | // Type will contain a type string if OpenStack defines a type for the service. |
| 70 | // Otherwise, for provider-specific services, the provider may assign their own type strings. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 71 | Type string `json:"type"` |
Ash Wilson | ab48bbc | 2014-10-03 09:57:03 -0400 | [diff] [blame] | 72 | |
| 73 | // Endpoints will let the caller iterate over all the different endpoints that may exist for |
| 74 | // the service. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 75 | Endpoints []Endpoint `json:"endpoints"` |
Ash Wilson | ab48bbc | 2014-10-03 09:57:03 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | // ServiceCatalog provides a view into the service catalog from a previous, successful authentication. |
| 79 | type ServiceCatalog struct { |
| 80 | Entries []CatalogEntry |
| 81 | } |
| 82 | |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 83 | // CreateResult defers the interpretation of a created token. |
| 84 | // Use ExtractToken() to interpret it as a Token, or ExtractServiceCatalog() to interpret it as a service catalog. |
| 85 | type CreateResult struct { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 86 | gophercloud.Result |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 87 | } |
| 88 | |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 89 | // GetResult is the deferred response from a Get call, which is the same with a Created token. |
| 90 | // Use ExtractUser() to interpret it as a User. |
hzlouchao | f6e2926 | 2015-10-27 12:51:08 +0800 | [diff] [blame] | 91 | type GetResult struct { |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 92 | CreateResult |
hzlouchao | f6e2926 | 2015-10-27 12:51:08 +0800 | [diff] [blame] | 93 | } |
| 94 | |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 95 | // ExtractToken returns the just-created Token from a CreateResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 96 | func (r CreateResult) ExtractToken() (*Token, error) { |
| 97 | var s struct { |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 98 | Access struct { |
| 99 | Token struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 100 | Expires string `json:"expires"` |
| 101 | ID string `json:"id"` |
| 102 | Tenant tenants.Tenant `json:"tenant"` |
| 103 | } `json:"token"` |
| 104 | } `json:"access"` |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 107 | err := r.ExtractInto(&s) |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 112 | expiresTs, err := time.Parse(gophercloud.RFC3339Milli, s.Access.Token.Expires) |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 113 | if err != nil { |
| 114 | return nil, err |
| 115 | } |
| 116 | |
| 117 | return &Token{ |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 118 | ID: s.Access.Token.ID, |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 119 | ExpiresAt: expiresTs, |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 120 | Tenant: s.Access.Token.Tenant, |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 121 | }, nil |
| 122 | } |
| 123 | |
Ash Wilson | ab48bbc | 2014-10-03 09:57:03 -0400 | [diff] [blame] | 124 | // ExtractServiceCatalog returns the ServiceCatalog that was generated along with the user's Token. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 125 | func (r CreateResult) ExtractServiceCatalog() (*ServiceCatalog, error) { |
| 126 | var s struct { |
Ash Wilson | ab48bbc | 2014-10-03 09:57:03 -0400 | [diff] [blame] | 127 | Access struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 128 | Entries []CatalogEntry `json:"serviceCatalog"` |
| 129 | } `json:"access"` |
Ash Wilson | ab48bbc | 2014-10-03 09:57:03 -0400 | [diff] [blame] | 130 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 131 | err := r.ExtractInto(&s) |
| 132 | return &ServiceCatalog{Entries: s.Access.Entries}, err |
Ash Wilson | ab48bbc | 2014-10-03 09:57:03 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 135 | // createErr quickly packs an error in a CreateResult. |
| 136 | func createErr(err error) CreateResult { |
Ash Wilson | f548aad | 2014-10-20 08:35:34 -0400 | [diff] [blame] | 137 | return CreateResult{gophercloud.Result{Err: err}} |
Ash Wilson | 1f11051 | 2014-10-02 15:43:47 -0400 | [diff] [blame] | 138 | } |
hzlouchao | b764089 | 2015-11-04 21:37:20 +0800 | [diff] [blame] | 139 | |
hzlouchao | 0454360 | 2015-11-30 18:44:15 +0800 | [diff] [blame] | 140 | // ExtractUser returns the User from a GetResult. |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 141 | func (r GetResult) ExtractUser() (*User, error) { |
| 142 | var s struct { |
hzlouchao | b764089 | 2015-11-04 21:37:20 +0800 | [diff] [blame] | 143 | Access struct { |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 144 | User User `json:"user"` |
| 145 | } `json:"access"` |
hzlouchao | b764089 | 2015-11-04 21:37:20 +0800 | [diff] [blame] | 146 | } |
Jon Perritt | 3c16647 | 2016-02-25 03:07:41 -0600 | [diff] [blame] | 147 | err := r.ExtractInto(&s) |
| 148 | return &s.Access.User, err |
hzlouchao | b764089 | 2015-11-04 21:37:20 +0800 | [diff] [blame] | 149 | } |