Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Couchbase/ClusterOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ public function authenticatorHash(): string
return hash("sha256", sprintf("--%s--%s--", $exported['username'], $exported['password']));
} elseif ($exported['type'] == 'certificate') {
return hash("sha256", sprintf("--%s--%s--", $exported['certificatePath'], $exported['keyPath']));
} elseif ($exported['type'] == 'jwt') {
return hash("sha256", sprintf("--%s--", $exported['token']));
} else {
throw new InvalidArgumentException("unknown type of the authenticator: " . $exported['type']);
}
Expand Down
39 changes: 39 additions & 0 deletions Couchbase/JwtAuthenticator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* Copyright 2014-Present Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types=1);

namespace Couchbase;

Class JwtAuthenticator implements Authenticator
{
private string $token;

public function __construct(string $token)
{
$this->token = $token;
}

public function export(): array
{
return [
'type' => 'jwt',
'token' => $this->token,
];
}
}
2 changes: 1 addition & 1 deletion src/deps/couchbase-cxx-client
35 changes: 35 additions & 0 deletions src/wrapper/connection_handle.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,21 @@ connection_handle::authenticator_set(const zval* auth) -> core_error_info
}
return {};
}
if (zend_binary_strcmp(Z_STRVAL_P(auth_type), Z_STRLEN_P(auth_type), ZEND_STRL("jwt")) == 0) {
const zval* token = zend_symtable_str_find(Z_ARRVAL_P(auth), ZEND_STRL("token"));
if (token == nullptr || Z_TYPE_P(token) != IS_STRING) {
return { errc::common::invalid_argument,
ERROR_LOCATION,
"expected jwt to be a string in the authenticator" };
}
couchbase::jwt_authenticator jwt_auth{ Z_STRVAL_P(token) };

auto ctx = impl_->public_api().set_authenticator(jwt_auth);
if (ctx.ec()) {
return { ctx.ec(), ERROR_LOCATION, "unable to set authenticator", build_error_context(ctx) };
}
return {};
}
return { errc::common::invalid_argument,
ERROR_LOCATION,
fmt::format("unknown type of the authenticator: {}",
Expand Down Expand Up @@ -6439,6 +6454,26 @@ construct_cluster_options(zval* options)
},
};
}
if (zend_binary_strcmp(Z_STRVAL_P(auth_type), Z_STRLEN_P(auth_type), ZEND_STRL("jwt")) ==
0) {
const zval* token = zend_symtable_str_find(Z_ARRVAL_P(auth), ZEND_STRL("token"));
if (token == nullptr || Z_TYPE_P(token) != IS_STRING) {
return {
{ errc::common::invalid_argument,
ERROR_LOCATION,
"expected jwt token to be a string in the authenticator" },
{},
};
}
return {
{},
cluster_options{
jwt_authenticator{
Z_STRVAL_P(token),
},
},
};
}
return {
{ errc::common::invalid_argument,
ERROR_LOCATION,
Expand Down