@Test public void oidcFetchEntityDetailsSuccess(){ ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body("{\"messages\":[\"Success\"]}"); String token = "qwqwdsfsf"; String accessor = "testUser"; String username = "testUser"; DirectoryUser directoryUser = new DirectoryUser(); directoryUser.setDisplayName("testUser"); directoryUser.setGivenName("testUser"); directoryUser.setUserEmail("testUser@t-mobile.com"); directoryUser.setUserId("testuser01"); directoryUser.setUserName("testUser"); List<DirectoryUser> persons = new ArrayList<>(); persons.add(directoryUser); DirectoryObjects users = new DirectoryObjects(); DirectoryObjectsList usersList = new DirectoryObjectsList(); usersList.setValues(persons.toArray(new DirectoryUser[persons.size()])); users.setData(usersList); ResponseEntity<DirectoryObjects> responseEntity1 = ResponseEntity.status(HttpStatus.OK).body(users); String dataOutput = "{\"data\":{\"oidc/\":{\"accessor\":\"auth_oidc_8b51f292\",\"config\":{\"default_lease_ttl\":0,\"force_no_cache\":false,\"max_lease_ttl\":0,\"token_type\":\"default-service\"},\"description\":\"\",\"external_entropy_access\":false,\"local\":false,\"options\":null,\"seal_wrap\":false,\"type\":\"oidc\",\"uuid\":\"fbd45cc4-d6b6-8b49-6d1a-d4d931345df9\"}}}"; Response responsemock = getMockResponse(HttpStatus.OK, true, dataOutput); when(reqProcessor.process(eq("/sys/list"),Mockito.any(),eq(token))).thenReturn(responsemock); OIDCLookupEntityRequest oidcLookupEntityRequest = new OIDCLookupEntityRequest(); oidcLookupEntityRequest.setAlias_name("alias_name"); oidcLookupEntityRequest.setAlias_mount_accessor("alias_mount_accessor"); String jsonStr = JSONUtil.getJSON(oidcLookupEntityRequest); String authMountResponse = "{\"data\":{\"name\":\"entity_63f119d2\",\"policies\":[\"default\"]}}"; Response response = getMockResponse(HttpStatus.OK, true, authMountResponse); when(reqProcessor.process("/identity/lookup/entity", jsonStr, token)).thenReturn(response); when(reqProcessor.process("/auth/tvault/lookup", "{}", token)).thenReturn(response); when(directoryService.searchByCorpId(username)).thenReturn(responseEntity1); ResponseEntity<OIDCEntityResponse> oiEntity = oidcUtil.oidcFetchEntityDetails(token, username, null); assertEquals(oiEntity.getStatusCode(), responseEntityExpected.getStatusCode()); }
public ResponseEntity<OIDCEntityResponse> oidcFetchEntityDetails(String token, String username, UserDetails userDetails) { String mountAccessor = fetchMountAccessorForOidc(token); if (!StringUtils.isEmpty(mountAccessor)) { ResponseEntity<DirectoryObjects> response = directoryService.searchByCorpId(username); String aliasName = ""; Object[] results = response.getBody().getData().getValues(); for (Object tp : results) { aliasName = ((DirectoryUser) tp).getUserEmail(); } OIDCLookupEntityRequest oidcLookupEntityRequest = new OIDCLookupEntityRequest(); oidcLookupEntityRequest.setAlias_name(aliasName); oidcLookupEntityRequest.setAlias_mount_accessor(mountAccessor); ResponseEntity<OIDCEntityResponse> entityResponseResponseEntity = entityLookUp(token, oidcLookupEntityRequest); if (!HttpStatus.OK.equals(entityResponseResponseEntity.getStatusCode())) { OidcEntityAliasRequest oidcEntityAliasRequest = new OidcEntityAliasRequest(aliasName, mountAccessor); Response createEntityAliasResponse = createEntityAlias(token, oidcEntityAliasRequest); if (HttpStatus.OK.equals(createEntityAliasResponse.getHttpstatus())) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "oidcFetchEntityDetails"). put(LogMessage.MESSAGE, String.format("Created entity alias for [%s]", aliasName)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); entityResponseResponseEntity = entityLookUp(token, oidcLookupEntityRequest); } else { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "oidcFetchEntityDetails"). put(LogMessage.MESSAGE, String.format("Failed to created entity alias for [%s]", aliasName)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(createEntityAliasResponse.getHttpstatus()).body(new OIDCEntityResponse()); } } List<String> combinedPolicyList = new ArrayList<>(); List<String> policies = entityResponseResponseEntity.getBody().getPolicies(); if (policies!=null) { combinedPolicyList.addAll(policies); } List<String> policiesFromToken; if (userDetails != null && username.equalsIgnoreCase(userDetails.getUsername())) { policiesFromToken = tokenLookUp(userDetails.getClientToken()); combinedPolicyList.addAll(policiesFromToken); } List<String> policiesWithOutDuplicates = combinedPolicyList.stream().distinct().collect(Collectors.toList()); entityResponseResponseEntity.getBody().setPolicies(policiesWithOutDuplicates); return entityResponseResponseEntity; } return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new OIDCEntityResponse()); }
OIDCUtil { public ResponseEntity<OIDCEntityResponse> oidcFetchEntityDetails(String token, String username, UserDetails userDetails) { String mountAccessor = fetchMountAccessorForOidc(token); if (!StringUtils.isEmpty(mountAccessor)) { ResponseEntity<DirectoryObjects> response = directoryService.searchByCorpId(username); String aliasName = ""; Object[] results = response.getBody().getData().getValues(); for (Object tp : results) { aliasName = ((DirectoryUser) tp).getUserEmail(); } OIDCLookupEntityRequest oidcLookupEntityRequest = new OIDCLookupEntityRequest(); oidcLookupEntityRequest.setAlias_name(aliasName); oidcLookupEntityRequest.setAlias_mount_accessor(mountAccessor); ResponseEntity<OIDCEntityResponse> entityResponseResponseEntity = entityLookUp(token, oidcLookupEntityRequest); if (!HttpStatus.OK.equals(entityResponseResponseEntity.getStatusCode())) { OidcEntityAliasRequest oidcEntityAliasRequest = new OidcEntityAliasRequest(aliasName, mountAccessor); Response createEntityAliasResponse = createEntityAlias(token, oidcEntityAliasRequest); if (HttpStatus.OK.equals(createEntityAliasResponse.getHttpstatus())) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "oidcFetchEntityDetails"). put(LogMessage.MESSAGE, String.format("Created entity alias for [%s]", aliasName)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); entityResponseResponseEntity = entityLookUp(token, oidcLookupEntityRequest); } else { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "oidcFetchEntityDetails"). put(LogMessage.MESSAGE, String.format("Failed to created entity alias for [%s]", aliasName)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(createEntityAliasResponse.getHttpstatus()).body(new OIDCEntityResponse()); } } List<String> combinedPolicyList = new ArrayList<>(); List<String> policies = entityResponseResponseEntity.getBody().getPolicies(); if (policies!=null) { combinedPolicyList.addAll(policies); } List<String> policiesFromToken; if (userDetails != null && username.equalsIgnoreCase(userDetails.getUsername())) { policiesFromToken = tokenLookUp(userDetails.getClientToken()); combinedPolicyList.addAll(policiesFromToken); } List<String> policiesWithOutDuplicates = combinedPolicyList.stream().distinct().collect(Collectors.toList()); entityResponseResponseEntity.getBody().setPolicies(policiesWithOutDuplicates); return entityResponseResponseEntity; } return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new OIDCEntityResponse()); } }
OIDCUtil { public ResponseEntity<OIDCEntityResponse> oidcFetchEntityDetails(String token, String username, UserDetails userDetails) { String mountAccessor = fetchMountAccessorForOidc(token); if (!StringUtils.isEmpty(mountAccessor)) { ResponseEntity<DirectoryObjects> response = directoryService.searchByCorpId(username); String aliasName = ""; Object[] results = response.getBody().getData().getValues(); for (Object tp : results) { aliasName = ((DirectoryUser) tp).getUserEmail(); } OIDCLookupEntityRequest oidcLookupEntityRequest = new OIDCLookupEntityRequest(); oidcLookupEntityRequest.setAlias_name(aliasName); oidcLookupEntityRequest.setAlias_mount_accessor(mountAccessor); ResponseEntity<OIDCEntityResponse> entityResponseResponseEntity = entityLookUp(token, oidcLookupEntityRequest); if (!HttpStatus.OK.equals(entityResponseResponseEntity.getStatusCode())) { OidcEntityAliasRequest oidcEntityAliasRequest = new OidcEntityAliasRequest(aliasName, mountAccessor); Response createEntityAliasResponse = createEntityAlias(token, oidcEntityAliasRequest); if (HttpStatus.OK.equals(createEntityAliasResponse.getHttpstatus())) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "oidcFetchEntityDetails"). put(LogMessage.MESSAGE, String.format("Created entity alias for [%s]", aliasName)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); entityResponseResponseEntity = entityLookUp(token, oidcLookupEntityRequest); } else { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "oidcFetchEntityDetails"). put(LogMessage.MESSAGE, String.format("Failed to created entity alias for [%s]", aliasName)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(createEntityAliasResponse.getHttpstatus()).body(new OIDCEntityResponse()); } } List<String> combinedPolicyList = new ArrayList<>(); List<String> policies = entityResponseResponseEntity.getBody().getPolicies(); if (policies!=null) { combinedPolicyList.addAll(policies); } List<String> policiesFromToken; if (userDetails != null && username.equalsIgnoreCase(userDetails.getUsername())) { policiesFromToken = tokenLookUp(userDetails.getClientToken()); combinedPolicyList.addAll(policiesFromToken); } List<String> policiesWithOutDuplicates = combinedPolicyList.stream().distinct().collect(Collectors.toList()); entityResponseResponseEntity.getBody().setPolicies(policiesWithOutDuplicates); return entityResponseResponseEntity; } return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new OIDCEntityResponse()); } OIDCUtil(); }
OIDCUtil { public ResponseEntity<OIDCEntityResponse> oidcFetchEntityDetails(String token, String username, UserDetails userDetails) { String mountAccessor = fetchMountAccessorForOidc(token); if (!StringUtils.isEmpty(mountAccessor)) { ResponseEntity<DirectoryObjects> response = directoryService.searchByCorpId(username); String aliasName = ""; Object[] results = response.getBody().getData().getValues(); for (Object tp : results) { aliasName = ((DirectoryUser) tp).getUserEmail(); } OIDCLookupEntityRequest oidcLookupEntityRequest = new OIDCLookupEntityRequest(); oidcLookupEntityRequest.setAlias_name(aliasName); oidcLookupEntityRequest.setAlias_mount_accessor(mountAccessor); ResponseEntity<OIDCEntityResponse> entityResponseResponseEntity = entityLookUp(token, oidcLookupEntityRequest); if (!HttpStatus.OK.equals(entityResponseResponseEntity.getStatusCode())) { OidcEntityAliasRequest oidcEntityAliasRequest = new OidcEntityAliasRequest(aliasName, mountAccessor); Response createEntityAliasResponse = createEntityAlias(token, oidcEntityAliasRequest); if (HttpStatus.OK.equals(createEntityAliasResponse.getHttpstatus())) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "oidcFetchEntityDetails"). put(LogMessage.MESSAGE, String.format("Created entity alias for [%s]", aliasName)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); entityResponseResponseEntity = entityLookUp(token, oidcLookupEntityRequest); } else { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "oidcFetchEntityDetails"). put(LogMessage.MESSAGE, String.format("Failed to created entity alias for [%s]", aliasName)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(createEntityAliasResponse.getHttpstatus()).body(new OIDCEntityResponse()); } } List<String> combinedPolicyList = new ArrayList<>(); List<String> policies = entityResponseResponseEntity.getBody().getPolicies(); if (policies!=null) { combinedPolicyList.addAll(policies); } List<String> policiesFromToken; if (userDetails != null && username.equalsIgnoreCase(userDetails.getUsername())) { policiesFromToken = tokenLookUp(userDetails.getClientToken()); combinedPolicyList.addAll(policiesFromToken); } List<String> policiesWithOutDuplicates = combinedPolicyList.stream().distinct().collect(Collectors.toList()); entityResponseResponseEntity.getBody().setPolicies(policiesWithOutDuplicates); return entityResponseResponseEntity; } return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new OIDCEntityResponse()); } OIDCUtil(); String fetchMountAccessorForOidc(String token); OIDCEntityResponse getEntityLookUpResponse(String authMountResponse); Response updateGroupPolicies(String token, String groupName, List<String> policies,
			List<String> currentPolicies, String id); Response deleteGroupByName(String token, String name); OIDCGroup getIdentityGroupDetails(String groupName, String token); String getSSOToken(); String getGroupObjectResponse(String ssoToken, String groupName); String updateIdentityGroupByName(String token, OIDCIdentityGroupRequest oidcIdentityGroupRequest); Response createGroupAlias(String token, GroupAliasRequest groupAliasRequest); ResponseEntity<OIDCEntityResponse> oidcFetchEntityDetails(String token, String username, UserDetails userDetails); ResponseEntity<OIDCEntityResponse> entityLookUp(String token,
			OIDCLookupEntityRequest oidcLookupEntityRequest); Response deleteGroupAliasByID(String token, String id); Response updateOIDCEntity(List<String> policies, String entityName); Response updateEntityByName(String token, OIDCEntityRequest oidcEntityRequest); void renewUserToken(String token); List<String> getPoliciedFromTokenLookUp(String authMountResponse); List<String> tokenLookUp(String token); Response createEntityAlias(String token, OidcEntityAliasRequest oidcEntityAliasRequest); String getUserName(String email); List<DirectoryGroup> getGroupsFromAAD(String ssoToken, String groupName); String getIdOfTheUser(String accessToken, String userEmail); List<String> getSelfServiceGroupsFromAADById(String accessToken, String userAADId, String userName); }
OIDCUtil { public ResponseEntity<OIDCEntityResponse> oidcFetchEntityDetails(String token, String username, UserDetails userDetails) { String mountAccessor = fetchMountAccessorForOidc(token); if (!StringUtils.isEmpty(mountAccessor)) { ResponseEntity<DirectoryObjects> response = directoryService.searchByCorpId(username); String aliasName = ""; Object[] results = response.getBody().getData().getValues(); for (Object tp : results) { aliasName = ((DirectoryUser) tp).getUserEmail(); } OIDCLookupEntityRequest oidcLookupEntityRequest = new OIDCLookupEntityRequest(); oidcLookupEntityRequest.setAlias_name(aliasName); oidcLookupEntityRequest.setAlias_mount_accessor(mountAccessor); ResponseEntity<OIDCEntityResponse> entityResponseResponseEntity = entityLookUp(token, oidcLookupEntityRequest); if (!HttpStatus.OK.equals(entityResponseResponseEntity.getStatusCode())) { OidcEntityAliasRequest oidcEntityAliasRequest = new OidcEntityAliasRequest(aliasName, mountAccessor); Response createEntityAliasResponse = createEntityAlias(token, oidcEntityAliasRequest); if (HttpStatus.OK.equals(createEntityAliasResponse.getHttpstatus())) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "oidcFetchEntityDetails"). put(LogMessage.MESSAGE, String.format("Created entity alias for [%s]", aliasName)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); entityResponseResponseEntity = entityLookUp(token, oidcLookupEntityRequest); } else { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "oidcFetchEntityDetails"). put(LogMessage.MESSAGE, String.format("Failed to created entity alias for [%s]", aliasName)). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(createEntityAliasResponse.getHttpstatus()).body(new OIDCEntityResponse()); } } List<String> combinedPolicyList = new ArrayList<>(); List<String> policies = entityResponseResponseEntity.getBody().getPolicies(); if (policies!=null) { combinedPolicyList.addAll(policies); } List<String> policiesFromToken; if (userDetails != null && username.equalsIgnoreCase(userDetails.getUsername())) { policiesFromToken = tokenLookUp(userDetails.getClientToken()); combinedPolicyList.addAll(policiesFromToken); } List<String> policiesWithOutDuplicates = combinedPolicyList.stream().distinct().collect(Collectors.toList()); entityResponseResponseEntity.getBody().setPolicies(policiesWithOutDuplicates); return entityResponseResponseEntity; } return ResponseEntity.status(HttpStatus.FORBIDDEN).body(new OIDCEntityResponse()); } OIDCUtil(); String fetchMountAccessorForOidc(String token); OIDCEntityResponse getEntityLookUpResponse(String authMountResponse); Response updateGroupPolicies(String token, String groupName, List<String> policies,
			List<String> currentPolicies, String id); Response deleteGroupByName(String token, String name); OIDCGroup getIdentityGroupDetails(String groupName, String token); String getSSOToken(); String getGroupObjectResponse(String ssoToken, String groupName); String updateIdentityGroupByName(String token, OIDCIdentityGroupRequest oidcIdentityGroupRequest); Response createGroupAlias(String token, GroupAliasRequest groupAliasRequest); ResponseEntity<OIDCEntityResponse> oidcFetchEntityDetails(String token, String username, UserDetails userDetails); ResponseEntity<OIDCEntityResponse> entityLookUp(String token,
			OIDCLookupEntityRequest oidcLookupEntityRequest); Response deleteGroupAliasByID(String token, String id); Response updateOIDCEntity(List<String> policies, String entityName); Response updateEntityByName(String token, OIDCEntityRequest oidcEntityRequest); void renewUserToken(String token); List<String> getPoliciedFromTokenLookUp(String authMountResponse); List<String> tokenLookUp(String token); Response createEntityAlias(String token, OidcEntityAliasRequest oidcEntityAliasRequest); String getUserName(String email); List<DirectoryGroup> getGroupsFromAAD(String ssoToken, String groupName); String getIdOfTheUser(String accessToken, String userEmail); List<String> getSelfServiceGroupsFromAADById(String accessToken, String userAADId, String userName); static final Logger log; }