OIDCUtil { public List<DirectoryGroup> getGroupsFromAAD(String ssoToken, String groupName) { JsonParser jsonParser = new JsonParser(); HttpClient httpClient = httpUtils.getHttpClient(); List<DirectoryGroup> 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, "getGroupsFromAAD"). put(LogMessage.MESSAGE, "Failed to initialize httpClient"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return allGroups; } String filterSearch = "$filter=startsWith%28displayName%2C'"+groupName+"'%29"; String api = ssoGroupsEndpoint + filterSearch; HttpGet getRequest = new HttpGet(api); getRequest.addHeader("accept", TVaultConstants.HTTP_CONTENT_TYPE_JSON); getRequest.addHeader("Authorization", "Bearer " + ssoToken); 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 = new HashSet<>(); for (int i=0;i<vaulesArray.size();i++) { JsonObject adObject = vaulesArray.get(i).getAsJsonObject(); groupNamesSet.add(adObject.get("displayName").getAsString()); } for (String group: groupNamesSet) { DirectoryGroup directoryGroup = new DirectoryGroup(); directoryGroup.setDisplayName(group); directoryGroup.setGroupName(group); directoryGroup.setEmail(null); allGroups.add(directoryGroup); } } } log.debug(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getGroupsFromAAD"). put(LogMessage.MESSAGE, String.format("Retrieved %d group(s) from AAD", allGroups.size())). 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, "getGroupsFromAAD"). 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, "getGroupsFromAAD"). 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_getGroupsFromAAD_success() throws Exception { String group = "testgroup"; 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\":\"testgroup1\"}]}"; when(mockHttpEntity.getContent()).thenReturn( new ByteArrayInputStream(groupResponseString.getBytes())); ReflectionTestUtils.setField(oidcUtil, "ssoGroupsEndpoint", "testgroupurl"); List<DirectoryGroup> groups = oidcUtil.getGroupsFromAAD("testssotoken", group); assertEquals(1, groups.size()); assertEquals("testgroup1", groups.get(0).getDisplayName()); }
@Test public void test_getGroupsFromAAD_failed() throws Exception { String group = "testgroup"; 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, "ssoGroupsEndpoint", "testgroupurl"); List<DirectoryGroup> groups = oidcUtil.getGroupsFromAAD("testssotoken", group); assertEquals(0, groups.size()); }