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 | 1d3fa66 | 2013-06-25 15:29:32 -0700 | [diff] [blame] | 7 | type AuthOptions struct { |
| 8 | Username, Password, TenantId string |
| 9 | } |
| 10 | |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 11 | type AuthContainer struct { |
| 12 | Auth Auth `json:"auth"` |
| 13 | } |
| 14 | |
| 15 | type Auth struct { |
| 16 | PasswordCredentials PasswordCredentials `json:"passwordCredentials"` |
Samuel A. Falvo II | 839428e | 2013-06-25 18:02:24 -0700 | [diff] [blame] | 17 | TenantId string `json:"tenantId,omitempty"` |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 18 | } |
| 19 | |
| 20 | type PasswordCredentials struct { |
| 21 | Username string `json:"username"` |
| 22 | Password string `json:"password"` |
| 23 | } |
| 24 | |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame^] | 25 | // 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. |
| 29 | type Access struct { |
| 30 | Token Token |
| 31 | ServiceCatalog []CatalogEntry |
| 32 | User User |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 33 | } |
| 34 | |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame^] | 35 | // Token encapsulates an authentication token and when it expires. It also includes |
| 36 | // tenant information if available. |
| 37 | type 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 "". |
| 44 | type 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. |
| 50 | type 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. |
| 57 | type Role struct { |
| 58 | Description, Id, Name string |
| 59 | } |
| 60 | |
| 61 | // CatalogEntry encapsulates a service catalog record. |
| 62 | type CatalogEntry struct { |
| 63 | Name, Type string |
| 64 | Endpoints []EntryEndpoint |
| 65 | } |
| 66 | |
| 67 | // EntryEndpoint encapsulates how to get to the API of some service. |
| 68 | type EntryEndpoint struct { |
| 69 | Region, TenantId string |
| 70 | PublicURL, InternalURL string |
| 71 | VersionId, VersionInfo, VersionList string |
| 72 | } |
| 73 | |
| 74 | func (c *Context) Authenticate(provider string, options AuthOptions) (*Access, error) { |
| 75 | var access *Access |
| 76 | |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 77 | p, err := c.ProviderByName(provider) |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 78 | if err != nil { |
| 79 | return nil, err |
| 80 | } |
Samuel A. Falvo II | fd78c30 | 2013-06-25 16:35:32 -0700 | [diff] [blame] | 81 | if (options.Username == "") || (options.Password == "") { |
| 82 | return nil, ErrCredentials |
| 83 | } |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 84 | |
| 85 | err = perigee.Post(p.AuthEndpoint, perigee.Options{ |
| 86 | CustomClient: c.httpClient, |
Samuel A. Falvo II | 839428e | 2013-06-25 18:02:24 -0700 | [diff] [blame] | 87 | ReqBody: &AuthContainer{ |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 88 | Auth: Auth{ |
| 89 | PasswordCredentials: PasswordCredentials{ |
| 90 | Username: options.Username, |
| 91 | Password: options.Password, |
| 92 | }, |
| 93 | TenantId: options.TenantId, |
| 94 | }, |
| 95 | }, |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame^] | 96 | Results: &struct{ |
| 97 | Access **Access `json:"access"` |
| 98 | }{ |
| 99 | &access, |
| 100 | }, |
Samuel A. Falvo II | 5d0d74c | 2013-06-25 17:23:18 -0700 | [diff] [blame] | 101 | }) |
Samuel A. Falvo II | d1ee798 | 2013-06-26 14:32:45 -0700 | [diff] [blame^] | 102 | return access, err |
Samuel A. Falvo II | 1d3fa66 | 2013-06-25 15:29:32 -0700 | [diff] [blame] | 103 | } |