1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2017-2020 Couchbase, Inc.
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #ifndef LCB_AUTH_PRIV_H
19 #define LCB_AUTH_PRIV_H
20 #include <libcouchbase/auth.h>
21 
22 #ifdef __cplusplus
23 #include <string>
24 #include <map>
25 
26 struct lcbauth_CREDENTIALS_ {
hostnamelcbauth_CREDENTIALS_27     void hostname(std::string hostname)
28     {
29         hostname_ = std::move(hostname);
30     }
31 
portlcbauth_CREDENTIALS_32     void port(std::string port)
33     {
34         port_ = std::move(port);
35     }
36 
bucketlcbauth_CREDENTIALS_37     void bucket(std::string bucket)
38     {
39         bucket_ = std::move(bucket);
40     }
41 
usernamelcbauth_CREDENTIALS_42     void username(std::string username)
43     {
44         username_ = std::move(username);
45     }
46 
passwordlcbauth_CREDENTIALS_47     void password(std::string password)
48     {
49         password_ = std::move(password);
50     }
51 
resultlcbauth_CREDENTIALS_52     void result(lcbauth_RESULT result)
53     {
54         result_ = result;
55     }
56 
reasonlcbauth_CREDENTIALS_57     void reason(lcbauth_REASON reason)
58     {
59         reason_ = reason;
60     }
61 
cookielcbauth_CREDENTIALS_62     void cookie(void *cookie)
63     {
64         cookie_ = cookie;
65     }
66 
cookielcbauth_CREDENTIALS_67     void *cookie() const
68     {
69         return cookie_;
70     }
71 
usernamelcbauth_CREDENTIALS_72     const std::string &username() const
73     {
74         return username_;
75     }
76 
passwordlcbauth_CREDENTIALS_77     const std::string &password() const
78     {
79         return password_;
80     }
81 
hostnamelcbauth_CREDENTIALS_82     const std::string &hostname() const
83     {
84         return hostname_;
85     }
86 
portlcbauth_CREDENTIALS_87     const std::string &port() const
88     {
89         return port_;
90     }
91 
bucketlcbauth_CREDENTIALS_92     const std::string &bucket() const
93     {
94         return bucket_;
95     }
96 
resultlcbauth_CREDENTIALS_97     lcbauth_RESULT result() const
98     {
99         return result_;
100     }
101 
reasonlcbauth_CREDENTIALS_102     lcbauth_REASON reason() const
103     {
104         return reason_;
105     }
106 
servicelcbauth_CREDENTIALS_107     lcbauth_SERVICE service() const
108     {
109         return service_;
110     }
111 
servicelcbauth_CREDENTIALS_112     void service(lcbauth_SERVICE service)
113     {
114         service_ = service;
115     }
116 
117   private:
118     void *cookie_{nullptr};
119     std::string hostname_{};
120     std::string port_{};
121     std::string bucket_{};
122     lcbauth_REASON reason_{LCBAUTH_REASON_NEW_OPERATION};
123     lcbauth_SERVICE service_{LCBAUTH_SERVICE_UNSPECIFIED};
124 
125     /* output */
126     lcbauth_RESULT result_{LCBAUTH_RESULT_OK};
127     std::string username_{};
128     std::string password_{};
129 };
130 
131 namespace lcb
132 {
133 class Authenticator
134 {
135   public:
136     Authenticator() = default;
Authenticator(const Authenticator &other)137     Authenticator(const Authenticator &other)
138         : buckets_{other.buckets_}, username_{other.username_}, password_{other.password_}, mode_{other.mode_},
139           cookie_{other.cookie_}, callback_{other.callback_}
140     {
141     }
142 
143     // Gets the "global" username
username() const144     const std::string &username() const
145     {
146         return username_;
147     }
148 
149     // Gets the "global" password
password() const150     const std::string &password() const
151     {
152         return password_;
153     }
154 
155     lcbauth_CREDENTIALS credentials_for(lcbauth_SERVICE service, lcbauth_REASON reason, const char *host,
156                                         const char *port, const char *bucket) const;
157 
buckets() const158     const std::map<std::string, std::string> &buckets() const
159     {
160         return buckets_;
161     }
162 
refcount() const163     size_t refcount() const
164     {
165         return refcount_;
166     }
167 
incref()168     void incref()
169     {
170         ++refcount_;
171     }
172 
decref()173     void decref()
174     {
175         if (!--refcount_) {
176             delete this;
177         }
178     }
179 
set_mode(lcbauth_MODE mode)180     lcb_STATUS set_mode(lcbauth_MODE mode)
181     {
182         if (mode_ == LCBAUTH_MODE_DYNAMIC && callback_ == nullptr) {
183             return LCB_ERR_INVALID_ARGUMENT;
184         }
185         if (buckets_.size() || username_.size() || password_.size()) {
186             return LCB_ERR_INVALID_ARGUMENT;
187         } else {
188             mode_ = mode;
189             return LCB_SUCCESS;
190         }
191     }
mode() const192     lcbauth_MODE mode() const
193     {
194         return mode_;
195     }
196     lcb_STATUS add(const char *user, const char *pass, int flags);
add(const std::string &user, const std::string &pass, int flags)197     lcb_STATUS add(const std::string &user, const std::string &pass, int flags)
198     {
199         return add(user.c_str(), pass.c_str(), flags);
200     }
set_callback(void *cookie, void (*callback)(lcbauth_CREDENTIALS *))201     lcb_STATUS set_callback(void *cookie, void (*callback)(lcbauth_CREDENTIALS *))
202     {
203         cookie_ = cookie;
204         callback_ = callback;
205         return LCB_SUCCESS;
206     }
207 
208   private:
209     size_t refcount_{1};
210 
211     std::map<std::string, std::string> buckets_{};
212     std::string username_;
213     std::string password_;
214     lcbauth_MODE mode_{LCBAUTH_MODE_CLASSIC};
215     void *cookie_{nullptr};
callback_(lcbauth_CREDENTIALS *)216     void (*callback_)(lcbauth_CREDENTIALS *){nullptr};
217 };
218 } // namespace lcb
219 #endif
220 #endif /* LCB_AUTH_H */
221