Samuel A. Falvo II | 1d3fa66 | 2013-06-25 15:29:32 -0700 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 3 | import ( |
| 4 | "github.com/racker/perigee" |
| 5 | ) |
| 6 | |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame] | 7 | // AuthOptions lets anyone calling Authenticate() supply the required access credentials. |
| 8 | // At present, only Identity V2 API support exists; therefore, only Username, Password, |
| 9 | // and optionally, TenantId are provided. If future Identity API versions become available, |
| 10 | // alternative fields unique to those versions may appear here. |
Samuel A. Falvo II | 1d3fa66 | 2013-06-25 15:29:32 -0700 | [diff] [blame] | 11 | type AuthOptions struct { |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame] | 12 | // Username and Password are required if using Identity V2 API. |
| 13 | // Consult with your provider's control panel to discover your |
| 14 | // account's username and password. |
| 15 | Username, Password string |
| 16 | |
| 17 | // The TenantId field is optional for the Identity V2 API. |
| 18 | TenantId string |
Samuel A. Falvo II | 9e64f6b | 2013-07-16 14:26:50 -0700 | [diff] [blame] | 19 | |
Justin Santa Barbara | 21682a4 | 2013-08-31 17:56:13 -0700 | [diff] [blame] | 20 | // The TenantName can be specified instead of the TenantId |
| 21 | TenantName string |
| 22 | |
Samuel A. Falvo II | 9e64f6b | 2013-07-16 14:26:50 -0700 | [diff] [blame] | 23 | // AllowReauth should be set to true if you grant permission for Gophercloud to cache |
| 24 | // your credentials in memory, and to allow Gophercloud to attempt to re-authenticate |
| 25 | // automatically if/when your token expires. If you set it to false, it will not cache |
| 26 | // these settings, but re-authentication will not be possible. This setting defaults |
| 27 | // to false. |
| 28 | AllowReauth bool |
Samuel A. Falvo II | 1d3fa66 | 2013-06-25 15:29:32 -0700 | [diff] [blame] | 29 | } |
| 30 | |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame] | 31 | // AuthContainer provides a JSON encoding wrapper for passing credentials to the Identity |
| 32 | // service. You will not work with this structure directly. |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 33 | type AuthContainer struct { |
| 34 | Auth Auth `json:"auth"` |
| 35 | } |
| 36 | |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame] | 37 | // Auth provides a JSON encoding wrapper for passing credentials to the Identity |
| 38 | // service. You will not work with this structure directly. |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 39 | type Auth struct { |
| 40 | PasswordCredentials PasswordCredentials `json:"passwordCredentials"` |
Samuel A. Falvo II | 839428e | 2013-06-25 18:02:24 -0700 | [diff] [blame] | 41 | TenantId string `json:"tenantId,omitempty"` |
Justin Santa Barbara | 21682a4 | 2013-08-31 17:56:13 -0700 | [diff] [blame] | 42 | TenantName string `json:"tenantName,omitempty"` |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 43 | } |
| 44 | |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame] | 45 | // PasswordCredentials provides a JSON encoding wrapper for passing credentials to the Identity |
| 46 | // service. You will not work with this structure directly. |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 47 | type PasswordCredentials struct { |
| 48 | Username string `json:"username"` |
| 49 | Password string `json:"password"` |
| 50 | } |
| 51 | |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 52 | // Access encapsulates the API token and its relevant fields, as well as the |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 53 | // services catalog that Identity API returns once authenticated. |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 54 | type Access struct { |
| 55 | Token Token |
| 56 | ServiceCatalog []CatalogEntry |
| 57 | User User |
Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame] | 58 | provider Provider `json:"-"` |
Samuel A. Falvo II | 9e64f6b | 2013-07-16 14:26:50 -0700 | [diff] [blame] | 59 | options AuthOptions `json:"-"` |
Samuel A. Falvo II | 20f1aa4 | 2013-07-31 14:32:03 -0700 | [diff] [blame] | 60 | context *Context `json:"-"` |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 63 | // Token encapsulates an authentication token and when it expires. It also includes |
| 64 | // tenant information if available. |
| 65 | type Token struct { |
| 66 | Id, Expires string |
| 67 | Tenant Tenant |
| 68 | } |
| 69 | |
| 70 | // Tenant encapsulates tenant authentication information. If, after authentication, |
| 71 | // no tenant information is supplied, both Id and Name will be "". |
| 72 | type Tenant struct { |
| 73 | Id, Name string |
| 74 | } |
| 75 | |
| 76 | // User encapsulates the user credentials, and provides visibility in what |
| 77 | // the user can do through its role assignments. |
| 78 | type User struct { |
| 79 | Id, Name string |
| 80 | XRaxDefaultRegion string `json:"RAX-AUTH:defaultRegion"` |
| 81 | Roles []Role |
| 82 | } |
| 83 | |
| 84 | // Role encapsulates a permission that a user can rely on. |
| 85 | type Role struct { |
| 86 | Description, Id, Name string |
| 87 | } |
| 88 | |
| 89 | // CatalogEntry encapsulates a service catalog record. |
| 90 | type CatalogEntry struct { |
| 91 | Name, Type string |
| 92 | Endpoints []EntryEndpoint |
| 93 | } |
| 94 | |
| 95 | // EntryEndpoint encapsulates how to get to the API of some service. |
| 96 | type EntryEndpoint struct { |
| 97 | Region, TenantId string |
| 98 | PublicURL, InternalURL string |
| 99 | VersionId, VersionInfo, VersionList string |
| 100 | } |
| 101 | |
Samuel A. Falvo II | 9e64f6b | 2013-07-16 14:26:50 -0700 | [diff] [blame] | 102 | // papersPlease contains the common logic between authentication and re-authentication. |
| 103 | // The name, obviously a joke on the process of authentication, was chosen because |
| 104 | // of how many other entities exist in the program containing the word Auth or Authorization. |
| 105 | // I didn't need another one. |
| 106 | func (c *Context) papersPlease(p Provider, options AuthOptions) (*Access, error) { |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 107 | var access *Access |
| 108 | |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 109 | if (options.Username == "") || (options.Password == "") { |
| 110 | return nil, ErrCredentials |
| 111 | } |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 112 | |
Samuel A. Falvo II | 9e64f6b | 2013-07-16 14:26:50 -0700 | [diff] [blame] | 113 | err := perigee.Post(p.AuthEndpoint, perigee.Options{ |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 114 | CustomClient: c.httpClient, |
Samuel A. Falvo II | 839428e | 2013-06-25 18:02:24 -0700 | [diff] [blame] | 115 | ReqBody: &AuthContainer{ |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 116 | Auth: Auth{ |
| 117 | PasswordCredentials: PasswordCredentials{ |
| 118 | Username: options.Username, |
| 119 | Password: options.Password, |
| 120 | }, |
Justin Santa Barbara | 21682a4 | 2013-08-31 17:56:13 -0700 | [diff] [blame] | 121 | TenantId: options.TenantId, |
| 122 | TenantName: options.TenantName, |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 123 | }, |
| 124 | }, |
Samuel A. Falvo II | 4e89518 | 2013-06-26 15:44:18 -0700 | [diff] [blame] | 125 | Results: &struct { |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 126 | Access **Access `json:"access"` |
| 127 | }{ |
| 128 | &access, |
| 129 | }, |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 130 | }) |
Samuel A. Falvo II | 0167aaa | 2013-07-16 12:36:25 -0700 | [diff] [blame] | 131 | if err == nil { |
Samuel A. Falvo II | 9e64f6b | 2013-07-16 14:26:50 -0700 | [diff] [blame] | 132 | access.options = options |
Samuel A. Falvo II | 0167aaa | 2013-07-16 12:36:25 -0700 | [diff] [blame] | 133 | access.provider = p |
Samuel A. Falvo II | 9e64f6b | 2013-07-16 14:26:50 -0700 | [diff] [blame] | 134 | access.context = c |
Samuel A. Falvo II | 0167aaa | 2013-07-16 12:36:25 -0700 | [diff] [blame] | 135 | } |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame] | 136 | return access, err |
Samuel A. Falvo II | 1d3fa66 | 2013-06-25 15:29:32 -0700 | [diff] [blame] | 137 | } |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 138 | |
Samuel A. Falvo II | 9e64f6b | 2013-07-16 14:26:50 -0700 | [diff] [blame] | 139 | // Authenticate() grants access to the OpenStack-compatible provider API. |
| 140 | // |
| 141 | // Providers are identified through a unique key string. |
| 142 | // See the RegisterProvider() method for more details. |
| 143 | // |
| 144 | // The supplied AuthOptions instance allows the client to specify only those credentials |
| 145 | // relevant for the authentication request. At present, support exists for OpenStack |
| 146 | // Identity V2 API only; support for V3 will become available as soon as documentation for it |
| 147 | // becomes readily available. |
| 148 | // |
| 149 | // For Identity V2 API requirements, you must provide at least the Username and Password |
| 150 | // options. The TenantId field is optional, and defaults to "". |
| 151 | func (c *Context) Authenticate(provider string, options AuthOptions) (*Access, error) { |
| 152 | p, err := c.ProviderByName(provider) |
| 153 | if err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | return c.papersPlease(p, options) |
| 157 | } |
| 158 | |
| 159 | // Reauthenticate attempts to reauthenticate using the configured access credentials, if |
| 160 | // allowed. This method takes no action unless your AuthOptions has the AllowReauth flag |
| 161 | // set to true. |
| 162 | func (a *Access) Reauthenticate() error { |
| 163 | var other *Access |
| 164 | var err error |
| 165 | |
Samuel A. Falvo II | 9e64f6b | 2013-07-16 14:26:50 -0700 | [diff] [blame] | 166 | if a.options.AllowReauth { |
| 167 | other, err = a.context.papersPlease(a.provider, a.options) |
Samuel A. Falvo II | fb58669 | 2013-07-16 17:00:14 -0700 | [diff] [blame] | 168 | if err == nil { |
Samuel A. Falvo II | 9e64f6b | 2013-07-16 14:26:50 -0700 | [diff] [blame] | 169 | *a = *other |
| 170 | } |
| 171 | } |
| 172 | return err |
| 173 | } |
| 174 | |
Samuel A. Falvo II | 2e2b877 | 2013-07-04 15:40:15 -0700 | [diff] [blame] | 175 | // See AccessProvider interface definition for details. |
| 176 | func (a *Access) FirstEndpointUrlByCriteria(ac ApiCriteria) string { |
| 177 | ep := FindFirstEndpointByCriteria(a.ServiceCatalog, ac) |
| 178 | urls := []string{ep.PublicURL, ep.InternalURL} |
| 179 | return urls[ac.UrlChoice] |
| 180 | } |
Samuel A. Falvo II | bc0d54a | 2013-07-08 14:45:21 -0700 | [diff] [blame] | 181 | |
| 182 | // See AccessProvider interface definition for details. |
| 183 | func (a *Access) AuthToken() string { |
| 184 | return a.Token.Id |
| 185 | } |
Samuel A. Falvo II | 659e14b | 2013-07-16 12:04:54 -0700 | [diff] [blame] | 186 | |
| 187 | // See AccessProvider interface definition for details. |
| 188 | func (a *Access) Revoke(tok string) error { |
Samuel A. Falvo II | 0167aaa | 2013-07-16 12:36:25 -0700 | [diff] [blame] | 189 | url := a.provider.AuthEndpoint + "/" + tok |
| 190 | err := perigee.Delete(url, perigee.Options{ |
| 191 | MoreHeaders: map[string]string{ |
| 192 | "X-Auth-Token": a.AuthToken(), |
| 193 | }, |
| 194 | OkCodes: []int{204}, |
| 195 | }) |
| 196 | return err |
Samuel A. Falvo II | 659e14b | 2013-07-16 12:04:54 -0700 | [diff] [blame] | 197 | } |
Samuel A. Falvo II | cfb352a | 2013-11-19 14:39:37 -0800 | [diff] [blame^] | 198 | |
| 199 | // See ServiceCatalogerForIdentityV2 interface definition for details. |
| 200 | // Note that the raw slice is returend; be careful not to alter the fields of any members, |
| 201 | // for other components of Gophercloud may depend upon them. |
| 202 | // If this becomes a problem in the future, |
| 203 | // a future revision may return a deep-copy of the service catalog instead. |
| 204 | func (a *Access) V2ServiceCatalog() []CatalogEntry { |
| 205 | return a.ServiceCatalog |
| 206 | } |