func v2auth(client *gophercloud.ProviderClient, endpoint string, options gophercloud.AuthOptions) error { v2Client := NewIdentityV2(client) if endpoint != "" { v2Client.Endpoint = endpoint } result := tokens2.Create(v2Client, tokens2.AuthOptions{AuthOptions: options}) token, err := result.ExtractToken() if err != nil { return err } catalog, err := result.ExtractServiceCatalog() if err != nil { return err } if options.AllowReauth { client.ReauthFunc = func() error { client.TokenID = "" return AuthenticateV2(client, options) } } client.TokenID = token.ID client.EndpointLocator = func(opts gophercloud.EndpointOpts) (string, error) { return V2EndpointURL(catalog, opts) } return nil }
func TestAuthenticate(t *testing.T) { ao := v2AuthOptions(t) service := unauthenticatedClient(t) // Authenticated! result := tokens2.Create(service, tokens2.WrapOptions(ao)) // Extract and print the token. token, err := result.ExtractToken() th.AssertNoErr(t, err) t.Logf("Acquired token: [%s]", token.ID) t.Logf("The token will expire at: [%s]", token.ExpiresAt.String()) t.Logf("The token is valid for tenant: [%#v]", token.Tenant) // Extract and print the service catalog. catalog, err := result.ExtractServiceCatalog() th.AssertNoErr(t, err) t.Logf("Acquired service catalog listing [%d] services", len(catalog.Entries)) for i, entry := range catalog.Entries { t.Logf("[%02d]: name=[%s], type=[%s]", i, entry.Name, entry.Type) for _, endpoint := range entry.Endpoints { t.Logf(" - region=[%s] publicURL=[%s]", endpoint.Region, endpoint.PublicURL) } } }
func CreateToken(a gophercloud.AuthOptions, c *gophercloud.ServiceClient) (*tokens.Token, error) { opts := tokens.WrapOptions(a) // Create a new token token, err := tokens.Create(c, opts).ExtractToken() if err != nil { return nil, err } return token, nil }
func TestAuthenticateAndValidate(t *testing.T) { // 1. TestAuthenticate ao := v2AuthOptions(t) service := unauthenticatedClient(t) // Authenticated! result := tokens2.Create(service, tokens2.WrapOptions(ao)) // Extract and print the token. token, err := result.ExtractToken() th.AssertNoErr(t, err) t.Logf("Acquired token: [%s]", token.ID) t.Logf("The token will expire at: [%s]", token.ExpiresAt.String()) t.Logf("The token is valid for tenant: [%#v]", token.Tenant) // Extract and print the service catalog. catalog, err := result.ExtractServiceCatalog() th.AssertNoErr(t, err) t.Logf("Acquired service catalog listing [%d] services", len(catalog.Entries)) for i, entry := range catalog.Entries { t.Logf("[%02d]: name=[%s], type=[%s]", i, entry.Name, entry.Type) for _, endpoint := range entry.Endpoints { t.Logf(" - region=[%s] publicURL=[%s]", endpoint.Region, endpoint.PublicURL) } } // 2. TestValidate client := authenticatedClient(t) // Validate Token! getResult := tokens2.Get(client, token.ID) // Extract and print the user. user, err := getResult.ExtractUser() th.AssertNoErr(t, err) t.Logf("Acquired User: [%s]", user.Name) t.Logf("The User id: [%s]", user.ID) t.Logf("The User username: [%s]", user.UserName) t.Logf("The User roles: [%#v]", user.Roles) }
func get_token() (*tokens.Token, error) { authOpts, err := openstack.AuthOptionsFromEnv() // authOpts := gophercloud.AuthOptions{ // IdentityEndpoint: endpoint, // Username: username, // Password: pass, // TenantID: tenantid, // } provider, err := openstack.AuthenticatedClient(authOpts) client := openstack.NewIdentityV2(provider) opts := tokens.AuthOptions{authOpts} bearer_token, err := tokens.Create(client, opts).ExtractToken() if err != nil { return nil, err } return bearer_token, nil }
// Create authenticates to Rackspace's identity service and attempts to acquire a Token. Rather // than interact with this service directly, users should generally call // rackspace.AuthenticatedClient(). func Create(client *gophercloud.ServiceClient, auth AuthOptions) os.CreateResult { return os.Create(client, auth) }