|
57 | 57 | import java.time.Instant; |
58 | 58 | import java.util.Date; |
59 | 59 | import java.util.HashMap; |
| 60 | +import java.util.LinkedHashSet; |
60 | 61 | import java.util.List; |
61 | 62 | import java.util.Map; |
62 | 63 | import java.util.Set; |
@@ -1234,6 +1235,120 @@ void getOrCreateOidcUserUpdatesExistingUserFromAdminAndTeamClaims() throws Excep |
1234 | 1235 | } |
1235 | 1236 | } |
1236 | 1237 |
|
| 1238 | + @Test |
| 1239 | + void getOrCreateOidcUserAllowsSignupWhenAllowedDomainsEmpty() throws Exception { |
| 1240 | + AuthenticationCodeFlowHandler handler = newHandler(); |
| 1241 | + AuthenticationConfiguration authConfig = new AuthenticationConfiguration(); |
| 1242 | + authConfig.setEnableSelfSignup(true); |
| 1243 | + setField(handler, "authenticationConfiguration", authConfig); |
| 1244 | + AuthorizerConfiguration authorizer = authorizerConfiguration(Set.of()); |
| 1245 | + authorizer.setAllowedEmailRegistrationDomains(new LinkedHashSet<>()); |
| 1246 | + setField(handler, "authorizerConfiguration", authorizer); |
| 1247 | + |
| 1248 | + try (MockedStatic<Entity> entity = mockStatic(Entity.class); |
| 1249 | + MockedStatic<UserUtil> userUtil = mockStatic(UserUtil.class)) { |
| 1250 | + entity |
| 1251 | + .when( |
| 1252 | + () -> |
| 1253 | + Entity.getEntityByName( |
| 1254 | + Entity.USER, "gmail-user", "id,roles,teams", Include.NON_DELETED)) |
| 1255 | + .thenThrow(EntityNotFoundException.byName("gmail-user")); |
| 1256 | + User draftUser = new User(); |
| 1257 | + userUtil |
| 1258 | + .when(() -> UserUtil.user("gmail-user", "gmail.com", "gmail-user")) |
| 1259 | + .thenReturn(draftUser); |
| 1260 | + userUtil.when(() -> UserUtil.assignTeamsFromClaim(draftUser, List.of())).thenReturn(false); |
| 1261 | + User persistedUser = new User(); |
| 1262 | + persistedUser.setName("gmail-user"); |
| 1263 | + userUtil.when(() -> UserUtil.addOrUpdateUser(draftUser)).thenReturn(persistedUser); |
| 1264 | + |
| 1265 | + User user = |
| 1266 | + invokePrivate( |
| 1267 | + handler, |
| 1268 | + "getOrCreateOidcUser", |
| 1269 | + new Class<?>[] {String.class, String.class, Map.class}, |
| 1270 | + "gmail-user", |
| 1271 | + "gmail-user@gmail.com", |
| 1272 | + Map.of()); |
| 1273 | + |
| 1274 | + assertEquals(persistedUser, user); |
| 1275 | + } |
| 1276 | + } |
| 1277 | + |
| 1278 | + @Test |
| 1279 | + void getOrCreateOidcUserBlocksDisallowedDomain() throws Exception { |
| 1280 | + AuthenticationCodeFlowHandler handler = newHandler(); |
| 1281 | + AuthenticationConfiguration authConfig = new AuthenticationConfiguration(); |
| 1282 | + authConfig.setEnableSelfSignup(true); |
| 1283 | + setField(handler, "authenticationConfiguration", authConfig); |
| 1284 | + AuthorizerConfiguration authorizer = authorizerConfiguration(Set.of()); |
| 1285 | + authorizer.setAllowedEmailRegistrationDomains(Set.of("corp.com")); |
| 1286 | + setField(handler, "authorizerConfiguration", authorizer); |
| 1287 | + |
| 1288 | + try (MockedStatic<Entity> entity = mockStatic(Entity.class)) { |
| 1289 | + entity |
| 1290 | + .when( |
| 1291 | + () -> |
| 1292 | + Entity.getEntityByName( |
| 1293 | + Entity.USER, "outside-user", "id,roles,teams", Include.NON_DELETED)) |
| 1294 | + .thenThrow(EntityNotFoundException.byName("outside-user")); |
| 1295 | + |
| 1296 | + AuthenticationException exception = |
| 1297 | + assertThrows( |
| 1298 | + AuthenticationException.class, |
| 1299 | + () -> |
| 1300 | + invokePrivate( |
| 1301 | + handler, |
| 1302 | + "getOrCreateOidcUser", |
| 1303 | + new Class<?>[] {String.class, String.class, Map.class}, |
| 1304 | + "outside-user", |
| 1305 | + "outside-user@gmail.com", |
| 1306 | + Map.of())); |
| 1307 | + |
| 1308 | + assertTrue(exception.getMessage().contains("Email domain not allowed")); |
| 1309 | + } |
| 1310 | + } |
| 1311 | + |
| 1312 | + @Test |
| 1313 | + void getOrCreateOidcUserAllowsMatchingDomain() throws Exception { |
| 1314 | + AuthenticationCodeFlowHandler handler = newHandler(); |
| 1315 | + AuthenticationConfiguration authConfig = new AuthenticationConfiguration(); |
| 1316 | + authConfig.setEnableSelfSignup(true); |
| 1317 | + setField(handler, "authenticationConfiguration", authConfig); |
| 1318 | + AuthorizerConfiguration authorizer = authorizerConfiguration(Set.of()); |
| 1319 | + authorizer.setAllowedEmailRegistrationDomains(Set.of("gmail.com")); |
| 1320 | + setField(handler, "authorizerConfiguration", authorizer); |
| 1321 | + |
| 1322 | + try (MockedStatic<Entity> entity = mockStatic(Entity.class); |
| 1323 | + MockedStatic<UserUtil> userUtil = mockStatic(UserUtil.class)) { |
| 1324 | + entity |
| 1325 | + .when( |
| 1326 | + () -> |
| 1327 | + Entity.getEntityByName( |
| 1328 | + Entity.USER, "gmail-user", "id,roles,teams", Include.NON_DELETED)) |
| 1329 | + .thenThrow(EntityNotFoundException.byName("gmail-user")); |
| 1330 | + User draftUser = new User(); |
| 1331 | + userUtil |
| 1332 | + .when(() -> UserUtil.user("gmail-user", "gmail.com", "gmail-user")) |
| 1333 | + .thenReturn(draftUser); |
| 1334 | + userUtil.when(() -> UserUtil.assignTeamsFromClaim(draftUser, List.of())).thenReturn(false); |
| 1335 | + User persistedUser = new User(); |
| 1336 | + persistedUser.setName("gmail-user"); |
| 1337 | + userUtil.when(() -> UserUtil.addOrUpdateUser(draftUser)).thenReturn(persistedUser); |
| 1338 | + |
| 1339 | + User user = |
| 1340 | + invokePrivate( |
| 1341 | + handler, |
| 1342 | + "getOrCreateOidcUser", |
| 1343 | + new Class<?>[] {String.class, String.class, Map.class}, |
| 1344 | + "gmail-user", |
| 1345 | + "gmail-user@gmail.com", |
| 1346 | + Map.of()); |
| 1347 | + |
| 1348 | + assertEquals(persistedUser, user); |
| 1349 | + } |
| 1350 | + } |
| 1351 | + |
1237 | 1352 | @Test |
1238 | 1353 | void handleCallbackReturnsErrorWhenProviderRespondsWithAuthenticationError() throws Exception { |
1239 | 1354 | AuthenticationCodeFlowHandler handler = newHandler(); |
|
0 commit comments