@@ -179,18 +179,21 @@ cb_Backend_allocate(VALUE klass)
179179}
180180
181181auto
182- construct_authenticator (VALUE credentials)
183- -> std::variant<couchbase::password_authenticator, couchbase::certificate_authenticator>
182+ construct_authenticator (VALUE credentials) -> std::variant<couchbase::password_authenticator,
183+ couchbase::certificate_authenticator,
184+ couchbase::jwt_authenticator>
184185{
185186 cb_check_type (credentials, T_HASH);
186187
187188 static const auto sym_certificate_path{ rb_id2sym (rb_intern (" certificate_path" )) };
188189 static const auto sym_key_path{ rb_id2sym (rb_intern (" key_path" )) };
190+ static const auto sym_jwt{ rb_id2sym (rb_intern (" jwt" )) };
189191
190192 const VALUE certificate_path = rb_hash_aref (credentials, sym_certificate_path);
191193 const VALUE key_path = rb_hash_aref (credentials, sym_key_path);
194+ const VALUE jwt = rb_hash_aref (credentials, sym_jwt);
192195
193- if (NIL_P (certificate_path) || NIL_P (key_path)) {
196+ if (NIL_P (certificate_path) && NIL_P (key_path) && NIL_P (jwt )) {
194197 static const auto sym_username = rb_id2sym (rb_intern (" username" ));
195198 static const auto sym_password = rb_id2sym (rb_intern (" password" ));
196199
@@ -206,27 +209,45 @@ construct_authenticator(VALUE credentials)
206209 };
207210 }
208211
209- cb_check_type (certificate_path, T_STRING);
210- cb_check_type (key_path, T_STRING);
212+ if (NIL_P (jwt)) {
213+ cb_check_type (certificate_path, T_STRING);
214+ cb_check_type (key_path, T_STRING);
211215
212- return couchbase::certificate_authenticator{
213- cb_string_new (certificate_path),
214- cb_string_new (key_path),
216+ return couchbase::certificate_authenticator{
217+ cb_string_new (certificate_path),
218+ cb_string_new (key_path),
219+ };
220+ }
221+
222+ cb_check_type (jwt, T_STRING);
223+ return couchbase::jwt_authenticator{
224+ cb_string_new (jwt),
215225 };
216226}
217227
218228auto
219229construct_cluster_options (VALUE credentials, bool tls_enabled) -> couchbase::cluster_options
220230{
221- std::variant<couchbase::password_authenticator, couchbase::certificate_authenticator>
222- authenticator = construct_authenticator (credentials);
231+ auto authenticator = construct_authenticator (credentials);
223232
224233 if (std::holds_alternative<couchbase::password_authenticator>(authenticator)) {
225234 return couchbase::cluster_options{
226235 std::get<couchbase::password_authenticator>(std::move (authenticator)),
227236 };
228237 }
229238
239+ if (std::holds_alternative<couchbase::jwt_authenticator>(authenticator)) {
240+ if (!tls_enabled) {
241+ throw ruby_exception (
242+ exc_invalid_argument (),
243+ " JWT authenticator requires TLS connection, check the connection string" );
244+ }
245+
246+ return couchbase::cluster_options{
247+ std::get<couchbase::jwt_authenticator>(std::move (authenticator)),
248+ };
249+ }
250+
230251 if (!tls_enabled) {
231252 throw ruby_exception (
232253 exc_invalid_argument (),
@@ -572,13 +593,15 @@ cb_Backend_update_credentials(VALUE self, VALUE credentials)
572593 auto cluster = cb_backend_to_public_api_cluster (self);
573594
574595 try {
575- std::variant<couchbase::password_authenticator, couchbase::certificate_authenticator>
576- authenticator = construct_authenticator (credentials);
596+ auto authenticator = construct_authenticator (credentials);
577597
578598 couchbase::error err{};
579599 if (std::holds_alternative<couchbase::password_authenticator>(authenticator)) {
580600 err = cluster.set_authenticator (
581601 std::get<couchbase::password_authenticator>(std::move (authenticator)));
602+ } else if (std::holds_alternative<couchbase::jwt_authenticator>(authenticator)) {
603+ err =
604+ cluster.set_authenticator (std::get<couchbase::jwt_authenticator>(std::move (authenticator)));
582605 } else {
583606 err = cluster.set_authenticator (
584607 std::get<couchbase::certificate_authenticator>(std::move (authenticator)));
0 commit comments