blob: 2ef427a87b19017eb20917e3c6575aacc14031e9 [file] [log] [blame]
Ash Wilson70dfe0c2014-08-28 13:57:09 -04001package gophercloud
2
Ash Wilson730a5062014-10-31 15:13:35 -04003/*
4AuthOptions stores information needed to authenticate to an OpenStack cluster.
5You can populate one manually, or use a provider's AuthOptionsFromEnv() function
6to read relevant information from the standard environment variables. Pass one
7to a provider's AuthenticatedClient function to authenticate and obtain a
8ProviderClient representing an active session on that provider.
9
10Its fields are the union of those recognized by each identity implementation and
11provider.
12*/
Ash Wilson70dfe0c2014-08-28 13:57:09 -040013type AuthOptions struct {
Jamie Hannafordb280dea2014-10-24 15:14:06 +020014 // IdentityEndpoint specifies the HTTP endpoint that is required to work with
Ash Wilson730a5062014-10-31 15:13:35 -040015 // the Identity API of the appropriate version. While it's ultimately needed by
16 // all of the identity services, it will often be populated by a provider-level
17 // function.
Jon Perrittdb0ae142016-03-13 00:33:41 -060018 IdentityEndpoint string `json:"-"`
Ash Wilson70dfe0c2014-08-28 13:57:09 -040019
Jamie Hannafordb280dea2014-10-24 15:14:06 +020020 // Username is required if using Identity V2 API. Consult with your provider's
21 // control panel to discover your account's username. In Identity V3, either
Ash Wilson730a5062014-10-31 15:13:35 -040022 // UserID or a combination of Username and DomainID or DomainName are needed.
Jon Perrittdb0ae142016-03-13 00:33:41 -060023 Username string `json:"username,omitempty"`
24 UserID string `json:"id,omitempty"`
Ash Wilson70dfe0c2014-08-28 13:57:09 -040025
Jon Perrittdb0ae142016-03-13 00:33:41 -060026 Password string `json:"password,omitempty"`
Ash Wilson70dfe0c2014-08-28 13:57:09 -040027
Jamie Hannafordb280dea2014-10-24 15:14:06 +020028 // At most one of DomainID and DomainName must be provided if using Username
29 // with Identity V3. Otherwise, either are optional.
Jon Perrittdb0ae142016-03-13 00:33:41 -060030 DomainID string `json:"id,omitempty"`
31 DomainName string `json:"name,omitempty"`
Ash Wilson70dfe0c2014-08-28 13:57:09 -040032
33 // The TenantID and TenantName fields are optional for the Identity V2 API.
34 // Some providers allow you to specify a TenantName instead of the TenantId.
Ash Wilson730a5062014-10-31 15:13:35 -040035 // Some require both. Your provider's authentication policies will determine
Ash Wilson70dfe0c2014-08-28 13:57:09 -040036 // how these fields influence authentication.
Jon Perrittdb0ae142016-03-13 00:33:41 -060037 TenantID string `json:"tenantId,omitempty"`
38 TenantName string `json:"tenantName,omitempty"`
Ash Wilson70dfe0c2014-08-28 13:57:09 -040039
40 // AllowReauth should be set to true if you grant permission for Gophercloud to
41 // cache your credentials in memory, and to allow Gophercloud to attempt to
42 // re-authenticate automatically if/when your token expires. If you set it to
43 // false, it will not cache these settings, but re-authentication will not be
44 // possible. This setting defaults to false.
jrperritt6e2ca002016-04-16 15:37:08 -050045 //
46 // NOTE: The reauth function will try to re-authenticate endlessly if left unchecked.
jrperritt9b7b9e62016-07-11 22:30:50 -050047 // The way to limit the number of attempts is to provide a custom HTTP client to the provider client
jrperritt6e2ca002016-04-16 15:37:08 -050048 // and provide a transport that implements the RoundTripper interface and stores the number of failed retries.
49 // For an example of this, see here: https://github.com/rackspace/rack/blob/1.0.0/auth/clients.go#L311
Ash Wilson70dfe0c2014-08-28 13:57:09 -040050 AllowReauth bool
jrperritt95b74c82015-07-28 20:39:27 -060051
jrperritt1f218c82015-07-29 08:54:18 -060052 // TokenID allows users to authenticate (possibly as another user) with an
53 // authentication token ID.
54 TokenID string
Ash Wilson70dfe0c2014-08-28 13:57:09 -040055}
jrperritt64d0ef02016-04-13 13:10:04 -050056
57// ToTokenV2CreateMap allows AuthOptions to satisfy the AuthOptionsBuilder
58// interface in the v2 tokens package
59func (opts AuthOptions) ToTokenV2CreateMap() (map[string]interface{}, error) {
60 // Populate the request map.
61 authMap := make(map[string]interface{})
62
63 if opts.Username != "" {
64 if opts.Password != "" {
65 authMap["passwordCredentials"] = map[string]interface{}{
66 "username": opts.Username,
67 "password": opts.Password,
68 }
69 } else {
70 return nil, ErrMissingInput{Argument: "Password"}
71 }
72 } else if opts.TokenID != "" {
73 authMap["token"] = map[string]interface{}{
74 "id": opts.TokenID,
75 }
76 } else {
77 return nil, ErrMissingInput{Argument: "Username"}
78 }
79
80 if opts.TenantID != "" {
81 authMap["tenantId"] = opts.TenantID
82 }
83 if opts.TenantName != "" {
84 authMap["tenantName"] = opts.TenantName
85 }
86
87 return map[string]interface{}{"auth": authMap}, nil
88}