blob: 5f25ee615cc149841966e6bb23d403decd73ef0a [file] [log] [blame]
Ash Wilson85d82652014-08-28 13:57:46 -04001package v3
2
Ash Wilson8a85a912014-08-28 15:09:58 -04003import (
Ash Wilson6425a412014-08-29 12:30:35 -04004 "time"
5
Ash Wilson8a85a912014-08-28 15:09:58 -04006 "github.com/rackspace/gophercloud"
Ash Wilson131b7752014-08-29 12:53:55 -04007 "github.com/rackspace/gophercloud/openstack/identity/v3/tokens"
Ash Wilson8a85a912014-08-28 15:09:58 -04008)
Ash Wilson85d82652014-08-28 13:57:46 -04009
Ash Wilson6425a412014-08-29 12:30:35 -040010// Client abstracts the connection information necessary to make API calls to Identity v3 resources.
11// It exists mainly to adhere to the IdentityService interface.
Ash Wilson131b7752014-08-29 12:53:55 -040012type Client struct {
13 gophercloud.ServiceClient
14}
Ash Wilson85d82652014-08-28 13:57:46 -040015
Ash Wilson6425a412014-08-29 12:30:35 -040016// Token models a token acquired from the tokens/ API resource.
17type Token struct {
18 ID string
19 ExpiresAt time.Time
20}
Ash Wilson85d82652014-08-28 13:57:46 -040021
Ash Wilson6425a412014-08-29 12:30:35 -040022// NewClient creates a new client associated with the v3 identity service of a provider.
23func NewClient(provider *gophercloud.ProviderClient) *Client {
24 return &Client{
Ash Wilson131b7752014-08-29 12:53:55 -040025 ServiceClient: gophercloud.ServiceClient{
26 ProviderClient: *provider,
27 Endpoint: provider.IdentityEndpoint + "v3/",
28 },
Ash Wilson8a85a912014-08-28 15:09:58 -040029 }
Ash Wilson6425a412014-08-29 12:30:35 -040030}
Ash Wilson8a85a912014-08-28 15:09:58 -040031
Ash Wilson6425a412014-08-29 12:30:35 -040032// Authenticate provides the supplied credentials to an identity v3 endpoint and attempts to acquire a token.
33func (c *Client) Authenticate(authOptions gophercloud.AuthOptions) (*Token, error) {
Ash Wilson131b7752014-08-29 12:53:55 -040034 c.ServiceClient.ProviderClient.Options = authOptions
35
36 result, err := tokens.Create(&c.ServiceClient, nil)
37 if err != nil {
38 return nil, err
39 }
40
41 tokenID, err := result.TokenID()
42 if err != nil {
43 return nil, err
44 }
45
46 expiresAt, err := result.ExpiresAt()
47 if err != nil {
48 return nil, err
49 }
50
51 return &Token{
52 ID: tokenID,
53 ExpiresAt: expiresAt,
54 }, nil
Ash Wilson85d82652014-08-28 13:57:46 -040055}