3 import "github.com/gophercloud/gophercloud"
5 // PasswordCredentialsV2 represents the required options to authenticate
6 // with a username and password.
7 type PasswordCredentialsV2 struct {
8 Username string `json:"username" required:"true"`
9 Password string `json:"password" required:"true"`
12 // TokenCredentialsV2 represents the required options to authenticate
14 type TokenCredentialsV2 struct {
15 ID string `json:"id,omitempty" required:"true"`
18 // AuthOptionsV2 wraps a gophercloud AuthOptions in order to adhere to the
19 // AuthOptionsBuilder interface.
20 type AuthOptionsV2 struct {
21 PasswordCredentials *PasswordCredentialsV2 `json:"passwordCredentials,omitempty" xor:"TokenCredentials"`
23 // The TenantID and TenantName fields are optional for the Identity V2 API.
24 // Some providers allow you to specify a TenantName instead of the TenantId.
25 // Some require both. Your provider's authentication policies will determine
26 // how these fields influence authentication.
27 TenantID string `json:"tenantId,omitempty"`
28 TenantName string `json:"tenantName,omitempty"`
30 // TokenCredentials allows users to authenticate (possibly as another user)
31 // with an authentication token ID.
32 TokenCredentials *TokenCredentialsV2 `json:"token,omitempty" xor:"PasswordCredentials"`
35 // AuthOptionsBuilder allows extensions to add additional parameters to the
36 // token create request.
37 type AuthOptionsBuilder interface {
38 // ToTokenCreateMap assembles the Create request body, returning an error
39 // if parameters are missing or inconsistent.
40 ToTokenV2CreateMap() (map[string]interface{}, error)
43 // AuthOptions are the valid options for Openstack Identity v2 authentication.
44 // For field descriptions, see gophercloud.AuthOptions.
45 type AuthOptions struct {
46 IdentityEndpoint string `json:"-"`
47 Username string `json:"username,omitempty"`
48 Password string `json:"password,omitempty"`
49 TenantID string `json:"tenantId,omitempty"`
50 TenantName string `json:"tenantName,omitempty"`
51 AllowReauth bool `json:"-"`
55 // ToTokenV2CreateMap builds a token request body from the given AuthOptions.
56 func (opts AuthOptions) ToTokenV2CreateMap() (map[string]interface{}, error) {
57 v2Opts := AuthOptionsV2{
58 TenantID: opts.TenantID,
59 TenantName: opts.TenantName,
62 if opts.Password != "" {
63 v2Opts.PasswordCredentials = &PasswordCredentialsV2{
64 Username: opts.Username,
65 Password: opts.Password,
68 v2Opts.TokenCredentials = &TokenCredentialsV2{
73 b, err := gophercloud.BuildRequestBody(v2Opts, "auth")
80 // Create authenticates to the identity service and attempts to acquire a Token.
81 // Generally, rather than interact with this call directly, end users should
82 // call openstack.AuthenticatedClient(), which abstracts all of the gory details
83 // about navigating service catalogs and such.
84 func Create(client *gophercloud.ServiceClient, auth AuthOptionsBuilder) (r CreateResult) {
85 b, err := auth.ToTokenV2CreateMap()
90 _, r.Err = client.Post(CreateURL(client), b, &r.Body, &gophercloud.RequestOpts{
91 OkCodes: []int{200, 203},
92 MoreHeaders: map[string]string{"X-Auth-Token": ""},
97 // Get validates and retrieves information for user's token.
98 func Get(client *gophercloud.ServiceClient, token string) (r GetResult) {
99 _, r.Err = client.Get(GetURL(client, token), &r.Body, &gophercloud.RequestOpts{
100 OkCodes: []int{200, 203},