Skip to content

Commit ea71974

Browse files
committed
Small polish
1 parent 61029e2 commit ea71974

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

src/Access/Authentication.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ bool Authentication::areCredentialsValid(
298298
const ClientInfo & client_info,
299299
SettingsChanges & settings)
300300
{
301+
/// It is OK for TokenCredentials to be not ready:
302+
/// When auth request happens, we do not even know the username.
303+
/// Token is resolved a bit later and the user information will be put in credentials
301304
if (!typeid_cast<const TokenCredentials *>(&credentials) && !credentials.isReady())
302305
return false;
303306

src/Access/Common/JWKSProvider.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ jwt::jwks<jwt::traits::kazuho_picojson> JWKSClient::getJWKS()
2121
auto now = std::chrono::high_resolution_clock::now();
2222
auto diff = std::chrono::duration<double>(now - last_request_send).count();
2323

24-
if (diff < refresh_timeout) {
24+
if (diff < refresh_timeout)
25+
{
2526
jwt::jwks <jwt::traits::kazuho_picojson> result(cached_jwks);
2627
return result;
2728
}
@@ -31,14 +32,18 @@ jwt::jwks<jwt::traits::kazuho_picojson> JWKSClient::getJWKS()
3132

3233
Poco::Net::HTTPRequest request{Poco::Net::HTTPRequest::HTTP_GET, jwks_uri.getPathAndQuery()};
3334

34-
if (jwks_uri.getScheme() == "https") {
35+
if (jwks_uri.getScheme() == "https")
36+
{
3537
Poco::Net::HTTPSClientSession session = Poco::Net::HTTPSClientSession(jwks_uri.getHost(), jwks_uri.getPort());
3638
session.sendRequest(request);
3739
std::istream & responseStream = session.receiveResponse(response);
3840
if (response.getStatus() != Poco::Net::HTTPResponse::HTTP_OK || !responseStream)
39-
throw Exception(ErrorCodes::AUTHENTICATION_FAILED, "Failed to get user info by access token, code: {}, reason: {}", response.getStatus(), response.getReason());
41+
throw Exception(ErrorCodes::AUTHENTICATION_FAILED, "Failed to get user info by access token, code: {}, reason: {}",
42+
response.getStatus(), response.getReason());
4043
Poco::StreamCopier::copyStream(responseStream, responseString);
41-
} else {
44+
}
45+
else
46+
{
4247
Poco::Net::HTTPClientSession session = Poco::Net::HTTPClientSession(jwks_uri.getHost(), jwks_uri.getPort());
4348
session.sendRequest(request);
4449
std::istream & responseStream = session.receiveResponse(response);
@@ -51,10 +56,12 @@ jwt::jwks<jwt::traits::kazuho_picojson> JWKSClient::getJWKS()
5156

5257
jwt::jwks<jwt::traits::kazuho_picojson> parsed_jwks;
5358

54-
try {
59+
try
60+
{
5561
parsed_jwks = jwt::parse_jwks(responseString.str());
5662
}
57-
catch (const Exception & e) {
63+
catch (const Exception & e)
64+
{
5865
throw Exception(ErrorCodes::AUTHENTICATION_FAILED, "Failed to parse JWKS: {}", e.what());
5966
}
6067

@@ -78,7 +85,8 @@ StaticJWKSParams::StaticJWKSParams(const std::string &static_jwks_, const std::s
7885
StaticJWKS::StaticJWKS(const StaticJWKSParams &params)
7986
{
8087
String content = String(params.static_jwks);
81-
if (!params.static_jwks_file.empty()) {
88+
if (!params.static_jwks_file.empty())
89+
{
8290
std::ifstream ifs(params.static_jwks_file);
8391
content = String((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
8492
}

src/Access/Common/JWKSProvider.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
namespace DB
1313
{
1414

15+
/// JWKS (JSON Web Key Set) is a kind of a set of public keys that are used to validate JWT authenticity locally.
16+
/// They are usually exposed by identity providers (e.g. Keycloak) via a well-known URI (usually /.well-known/jwks.json)
17+
/// This interface is responsible for managing JWKS. Retrieving, caching and refreshing of JWKS happens here.
18+
/// JWKS can either be static (e.g. provided in config) or dynamic (fetched from a remote URI and).
1519
class IJWKSProvider
1620
{
1721
public:

0 commit comments

Comments
 (0)