@Test public void test_getSSOToken_success() throws Exception { 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 responseString = "{\"access_token\": \"abcd\"}"; when(mockHttpEntity.getContent()).thenReturn( new ByteArrayInputStream(responseString.getBytes())); ReflectionTestUtils.setField(oidcUtil, "ssoGroupsEndpoint", "testgroupurl"); when(ControllerUtil.getOidcADLoginUrl()).thenReturn("testurl"); String responseJson = "{\"data\":{\"objectId\": \"abcdefg\"}}"; ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(responseJson); String actualToken = oidcUtil.getSSOToken(); assertNotNull(actualToken); }
public String getSSOToken() { JsonParser jsonParser = new JsonParser(); HttpClient httpClient = httpUtils.getHttpClient(); String accessToken = ""; if (httpClient == null) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to initialize httpClient"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } String api = ControllerUtil.getOidcADLoginUrl(); HttpPost postRequest = new HttpPost(api); postRequest.addHeader("Content-type", TVaultConstants.HTTP_CONTENT_TYPE_URL_ENCODED); postRequest.addHeader("Accept",TVaultConstants.HTTP_CONTENT_TYPE_JSON); List<NameValuePair> form = new ArrayList<>(); form.add(new BasicNameValuePair("grant_type", "client_credentials")); form.add(new BasicNameValuePair("client_id", ControllerUtil.getOidcClientId())); form.add(new BasicNameValuePair("client_secret", ControllerUtil.getOidcClientSecret())); form.add(new BasicNameValuePair("resource", ssoResourceEndpoint)); UrlEncodedFormEntity entity; try { entity = new UrlEncodedFormEntity(form); } catch (UnsupportedEncodingException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to encode entity"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } postRequest.setEntity(entity); String output; StringBuilder jsonResponse = new StringBuilder(); try { HttpResponse apiResponse = httpClient.execute(postRequest); if (apiResponse.getStatusLine().getStatusCode() != 200) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to get sso token"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } 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.isJsonNull() && responseJson.has("access_token")) { accessToken = responseJson.get("access_token").getAsString(); } return accessToken; } catch (IOException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to parse SSO response"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); } return null; }
OIDCUtil { public String getSSOToken() { JsonParser jsonParser = new JsonParser(); HttpClient httpClient = httpUtils.getHttpClient(); String accessToken = ""; if (httpClient == null) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to initialize httpClient"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } String api = ControllerUtil.getOidcADLoginUrl(); HttpPost postRequest = new HttpPost(api); postRequest.addHeader("Content-type", TVaultConstants.HTTP_CONTENT_TYPE_URL_ENCODED); postRequest.addHeader("Accept",TVaultConstants.HTTP_CONTENT_TYPE_JSON); List<NameValuePair> form = new ArrayList<>(); form.add(new BasicNameValuePair("grant_type", "client_credentials")); form.add(new BasicNameValuePair("client_id", ControllerUtil.getOidcClientId())); form.add(new BasicNameValuePair("client_secret", ControllerUtil.getOidcClientSecret())); form.add(new BasicNameValuePair("resource", ssoResourceEndpoint)); UrlEncodedFormEntity entity; try { entity = new UrlEncodedFormEntity(form); } catch (UnsupportedEncodingException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to encode entity"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } postRequest.setEntity(entity); String output; StringBuilder jsonResponse = new StringBuilder(); try { HttpResponse apiResponse = httpClient.execute(postRequest); if (apiResponse.getStatusLine().getStatusCode() != 200) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to get sso token"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } 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.isJsonNull() && responseJson.has("access_token")) { accessToken = responseJson.get("access_token").getAsString(); } return accessToken; } catch (IOException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to parse SSO response"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); } return null; } }
OIDCUtil { public String getSSOToken() { JsonParser jsonParser = new JsonParser(); HttpClient httpClient = httpUtils.getHttpClient(); String accessToken = ""; if (httpClient == null) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to initialize httpClient"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } String api = ControllerUtil.getOidcADLoginUrl(); HttpPost postRequest = new HttpPost(api); postRequest.addHeader("Content-type", TVaultConstants.HTTP_CONTENT_TYPE_URL_ENCODED); postRequest.addHeader("Accept",TVaultConstants.HTTP_CONTENT_TYPE_JSON); List<NameValuePair> form = new ArrayList<>(); form.add(new BasicNameValuePair("grant_type", "client_credentials")); form.add(new BasicNameValuePair("client_id", ControllerUtil.getOidcClientId())); form.add(new BasicNameValuePair("client_secret", ControllerUtil.getOidcClientSecret())); form.add(new BasicNameValuePair("resource", ssoResourceEndpoint)); UrlEncodedFormEntity entity; try { entity = new UrlEncodedFormEntity(form); } catch (UnsupportedEncodingException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to encode entity"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } postRequest.setEntity(entity); String output; StringBuilder jsonResponse = new StringBuilder(); try { HttpResponse apiResponse = httpClient.execute(postRequest); if (apiResponse.getStatusLine().getStatusCode() != 200) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to get sso token"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } 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.isJsonNull() && responseJson.has("access_token")) { accessToken = responseJson.get("access_token").getAsString(); } return accessToken; } catch (IOException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to parse SSO response"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); } return null; } OIDCUtil(); }
OIDCUtil { public String getSSOToken() { JsonParser jsonParser = new JsonParser(); HttpClient httpClient = httpUtils.getHttpClient(); String accessToken = ""; if (httpClient == null) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to initialize httpClient"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } String api = ControllerUtil.getOidcADLoginUrl(); HttpPost postRequest = new HttpPost(api); postRequest.addHeader("Content-type", TVaultConstants.HTTP_CONTENT_TYPE_URL_ENCODED); postRequest.addHeader("Accept",TVaultConstants.HTTP_CONTENT_TYPE_JSON); List<NameValuePair> form = new ArrayList<>(); form.add(new BasicNameValuePair("grant_type", "client_credentials")); form.add(new BasicNameValuePair("client_id", ControllerUtil.getOidcClientId())); form.add(new BasicNameValuePair("client_secret", ControllerUtil.getOidcClientSecret())); form.add(new BasicNameValuePair("resource", ssoResourceEndpoint)); UrlEncodedFormEntity entity; try { entity = new UrlEncodedFormEntity(form); } catch (UnsupportedEncodingException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to encode entity"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } postRequest.setEntity(entity); String output; StringBuilder jsonResponse = new StringBuilder(); try { HttpResponse apiResponse = httpClient.execute(postRequest); if (apiResponse.getStatusLine().getStatusCode() != 200) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to get sso token"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } 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.isJsonNull() && responseJson.has("access_token")) { accessToken = responseJson.get("access_token").getAsString(); } return accessToken; } catch (IOException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to parse SSO response"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); } return null; } 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 String getSSOToken() { JsonParser jsonParser = new JsonParser(); HttpClient httpClient = httpUtils.getHttpClient(); String accessToken = ""; if (httpClient == null) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to initialize httpClient"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } String api = ControllerUtil.getOidcADLoginUrl(); HttpPost postRequest = new HttpPost(api); postRequest.addHeader("Content-type", TVaultConstants.HTTP_CONTENT_TYPE_URL_ENCODED); postRequest.addHeader("Accept",TVaultConstants.HTTP_CONTENT_TYPE_JSON); List<NameValuePair> form = new ArrayList<>(); form.add(new BasicNameValuePair("grant_type", "client_credentials")); form.add(new BasicNameValuePair("client_id", ControllerUtil.getOidcClientId())); form.add(new BasicNameValuePair("client_secret", ControllerUtil.getOidcClientSecret())); form.add(new BasicNameValuePair("resource", ssoResourceEndpoint)); UrlEncodedFormEntity entity; try { entity = new UrlEncodedFormEntity(form); } catch (UnsupportedEncodingException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to encode entity"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } postRequest.setEntity(entity); String output; StringBuilder jsonResponse = new StringBuilder(); try { HttpResponse apiResponse = httpClient.execute(postRequest); if (apiResponse.getStatusLine().getStatusCode() != 200) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to get sso token"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return null; } 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.isJsonNull() && responseJson.has("access_token")) { accessToken = responseJson.get("access_token").getAsString(); } return accessToken; } catch (IOException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "getSSOToken"). put(LogMessage.MESSAGE, "Failed to parse SSO response"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); } return null; } 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; }