OIDCAuthService { public ResponseEntity<String> processOIDCCallback(String state, String code) { String pathStr = "?code="+code+"&state="+state; Response response = reqProcessor.process("/auth/oidc/oidc/callback","{\"path\":\""+pathStr+"\"}", ""); if(HttpStatus.OK.equals(response.getHttpstatus())){ Map<String, Object> responseMap = null; try { responseMap = new ObjectMapper().readValue(response.getResponse(), new TypeReference<Map<String, Object>>(){}); } catch (IOException e) { log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "processCallback"). put(LogMessage.MESSAGE, "Failed to getresponse map from callback response"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); } if(responseMap!=null && responseMap.get("access")!=null) { Map<String,Object> access = (Map<String,Object>)responseMap.get("access"); access = ControllerUtil.filterDuplicateSafePermissions(access); access = ControllerUtil.filterDuplicateSvcaccPermissions(access); responseMap.put("access", access); Map<String,Object> feature = new HashMap<>(); feature.put(TVaultConstants.SELFSERVICE, isSSEnabled); feature.put(TVaultConstants.ADAUTOROTATION, isAdPswdRotationEnabled); responseMap.put("feature", feature); response.setResponse(JSONUtil.getJSON(responseMap)); } log.info(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "processCallback"). put(LogMessage.MESSAGE, "Successfully retrieved token from OIDC login"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); } log.error(JSONUtil.getJSON(ImmutableMap.<String, String>builder(). put(LogMessage.USER, ThreadLocalContext.getCurrentMap().get(LogMessage.USER)). put(LogMessage.ACTION, "processCallback"). put(LogMessage.MESSAGE, "Failed to get token from OIDC login"). put(LogMessage.APIURL, ThreadLocalContext.getCurrentMap().get(LogMessage.APIURL)). build())); return ResponseEntity.status(response.getHttpstatus()).body(response.getResponse()); }  ResponseEntity<String> getAuthenticationMounts(String token); ResponseEntity<OIDCEntityResponse> entityLookUp(String token,
			OIDCLookupEntityRequest oidcLookupEntityRequest); ResponseEntity<String> groupEntityLookUp(String token, OIDCLookupEntityRequest oidcLookupEntityRequest); ResponseEntity<String> readEntityAliasById(String token, String id); ResponseEntity<String> readEntityByName(String token, String entityName); ResponseEntity<String> updateEntityByName(String token, OIDCEntityRequest oidcEntityRequest); ResponseEntity<String> updateIdentityGroupByName(String token,
                                                            OIDCIdentityGroupRequest oidcIdentityGroupRequest); ResponseEntity<String> readGroupAliasById(String token, String id); ResponseEntity<String> deleteGroupByName(String token, String name); ResponseEntity<String> deleteGroupAliasByID(String token, String id); ResponseEntity<String> createGroupAlias(String token, GroupAliasRequest groupAliasRequest); ResponseEntity<String> getAuthUrl(OidcRequest oidcRequest); ResponseEntity<String> processOIDCCallback(String state, String code); ResponseEntity<String> getGroupObjectIdFromAzure(String groupName); ResponseEntity<String> getIdentityGroupDetails(String groupName, String token); ResponseEntity<String> getUserName(UserDetails userDetails); ResponseEntity<DirectoryObjects> searchGroupInAzureAD(String groupName);  }
@Test public void test_processCallback_successful() { String state = "teststatecode"; String code = "testauthcode"; String pathStr = "?code="+code+"&state="+state; String responseJson = "{\n" + "\"client_token\": \"testmioJHmaUB1k7PB2wUDh\",\n" + "\"admin\": \"yes\",\n" + "\"access\": {\n" + "\"cert\": [{\n" + "\"test.company.com\": \"read\"\n" + "}],\n" + "\"svcacct\": [{\n" + "\"svc_test04\": \"write\"\n" + "}],\n" + "\"users\": [{\n" + "\"testsafe1\": \"read\"\n" + "}]\n" + "},\n" + "\"policies\": [\"default\"],\n" + "\"lease_duration\": 1800\n" + "}"; Response response = getMockResponse(HttpStatus.OK, true, responseJson); when(reqProcessor.process("/auth/oidc/oidc/callback","{\"path\":\""+pathStr+"\"}","")).thenReturn(response); Map<String, Object> access = new HashMap<>(); when(ControllerUtil.filterDuplicateSafePermissions(any())).thenReturn(access); when(ControllerUtil.filterDuplicateSvcaccPermissions(any())).thenReturn(access); when(JSONUtil.getJSON(any(Map.class))).thenReturn(responseJson); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.OK).body(responseJson); ResponseEntity<String> responseEntity = oidcAuthService.processOIDCCallback(state, code); assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }
@Test public void test_processCallback_failed() { String state = "teststatecode"; String code = "testauthcode"; String pathStr = "?code="+code+"&state="+state; String responseJson = "{\"errors\":[\"Failed to get process callback\"]}";; Response response = getMockResponse(HttpStatus.BAD_REQUEST, true, responseJson); when(reqProcessor.process("/auth/oidc/oidc/callback","{\"path\":\""+pathStr+"\"}","")).thenReturn(response); ResponseEntity<String> responseEntityExpected = ResponseEntity.status(HttpStatus.BAD_REQUEST).body(responseJson); ResponseEntity<String> responseEntity = oidcAuthService.processOIDCCallback(state, code); assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode()); assertEquals(responseEntityExpected, responseEntity); }