blob: 8b87e4420681a50c27a8db2a2c13398ce0a4a179 [file] [log] [blame]
Samuel A. Falvo II1d3fa662013-06-25 15:29:32 -07001package gophercloud
2
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -07003import (
4 "github.com/racker/perigee"
5)
6
Samuel A. Falvo II4e895182013-06-26 15:44:18 -07007// 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 II1d3fa662013-06-25 15:29:32 -070011type AuthOptions struct {
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070012 // 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 II1d3fa662013-06-25 15:29:32 -070019}
20
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070021// AuthContainer provides a JSON encoding wrapper for passing credentials to the Identity
22// service. You will not work with this structure directly.
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070023type AuthContainer struct {
24 Auth Auth `json:"auth"`
25}
26
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070027// Auth provides a JSON encoding wrapper for passing credentials to the Identity
28// service. You will not work with this structure directly.
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070029type Auth struct {
30 PasswordCredentials PasswordCredentials `json:"passwordCredentials"`
Samuel A. Falvo II839428e2013-06-25 18:02:24 -070031 TenantId string `json:"tenantId,omitempty"`
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070032}
33
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070034// PasswordCredentials provides a JSON encoding wrapper for passing credentials to the Identity
35// service. You will not work with this structure directly.
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070036type PasswordCredentials struct {
37 Username string `json:"username"`
38 Password string `json:"password"`
39}
40
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -070041// Access encapsulates the API token and its relevant fields, as well as the
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -070042// services catalog that Identity API returns once authenticated.
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -070043type Access struct {
44 Token Token
45 ServiceCatalog []CatalogEntry
46 User User
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070047}
48
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -070049// Token encapsulates an authentication token and when it expires. It also includes
50// tenant information if available.
51type Token struct {
52 Id, Expires string
53 Tenant Tenant
54}
55
56// Tenant encapsulates tenant authentication information. If, after authentication,
57// no tenant information is supplied, both Id and Name will be "".
58type Tenant struct {
59 Id, Name string
60}
61
62// User encapsulates the user credentials, and provides visibility in what
63// the user can do through its role assignments.
64type User struct {
65 Id, Name string
66 XRaxDefaultRegion string `json:"RAX-AUTH:defaultRegion"`
67 Roles []Role
68}
69
70// Role encapsulates a permission that a user can rely on.
71type Role struct {
72 Description, Id, Name string
73}
74
75// CatalogEntry encapsulates a service catalog record.
76type CatalogEntry struct {
77 Name, Type string
78 Endpoints []EntryEndpoint
79}
80
81// EntryEndpoint encapsulates how to get to the API of some service.
82type EntryEndpoint struct {
83 Region, TenantId string
84 PublicURL, InternalURL string
85 VersionId, VersionInfo, VersionList string
86}
87
Samuel A. Falvo II4e895182013-06-26 15:44:18 -070088// Authenticate() grants access to the OpenStack-compatible provider API.
89//
90// Providers are identified through a unique key string.
91// See the RegisterProvider() method for more details.
92//
93// The supplied AuthOptions instance allows the client to specify only those credentials
94// relevant for the authentication request. At present, support exists for OpenStack
95// Identity V2 API only; support for V3 will become available as soon as documentation for it
96// becomes readily available.
97//
98// For Identity V2 API requirements, you must provide at least the Username and Password
99// options. The TenantId field is optional, and defaults to "".
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -0700100func (c *Context) Authenticate(provider string, options AuthOptions) (*Access, error) {
101 var access *Access
102
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -0700103 p, err := c.ProviderByName(provider)
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700104 if err != nil {
105 return nil, err
106 }
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -0700107 if (options.Username == "") || (options.Password == "") {
108 return nil, ErrCredentials
109 }
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -0700110
111 err = perigee.Post(p.AuthEndpoint, perigee.Options{
112 CustomClient: c.httpClient,
Samuel A. Falvo II839428e2013-06-25 18:02:24 -0700113 ReqBody: &AuthContainer{
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -0700114 Auth: Auth{
115 PasswordCredentials: PasswordCredentials{
116 Username: options.Username,
117 Password: options.Password,
118 },
119 TenantId: options.TenantId,
120 },
121 },
Samuel A. Falvo II4e895182013-06-26 15:44:18 -0700122 Results: &struct {
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -0700123 Access **Access `json:"access"`
124 }{
125 &access,
126 },
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -0700127 })
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -0700128 return access, err
Samuel A. Falvo II1d3fa662013-06-25 15:29:32 -0700129}
Samuel A. Falvo II2e2b8772013-07-04 15:40:15 -0700130
131// See AccessProvider interface definition for details.
132func (a *Access) FirstEndpointUrlByCriteria(ac ApiCriteria) string {
133 ep := FindFirstEndpointByCriteria(a.ServiceCatalog, ac)
134 urls := []string{ep.PublicURL, ep.InternalURL}
135 return urls[ac.UrlChoice]
136}
Samuel A. Falvo IIbc0d54a2013-07-08 14:45:21 -0700137
138// See AccessProvider interface definition for details.
139func (a *Access) AuthToken() string {
140 return a.Token.Id
141}