blob: 922a2798d7955f4a38ab16c7fdfe3d5fd3233517 [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.
Jon Perrittdb0ae142016-03-13 00:33:41 -060045 AllowReauth bool `json:"-"`
jrperritt95b74c82015-07-28 20:39:27 -060046
jrperritt1f218c82015-07-29 08:54:18 -060047 // TokenID allows users to authenticate (possibly as another user) with an
48 // authentication token ID.
49 TokenID string
Ash Wilson70dfe0c2014-08-28 13:57:09 -040050}
jrperritt64d0ef02016-04-13 13:10:04 -050051
52// ToTokenV2CreateMap allows AuthOptions to satisfy the AuthOptionsBuilder
53// interface in the v2 tokens package
54func (opts AuthOptions) ToTokenV2CreateMap() (map[string]interface{}, error) {
55 // Populate the request map.
56 authMap := make(map[string]interface{})
57
58 if opts.Username != "" {
59 if opts.Password != "" {
60 authMap["passwordCredentials"] = map[string]interface{}{
61 "username": opts.Username,
62 "password": opts.Password,
63 }
64 } else {
65 return nil, ErrMissingInput{Argument: "Password"}
66 }
67 } else if opts.TokenID != "" {
68 authMap["token"] = map[string]interface{}{
69 "id": opts.TokenID,
70 }
71 } else {
72 return nil, ErrMissingInput{Argument: "Username"}
73 }
74
75 if opts.TenantID != "" {
76 authMap["tenantId"] = opts.TenantID
77 }
78 if opts.TenantName != "" {
79 authMap["tenantName"] = opts.TenantName
80 }
81
82 return map[string]interface{}{"auth": authMap}, nil
83}