Skip to content

Commit 52a1d2a

Browse files
satheeshmsebyhr
authored andcommitted
Add null check for userId field
1 parent b40f087 commit 52a1d2a

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

gateway-ha/src/main/java/io/trino/gateway/ha/security/LbAuthenticator.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ public Optional<LbPrincipal> authenticate(String idToken)
5151
Optional<String> privilegesField = oauthManager.getPrivilegesField();
5252
if (privilegesField.isPresent()) {
5353
Map<String, Claim> claims = oauthManager.getClaimsFromIdToken(idToken).orElseThrow();
54-
String userId = claims.get(userIdField).asString().replace("\"", "");
54+
Claim userIdClaim = claims.get(userIdField);
55+
if (userIdClaim == null) {
56+
log.error("Required userId field %s not found", userIdField);
57+
throw new AuthenticationException("UserId field does not exist");
58+
}
59+
String userId = userIdClaim.asString().replace("\"", "");
5560

5661
Claim claim = claims.get(privilegesField.orElseThrow());
5762
if (claim == null) {

gateway-ha/src/test/java/io/trino/gateway/ha/security/TestLbAuthenticator.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,26 @@ void testAuthenticatorMissingClaim()
176176
assertThat(lbAuth.authenticate(ID_TOKEN)).isEmpty();
177177
}
178178

179+
@Test
180+
void testAuthenticatorUserIdFieldNotExist()
181+
{
182+
Claim claim = Mockito.mock(Claim.class);
183+
AuthorizationManager authorization = Mockito.mock(AuthorizationManager.class);
184+
LbOAuthManager authentication = Mockito.mock(LbOAuthManager.class);
185+
186+
Mockito.when(authentication.getClaimsFromIdToken(ID_TOKEN))
187+
.thenReturn(Optional.of(Map.of("no-sub", claim)));
188+
Mockito.when(authentication.getUserIdField())
189+
.thenReturn("sub");
190+
Mockito.when(authentication.getPrivilegesField())
191+
.thenReturn(Optional.of("role_list"));
192+
193+
LbAuthenticator lbAuth = new LbAuthenticator(authentication, authorization);
194+
195+
assertThatThrownBy(() -> lbAuth.authenticate(ID_TOKEN))
196+
.hasMessageStartingWith("UserId field does not exist");
197+
}
198+
179199
@Test
180200
void testPresetUsers()
181201
throws Exception

0 commit comments

Comments
 (0)