OIDCUtil { public List<String> getSelfServiceGroupsFromAADById(String accessToken, String userAADId, String userName) { JsonParser jsonParser = new JsonParser(); HttpClient httpClient = httpUtils.getHttpClient(); List<String> allGroups = new ArrayList<>(); if (httpClient == null) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, SSLCertificateConstants.GET_SELF_SERVICE_GROUPS_STRING) .put(LogMessage.MESSAGE, "Failed to initialize httpClient") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return allGroups; } String api = ssoGetUserEndpoint + userAADId + ssoGetUserGroups; HttpGet getRequest = new HttpGet(api); getRequest.addHeader("Authorization", "Bearer " + accessToken); String output = ""; StringBuilder jsonResponse = new StringBuilder(); try { HttpResponse apiResponse = httpClient.execute(getRequest); if (apiResponse.getStatusLine().getStatusCode() == 200) { BufferedReader br = new BufferedReader(new InputStreamReader((apiResponse.getEntity().getContent()))); while ((output = br.readLine()) != null) { jsonResponse.append(output); } JsonObject responseJson = (JsonObject) jsonParser.parse(jsonResponse.toString()); if (responseJson != null && responseJson.has("value")) { JsonArray vaulesArray = responseJson.get("value").getAsJsonArray(); if (vaulesArray.size() > 0) { Set<String> groupNamesSet = getGroupNameFromJsonArray(vaulesArray); if (!isUserSelfServiceAdmin(groupNamesSet)) { log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, SSLCertificateConstants.GET_SELF_SERVICE_GROUPS_STRING) .put(LogMessage.MESSAGE, "No self-service groups available") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)) .build())); return allGroups; } getMatchedSelfServiceGroups(allGroups, groupNamesSet); } } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, SSLCertificateConstants.GET_SELF_SERVICE_GROUPS_STRING) .put(LogMessage.MESSAGE, String.format("Retrieved %d group(s) from AAD for user %s", allGroups.size(), userName)) .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); return allGroups; } log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, SSLCertificateConstants.GET_SELF_SERVICE_GROUPS_STRING) .put(LogMessage.MESSAGE, "Failed to retrieve groups from AAD") .put(LogMessage.STATUS, String.valueOf(apiResponse.getStatusLine().getStatusCode())) .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); } catch (IOException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder() .put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)) .put(LogMessage.ACTION, SSLCertificateConstants.GET_SELF_SERVICE_GROUPS_STRING) .put(LogMessage.MESSAGE, "Failed to parse AAD groups api response") .put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)).build())); } return allGroups; } 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; }
@Test public void test_getSelfServiceGroupsFromAADById_success() throws Exception { String userAADId = "abcdefg"; String userName = "testuser"; Response response = new Response(); response.setHttpstatus(HttpStatus.OK); response.setSuccess(true); response.setResponse(null); when(httpUtils.getHttpClient()).thenReturn(httpClient); when(httpClient.execute(any())).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(200); when(httpResponse.getEntity()).thenReturn(mockHttpEntity); String groupResponseString = "{\"value\": [ {\"id\": \"abcdefg\", \"onPremisesSyncEnabled\":null, \"displayName\":\"r_selfservice_tvt_admin\"}]}"; when(mockHttpEntity.getContent()).thenReturn(new ByteArrayInputStream(groupResponseString.getBytes())); ReflectionTestUtils.setField(oidcUtil, "ssoGetUserEndpoint", "testgroupurl"); ReflectionTestUtils.setField(oidcUtil, "ssoGetUserGroups", "groupurlvalue"); ReflectionTestUtils.setField(oidcUtil, "ssoGroupPattern", "r_selfservice_[a-z]{3}_admin"); List<String> groups = oidcUtil.getSelfServiceGroupsFromAADById("testssotoken", userAADId, userName); assertEquals(1, groups.size()); assertEquals("tvt", groups.get(0)); }
@Test public void test_getSelfServiceGroupsFromAADById_failed() throws Exception { String userAADId = "abcdefg"; String userName = "testuser"; Response response = new Response(); response.setHttpstatus(HttpStatus.OK); response.setSuccess(true); response.setResponse(null); when(httpUtils.getHttpClient()).thenReturn(httpClient); when(httpClient.execute(any())).thenReturn(httpResponse); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(400); ReflectionTestUtils.setField(oidcUtil, "ssoGetUserEndpoint", "testgroupurl"); ReflectionTestUtils.setField(oidcUtil, "ssoGetUserGroups", "groupurlvalue"); ReflectionTestUtils.setField(oidcUtil, "ssoGroupPattern", "r_selfservice_[a-z]{3}_admin"); List<String> groups = oidcUtil.getSelfServiceGroupsFromAADById("testssotoken", userAADId, userName); assertEquals(0, groups.size()); }