blob: 4d3254fc328b9c108cc04e36247b6d8ec3dae933 [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 II1d3fa662013-06-25 15:29:32 -07007type AuthOptions struct {
8 Username, Password, TenantId string
9}
10
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070011type AuthContainer struct {
12 Auth Auth `json:"auth"`
13}
14
15type Auth struct {
16 PasswordCredentials PasswordCredentials `json:"passwordCredentials"`
Samuel A. Falvo II839428e2013-06-25 18:02:24 -070017 TenantId string `json:"tenantId,omitempty"`
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070018}
19
20type PasswordCredentials struct {
21 Username string `json:"username"`
22 Password string `json:"password"`
23}
24
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -070025// Access encapsulates the API token and its relevant fields, as well as the
26// services catalog that Identity API returns once authenticated. You'll probably
27// rarely use this record directly, unless you intend on marshalling or unmarshalling
28// Identity API JSON records yourself.
29type Access struct {
30 Token Token
31 ServiceCatalog []CatalogEntry
32 User User
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070033}
34
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -070035// Token encapsulates an authentication token and when it expires. It also includes
36// tenant information if available.
37type Token struct {
38 Id, Expires string
39 Tenant Tenant
40}
41
42// Tenant encapsulates tenant authentication information. If, after authentication,
43// no tenant information is supplied, both Id and Name will be "".
44type Tenant struct {
45 Id, Name string
46}
47
48// User encapsulates the user credentials, and provides visibility in what
49// the user can do through its role assignments.
50type User struct {
51 Id, Name string
52 XRaxDefaultRegion string `json:"RAX-AUTH:defaultRegion"`
53 Roles []Role
54}
55
56// Role encapsulates a permission that a user can rely on.
57type Role struct {
58 Description, Id, Name string
59}
60
61// CatalogEntry encapsulates a service catalog record.
62type CatalogEntry struct {
63 Name, Type string
64 Endpoints []EntryEndpoint
65}
66
67// EntryEndpoint encapsulates how to get to the API of some service.
68type EntryEndpoint struct {
69 Region, TenantId string
70 PublicURL, InternalURL string
71 VersionId, VersionInfo, VersionList string
72}
73
74func (c *Context) Authenticate(provider string, options AuthOptions) (*Access, error) {
75 var access *Access
76
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070077 p, err := c.ProviderByName(provider)
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070078 if err != nil {
79 return nil, err
80 }
Samuel A. Falvo IIfd78c302013-06-25 16:35:32 -070081 if (options.Username == "") || (options.Password == "") {
82 return nil, ErrCredentials
83 }
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070084
85 err = perigee.Post(p.AuthEndpoint, perigee.Options{
86 CustomClient: c.httpClient,
Samuel A. Falvo II839428e2013-06-25 18:02:24 -070087 ReqBody: &AuthContainer{
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -070088 Auth: Auth{
89 PasswordCredentials: PasswordCredentials{
90 Username: options.Username,
91 Password: options.Password,
92 },
93 TenantId: options.TenantId,
94 },
95 },
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -070096 Results: &struct{
97 Access **Access `json:"access"`
98 }{
99 &access,
100 },
Samuel A. Falvo II5d0d74c2013-06-25 17:23:18 -0700101 })
Samuel A. Falvo IId1ee7982013-06-26 14:32:45 -0700102 return access, err
Samuel A. Falvo II1d3fa662013-06-25 15:29:32 -0700103}