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