Skip to content

Commit ab0b1fe

Browse files
committed
reduce amount of const_cast
1 parent ea71974 commit ab0b1fe

File tree

5 files changed

+29
-38
lines changed

5 files changed

+29
-38
lines changed

src/Access/ExternalAuthenticators.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ HTTPAuthClientParams ExternalAuthenticators::getHTTPAuthenticationParams(const S
602602
}
603603

604604
bool ExternalAuthenticators::checkCredentialsAgainstProcessor(const ITokenProcessor & processor,
605-
const TokenCredentials & credentials) const
605+
TokenCredentials & credentials) const
606606
{
607607
if (processor.resolveAndValidate(credentials))
608608
{
@@ -675,12 +675,12 @@ bool ExternalAuthenticators::checkTokenCredentials(const TokenCredentials & cred
675675
{
676676
for (const auto & it: token_processors)
677677
{
678-
if (checkCredentialsAgainstProcessor(*it.second, credentials))
678+
if (checkCredentialsAgainstProcessor(*it.second, const_cast<TokenCredentials &>(credentials)))
679679
return true;
680680
}
681681
}
682682
else
683-
return token_processors.contains(processor_name) && checkCredentialsAgainstProcessor(*token_processors[processor_name], credentials);
683+
return token_processors.contains(processor_name) && checkCredentialsAgainstProcessor(*token_processors[processor_name], const_cast<TokenCredentials &>(credentials));
684684

685685
return false;
686686
}

src/Access/ExternalAuthenticators.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class ExternalAuthenticators
8888
mutable UsernameToTokenCache username_to_access_token_cache TSA_GUARDED_BY(mutex) ;
8989

9090
bool checkCredentialsAgainstProcessor(const ITokenProcessor & processor,
91-
const TokenCredentials & credentials) const TSA_REQUIRES(mutex);
91+
TokenCredentials & credentials) const TSA_REQUIRES(mutex);
9292

9393
void resetImpl() TSA_REQUIRES(mutex);
9494
};

src/Access/TokenProcessors.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class ITokenProcessor
2020
: processor_name(processor_name_), token_cache_lifetime(token_cache_lifetime_), username_claim(username_claim_), groups_claim(groups_claim_) {}
2121
virtual ~ITokenProcessor() = default;
2222

23-
virtual bool resolveAndValidate(const TokenCredentials & credentials) const = 0;
23+
virtual bool resolveAndValidate(TokenCredentials & credentials) const = 0;
2424

2525
virtual bool checkClaims(const TokenCredentials &, const String &) { return true; }
2626

@@ -57,7 +57,7 @@ class StaticKeyJwtProcessor : public ITokenProcessor
5757
const String & public_key_password,
5858
const String & private_key_password);
5959

60-
bool resolveAndValidate(const TokenCredentials & credentials) const override;
60+
bool resolveAndValidate(TokenCredentials & credentials) const override;
6161
bool checkClaims(const TokenCredentials & credentials, const String & claims_to_check) override;
6262

6363
private:
@@ -95,7 +95,7 @@ class JwksJwtProcessor : public ITokenProcessor
9595
verifier_leeway_,
9696
std::make_shared<JWKSClient>(jwks_uri_, jwks_cache_lifetime_)) {}
9797

98-
bool resolveAndValidate(const TokenCredentials & credentials) const override;
98+
bool resolveAndValidate(TokenCredentials & credentials) const override;
9999
bool checkClaims(const TokenCredentials & credentials, const String & claims_to_check) override;
100100

101101
private:
@@ -116,7 +116,7 @@ class GoogleTokenProcessor : public ITokenProcessor
116116
const String & groups_claim_)
117117
: ITokenProcessor(processor_name_, token_cache_lifetime_, username_claim_, groups_claim_) {}
118118

119-
bool resolveAndValidate(const TokenCredentials & credentials) const override;
119+
bool resolveAndValidate(TokenCredentials & credentials) const override;
120120
};
121121

122122
class AzureTokenProcessor : public ITokenProcessor
@@ -128,7 +128,7 @@ class AzureTokenProcessor : public ITokenProcessor
128128
const String & groups_claim_)
129129
: ITokenProcessor(processor_name_, token_cache_lifetime_, username_claim_, groups_claim_) {}
130130

131-
bool resolveAndValidate(const TokenCredentials & credentials) const override;
131+
bool resolveAndValidate(TokenCredentials & credentials) const override;
132132
};
133133

134134
class OpenIdTokenProcessor : public ITokenProcessor
@@ -154,7 +154,7 @@ class OpenIdTokenProcessor : public ITokenProcessor
154154
UInt64 verifier_leeway_,
155155
UInt64 jwks_cache_lifetime_);
156156

157-
bool resolveAndValidate(const TokenCredentials & credentials) const override;
157+
bool resolveAndValidate(TokenCredentials & credentials) const override;
158158
private:
159159
Poco::URI userinfo_endpoint;
160160
Poco::URI token_introspection_endpoint;

src/Access/TokenProcessorsJWT.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ bool JwksJwtProcessor::checkClaims(const TokenCredentials & credentials, const S
280280
return checkUserClaims(credentials, claims_to_check);
281281
}
282282

283-
bool StaticKeyJwtProcessor::resolveAndValidate(const TokenCredentials & credentials) const
283+
bool StaticKeyJwtProcessor::resolveAndValidate(TokenCredentials & credentials) const
284284
{
285285
try
286286
{
@@ -296,10 +296,10 @@ bool StaticKeyJwtProcessor::resolveAndValidate(const TokenCredentials & credenti
296296
return false;
297297
}
298298

299-
const_cast<TokenCredentials &>(credentials).setUserName(decoded_jwt.get_payload_claim(username_claim).as_string());
299+
credentials.setUserName(decoded_jwt.get_payload_claim(username_claim).as_string());
300300

301301
if (decoded_jwt.has_payload_claim(groups_claim))
302-
const_cast<TokenCredentials &>(credentials).setGroups(parseGroupsFromJsonArray(decoded_jwt.get_payload_claim(groups_claim).as_array()));
302+
credentials.setGroups(parseGroupsFromJsonArray(decoded_jwt.get_payload_claim(groups_claim).as_array()));
303303
else
304304
LOG_TRACE(getLogger("TokenAuthentication"), "{}: Specified groups_claim {} not found in token, no external roles will be mapped", processor_name, groups_claim);
305305

@@ -312,7 +312,7 @@ bool StaticKeyJwtProcessor::resolveAndValidate(const TokenCredentials & credenti
312312
}
313313
}
314314

315-
bool JwksJwtProcessor::resolveAndValidate(const TokenCredentials & credentials) const
315+
bool JwksJwtProcessor::resolveAndValidate(TokenCredentials & credentials) const
316316
{
317317
auto decoded_jwt = jwt::decode(credentials.getToken());
318318

@@ -383,10 +383,10 @@ bool JwksJwtProcessor::resolveAndValidate(const TokenCredentials & credentials)
383383
if (!claims.empty() && !check_claims(claims, decoded_jwt.get_payload_json()))
384384
return false;
385385

386-
const_cast<TokenCredentials &>(credentials).setUserName(decoded_jwt.get_payload_claim(username_claim).as_string());
386+
credentials.setUserName(decoded_jwt.get_payload_claim(username_claim).as_string());
387387

388388
if (decoded_jwt.has_payload_claim(groups_claim))
389-
const_cast<TokenCredentials &>(credentials).setGroups(parseGroupsFromJsonArray(decoded_jwt.get_payload_claim(groups_claim).as_array()));
389+
credentials.setGroups(parseGroupsFromJsonArray(decoded_jwt.get_payload_claim(groups_claim).as_array()));
390390
else
391391
LOG_TRACE(getLogger("TokenAuthentication"), "{}: Specified groups_claim {} not found in token, no external roles will be mapped", processor_name, groups_claim);
392392

src/Access/TokenProcessorsOpaque.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ namespace
8989
}
9090
}
9191

92-
bool GoogleTokenProcessor::resolveAndValidate(const TokenCredentials & credentials) const
92+
bool GoogleTokenProcessor::resolveAndValidate(TokenCredentials & credentials) const
9393
{
9494
const String & token = credentials.getToken();
9595

@@ -106,14 +106,11 @@ bool GoogleTokenProcessor::resolveAndValidate(const TokenCredentials & credentia
106106

107107
String user_name = user_info[username_claim];
108108

109-
110-
/// Credentials are passed as const everywhere up the flow, so we have to comply,
111-
/// in this case const_cast looks acceptable.
112-
const_cast<TokenCredentials &>(credentials).setUserName(user_name);
109+
credentials.setUserName(user_name);
113110

114111
auto token_info = getObjectFromURI(Poco::URI("https://www.googleapis.com/oauth2/v3/tokeninfo"), token);
115112
if (token_info.contains("exp"))
116-
const_cast<TokenCredentials &>(credentials).setExpiresAt(std::chrono::system_clock::from_time_t((getValueByKey<time_t>(token_info, "exp").value())));
113+
credentials.setExpiresAt(std::chrono::system_clock::from_time_t((getValueByKey<time_t>(token_info, "exp").value())));
117114

118115
/// Groups info can only be retrieved if user email is known.
119116
/// If no email found in user info, we skip this step and there are no external roles for the user.
@@ -152,7 +149,7 @@ bool GoogleTokenProcessor::resolveAndValidate(const TokenCredentials & credentia
152149
}
153150
}
154151

155-
const_cast<TokenCredentials &>(credentials).setGroups(external_groups_names);
152+
credentials.setGroups(external_groups_names);
156153
}
157154
catch (const Exception & e)
158155
{
@@ -166,7 +163,7 @@ bool GoogleTokenProcessor::resolveAndValidate(const TokenCredentials & credentia
166163
return true;
167164
}
168165

169-
bool AzureTokenProcessor::resolveAndValidate(const TokenCredentials & credentials) const
166+
bool AzureTokenProcessor::resolveAndValidate(TokenCredentials & credentials) const
170167
{
171168
/// Token is a JWT in this case, but we cannot directly verify it against Azure AD JWKS.
172169
/// We will not trust user data in this token except for 'exp' value to determine caching duration.
@@ -180,12 +177,9 @@ bool AzureTokenProcessor::resolveAndValidate(const TokenCredentials & credential
180177
{
181178
picojson::object user_info_json = getObjectFromURI(Poco::URI("https://graph.microsoft.com/oidc/userinfo"), token);
182179
String username = getValueByKey(user_info_json, username_claim).value();
180+
183181
if (!username.empty())
184-
{
185-
/// Credentials are passed as const everywhere up the flow, so we have to comply,
186-
/// in this case const_cast looks acceptable.
187-
const_cast<TokenCredentials &>(credentials).setUserName(username);
188-
}
182+
credentials.setUserName(username);
189183
else
190184
LOG_TRACE(getLogger("TokenAuthentication"), "{}: Failed to get username with token", processor_name);
191185

@@ -197,7 +191,7 @@ bool AzureTokenProcessor::resolveAndValidate(const TokenCredentials & credential
197191

198192
try
199193
{
200-
const_cast<TokenCredentials &>(credentials).setExpiresAt(jwt::decode(token).get_expires_at());
194+
credentials.setExpiresAt(jwt::decode(token).get_expires_at());
201195
}
202196
catch (...) {
203197
LOG_TRACE(getLogger("TokenAuthentication"),
@@ -250,8 +244,7 @@ bool AzureTokenProcessor::resolveAndValidate(const TokenCredentials & credential
250244
return true;
251245
}
252246

253-
const_cast<TokenCredentials &>(credentials).setGroups(external_groups_names);
254-
247+
credentials.setGroups(external_groups_names);
255248
return true;
256249
}
257250

@@ -309,7 +302,7 @@ OpenIdTokenProcessor::OpenIdTokenProcessor(const String & processor_name_,
309302
}
310303
}
311304

312-
bool OpenIdTokenProcessor::resolveAndValidate(const TokenCredentials & credentials) const
305+
bool OpenIdTokenProcessor::resolveAndValidate(TokenCredentials & credentials) const
313306
{
314307
const String & token = credentials.getToken();
315308
String username;
@@ -325,7 +318,7 @@ bool OpenIdTokenProcessor::resolveAndValidate(const TokenCredentials & credentia
325318

326319
/// TODO: Now we work only with Keycloak -- and it provides expires_at in token itself. Need to add actual token introspection logic for other OIDC providers.
327320
if (decoded_token.has_expires_at())
328-
const_cast<TokenCredentials &>(credentials).setExpiresAt(decoded_token.get_expires_at());
321+
credentials.setExpiresAt(decoded_token.get_expires_at());
329322
}
330323
catch (const std::exception & ex)
331324
{
@@ -359,9 +352,7 @@ bool OpenIdTokenProcessor::resolveAndValidate(const TokenCredentials & credentia
359352
return false;
360353
}
361354

362-
/// Credentials are passed as const everywhere up the flow, so we have to comply,
363-
/// in this case const_cast is acceptable.
364-
const_cast<TokenCredentials &>(credentials).setUserName(username);
355+
credentials.setUserName(username);
365356

366357
/// For now, list of groups is expected in a claim with specified name either in token itself or in userinfo response (Keycloak works this way)
367358
/// TODO: add support for custom endpoints for retrieving groups. Keycloak lists groups in /userinfo and token itself, which is not always the case.
@@ -382,7 +373,7 @@ bool OpenIdTokenProcessor::resolveAndValidate(const TokenCredentials & credentia
382373
if (group.is<std::string>())
383374
external_groups_names.insert(group.get<std::string>());
384375
}
385-
const_cast<TokenCredentials &>(credentials).setGroups(external_groups_names);
376+
credentials.setGroups(external_groups_names);
386377
}
387378

388379
return true;

0 commit comments

Comments
 (0)