Parse out auth response and fill in access object
diff --git a/authenticate.go b/authenticate.go
index 09ee36f..4d3254f 100644
--- a/authenticate.go
+++ b/authenticate.go
@@ -22,11 +22,58 @@
Password string `json:"password"`
}
-type ProviderAccess interface {
- // ...
+// Access encapsulates the API token and its relevant fields, as well as the
+// services catalog that Identity API returns once authenticated. You'll probably
+// rarely use this record directly, unless you intend on marshalling or unmarshalling
+// Identity API JSON records yourself.
+type Access struct {
+ Token Token
+ ServiceCatalog []CatalogEntry
+ User User
}
-func (c *Context) Authenticate(provider string, options AuthOptions) (ProviderAccess, error) {
+// 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
+}
+
+func (c *Context) Authenticate(provider string, options AuthOptions) (*Access, error) {
+ var access *Access
+
p, err := c.ProviderByName(provider)
if err != nil {
return nil, err
@@ -46,6 +93,11 @@
TenantId: options.TenantId,
},
},
+ Results: &struct{
+ Access **Access `json:"access"`
+ }{
+ &access,
+ },
})
- return nil, err
+ return access, err
}
diff --git a/authenticate_test.go b/authenticate_test.go
index 69b8bcc..0402e2a 100644
--- a/authenticate_test.go
+++ b/authenticate_test.go
@@ -8,6 +8,53 @@
"testing"
)
+const SUCCESSFUL_RESPONSE = `{
+ "access": {
+ "serviceCatalog": [{
+ "endpoints": [{
+ "publicURL": "https://ord.servers.api.rackspacecloud.com/v2/12345",
+ "region": "ORD",
+ "tenantId": "12345",
+ "versionId": "2",
+ "versionInfo": "https://ord.servers.api.rackspacecloud.com/v2",
+ "versionList": "https://ord.servers.api.rackspacecloud.com/"
+ },{
+ "publicURL": "https://dfw.servers.api.rackspacecloud.com/v2/12345",
+ "region": "DFW",
+ "tenantId": "12345",
+ "versionId": "2",
+ "versionInfo": "https://dfw.servers.api.rackspacecloud.com/v2",
+ "versionList": "https://dfw.servers.api.rackspacecloud.com/"
+ }],
+ "name": "cloudServersOpenStack",
+ "type": "compute"
+ },{
+ "endpoints": [{
+ "publicURL": "https://ord.databases.api.rackspacecloud.com/v1.0/12345",
+ "region": "ORD",
+ "tenantId": "12345"
+ }],
+ "name": "cloudDatabases",
+ "type": "rax:database"
+ }],
+ "token": {
+ "expires": "2012-04-13T13:15:00.000-05:00",
+ "id": "aaaaa-bbbbb-ccccc-dddd"
+ },
+ "user": {
+ "RAX-AUTH:defaultRegion": "DFW",
+ "id": "161418",
+ "name": "demoauthor",
+ "roles": [{
+ "description": "User Admin Role.",
+ "id": "3",
+ "name": "identity:user-admin"
+ }]
+ }
+ }
+}
+`
+
type testTransport struct {
called int
response string
@@ -179,3 +226,74 @@
return
}
}
+
+func TestTokenAcquisition(t *testing.T) {
+ c := TestContext()
+ tt := &testTransport{}
+ tt.response = SUCCESSFUL_RESPONSE
+ c.UseCustomClient(&http.Client{Transport: tt})
+ c.RegisterProvider("provider", &Provider{AuthEndpoint: "http://localhost"})
+
+ acc, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
+ if err != nil {
+ t.Error(err)
+ return
+ }
+
+ tok := acc.Token
+ if (tok.Id == "") || (tok.Expires == "") {
+ t.Error("Expected a valid token for successful login; got %s, %s", tok.Id, tok.Expires)
+ return
+ }
+}
+
+func TestServiceCatalogAcquisition(t *testing.T) {
+ c := TestContext()
+ tt := &testTransport{}
+ tt.response = SUCCESSFUL_RESPONSE
+ c.UseCustomClient(&http.Client{Transport: tt})
+ c.RegisterProvider("provider", &Provider{AuthEndpoint: "http://localhost"})
+
+ acc, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
+ if err != nil {
+ t.Error(err)
+ return
+ }
+
+ svcs := acc.ServiceCatalog
+ if len(svcs) < 2 {
+ t.Error("Expected 2 service catalog entries; got %d", len(svcs))
+ return
+ }
+
+ types := map[string]bool {
+ "compute": true,
+ "rax:database": true,
+ }
+ for _, entry := range svcs {
+ if !types[entry.Type] {
+ t.Error("Expected to find type %s.", entry.Type)
+ return
+ }
+ }
+}
+
+func TestUserAcquisition(t *testing.T) {
+ c := TestContext()
+ tt := &testTransport{}
+ tt.response = SUCCESSFUL_RESPONSE
+ c.UseCustomClient(&http.Client{Transport: tt})
+ c.RegisterProvider("provider", &Provider{AuthEndpoint: "http://localhost"})
+
+ acc, err := c.Authenticate("provider", AuthOptions{Username: "u", Password: "p"})
+ if err != nil {
+ t.Error(err)
+ return
+ }
+
+ u := acc.User
+ if u.Id != "161418" {
+ t.Error("Expected user ID of 16148; got", u.Id)
+ return
+ }
+}
\ No newline at end of file