File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed
main/java/io/trino/gateway/ha/security
test/java/io/trino/gateway/ha/security Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff 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 ) {
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments