blob: ec52f0b190c47e62f8dacac91aad1846310db7cf [file] [log] [blame]
Ash Wilson70dfe0c2014-08-28 13:57:09 -04001package gophercloud
2
Ash Wilson730a5062014-10-31 15:13:35 -04003/*
Jon Perrittdb0ae142016-03-13 00:33:41 -06004type AuthOptionsBuilder interface {
5 ToTokenCreateMap() (map[string]interface{}, error)
6}
7*/
8
9/*
Ash Wilson730a5062014-10-31 15:13:35 -040010AuthOptions stores information needed to authenticate to an OpenStack cluster.
11You can populate one manually, or use a provider's AuthOptionsFromEnv() function
12to read relevant information from the standard environment variables. Pass one
13to a provider's AuthenticatedClient function to authenticate and obtain a
14ProviderClient representing an active session on that provider.
15
16Its fields are the union of those recognized by each identity implementation and
17provider.
18*/
Ash Wilson70dfe0c2014-08-28 13:57:09 -040019type AuthOptions struct {
Jamie Hannafordb280dea2014-10-24 15:14:06 +020020 // IdentityEndpoint specifies the HTTP endpoint that is required to work with
Ash Wilson730a5062014-10-31 15:13:35 -040021 // the Identity API of the appropriate version. While it's ultimately needed by
22 // all of the identity services, it will often be populated by a provider-level
23 // function.
Jon Perrittdb0ae142016-03-13 00:33:41 -060024 IdentityEndpoint string `json:"-"`
Ash Wilson70dfe0c2014-08-28 13:57:09 -040025
Jamie Hannafordb280dea2014-10-24 15:14:06 +020026 // Username is required if using Identity V2 API. Consult with your provider's
27 // control panel to discover your account's username. In Identity V3, either
Ash Wilson730a5062014-10-31 15:13:35 -040028 // UserID or a combination of Username and DomainID or DomainName are needed.
Jon Perrittdb0ae142016-03-13 00:33:41 -060029 Username string `json:"username,omitempty"`
30 UserID string `json:"id,omitempty"`
Ash Wilson70dfe0c2014-08-28 13:57:09 -040031
Jon Perrittdb0ae142016-03-13 00:33:41 -060032 Password string `json:"password,omitempty"`
Ash Wilson70dfe0c2014-08-28 13:57:09 -040033
Jamie Hannafordb280dea2014-10-24 15:14:06 +020034 // At most one of DomainID and DomainName must be provided if using Username
35 // with Identity V3. Otherwise, either are optional.
Jon Perrittdb0ae142016-03-13 00:33:41 -060036 DomainID string `json:"id,omitempty"`
37 DomainName string `json:"name,omitempty"`
Ash Wilson70dfe0c2014-08-28 13:57:09 -040038
39 // The TenantID and TenantName fields are optional for the Identity V2 API.
40 // Some providers allow you to specify a TenantName instead of the TenantId.
Ash Wilson730a5062014-10-31 15:13:35 -040041 // Some require both. Your provider's authentication policies will determine
Ash Wilson70dfe0c2014-08-28 13:57:09 -040042 // how these fields influence authentication.
Jon Perrittdb0ae142016-03-13 00:33:41 -060043 TenantID string `json:"tenantId,omitempty"`
44 TenantName string `json:"tenantName,omitempty"`
Ash Wilson70dfe0c2014-08-28 13:57:09 -040045
46 // AllowReauth should be set to true if you grant permission for Gophercloud to
47 // cache your credentials in memory, and to allow Gophercloud to attempt to
48 // re-authenticate automatically if/when your token expires. If you set it to
49 // false, it will not cache these settings, but re-authentication will not be
50 // possible. This setting defaults to false.
Jon Perrittdb0ae142016-03-13 00:33:41 -060051 AllowReauth bool `json:"-"`
jrperritt95b74c82015-07-28 20:39:27 -060052
jrperritt1f218c82015-07-29 08:54:18 -060053 // TokenID allows users to authenticate (possibly as another user) with an
54 // authentication token ID.
55 TokenID string
Ash Wilson70dfe0c2014-08-28 13:57:09 -040056}
Jon Perrittdb0ae142016-03-13 00:33:41 -060057
58// ToTokenV2CreateMap allows AuthOptions to satisfy the AuthOptionsBuilder
59// interface in the v2 tokens package
60func (opts AuthOptions) ToTokenV2CreateMap() (map[string]interface{}, error) {
61 v2Opts := AuthOptionsV2{
62 PasswordCredentials: &PasswordCredentialsV2{
63 Username: opts.Username,
64 Password: opts.Password,
65 },
66 TenantID: opts.TenantID,
67 TenantName: opts.TenantName,
68 TokenCredentials: &TokenCredentialsV2{
69 ID: opts.TokenID,
70 },
71 }
72
73 b, err := BuildRequestBody(v2Opts, "auth")
74 if err != nil {
75 return nil, err
76 }
77 /*
78 if opts.TokenID == "" {
79 delete(b["auth"].(map[string]interface{}), "token")
80 return b, nil
81 }
82
83 delete(b["auth"].(map[string]interface{}), "passwordCredentials")*/
84 return b, nil
85}
86
87func (opts AuthOptions) ToTokenV3CreateMap(scope *ScopeOptsV3) (map[string]interface{}, error) {
88 var methods []string
89 if opts.TokenID != "" {
90 methods = []string{"token"}
91 } else {
92 methods = []string{"password"}
93 }
94
95 v3Opts := AuthOptionsV3{
96 Identity: &IdentityCredentialsV3{
97 Methods: methods,
98 PasswordCredentials: &PasswordCredentialsV3{
99 User: &UserV3{
100 ID: opts.UserID,
101 Name: opts.Username,
102 Password: opts.Password,
103 Domain: &DomainV3{
104 ID: opts.DomainID,
105 Name: opts.DomainName,
106 },
107 },
108 },
109 TokenCredentials: &TokenCredentialsV3{
110 ID: opts.TokenID,
111 },
112 },
113 }
114
115 if scope != nil {
116 v3Opts.Scope = &ScopeV3{
117 Domain: &ScopeDomainV3{
118 ID: scope.DomainID,
119 Name: scope.DomainName,
120 },
121 Project: &ScopeProjectV3{
122 Domain: &ScopeProjectDomainV3{
123 ID: scope.DomainID,
124 Name: scope.DomainName,
125 },
126 ID: scope.ProjectID,
127 Name: scope.ProjectName,
128 },
129 }
130 }
131
132 b, err := BuildRequestBody(v3Opts, "auth")
133 if err != nil {
134 return nil, err
135 }
136 /*
137 if opts.TokenID == "" {
138 delete(b["auth"].(map[string]interface{}), "token")
139 return b, nil
140 }
141
142 delete(b["auth"].(map[string]interface{}), "passwordCredentials")*/
143 return b, nil
144}