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