@Test public void enrollFactorTest() throws Exception { expect(mockAuthentication.getName()).andReturn(expectedUser.getId()).atLeastOnce(); expect(mockOktaUserService.getUser(expectedUser.getId())).andReturn(expectedUser); expect(mockFactorLifecycleService.enroll(expectedFactor.getFactorType(), expectedFactor.getProvider(), expectedFactor.getProfile(), expectedUser)).andReturn(expectedFactor); ObjectMapper objectMapper = new ObjectMapper(); String requestBody = objectMapper.writeValueAsString(expectedFactor); replayAll(); MvcResult mvcResult = mockMvc.perform( post("/api/latest/plugin/okta/factor/enrollment") .content(requestBody) .contentType(MediaType.APPLICATION_JSON) .accept(MediaType.APPLICATION_JSON) .session(mockHttpSession) .principal(mockAuthentication)) .andReturn(); verifyAll(); MockHttpServletResponse response = mvcResult.getResponse(); assertEquals(HttpStatus.OK.value(), response.getStatus()); assertTrue(response.getContentType().startsWith(MediaType.APPLICATION_JSON_VALUE)); Factor factor = objectMapper.readValue(response.getContentAsString(), Factor.class); assertNotNull(factor); assertEquals(expectedFactor.getId(), factor.getId()); assertEquals(expectedFactor.getFactorType(), factor.getFactorType()); assertEquals(expectedFactor.getProvider(), factor.getProvider()); FactorProfile profile = factor.getProfile(); assertNotNull(profile); assertEquals(expectedProfile.getEmail(), profile.getEmail()); }
@RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Factor enrollFactor(@RequestBody Factor enrollRequestDTO, Authentication auth) throws OktaException { Preconditions.checkNotNull(enrollRequestDTO, "enrollRequestDTO is null"); Preconditions.checkNotNull(enrollRequestDTO.getFactorType(), "factor type is null"); Preconditions.checkNotNull(enrollRequestDTO.getProvider(), "provider is null"); try { LOGGER.info("Enrolling user [{}] in factor [{}]", auth.getName(), enrollRequestDTO.getFactorType().name()); OktaUser user = getOktaUserService().getUser(auth.getName()); return getFactorLifecycleService().enroll(enrollRequestDTO.getFactorType(), enrollRequestDTO.getProvider(), enrollRequestDTO.getProfile(), user); } catch (Exception e) { LOGGER.error("Failed to enroll user [{}] in factor [{}]", auth.getName(), enrollRequestDTO.getFactorType().name(), e); throw new OktaException(e.getMessage(), e); } }
FactorEnrollmentAPIController { @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Factor enrollFactor(@RequestBody Factor enrollRequestDTO, Authentication auth) throws OktaException { Preconditions.checkNotNull(enrollRequestDTO, "enrollRequestDTO is null"); Preconditions.checkNotNull(enrollRequestDTO.getFactorType(), "factor type is null"); Preconditions.checkNotNull(enrollRequestDTO.getProvider(), "provider is null"); try { LOGGER.info("Enrolling user [{}] in factor [{}]", auth.getName(), enrollRequestDTO.getFactorType().name()); OktaUser user = getOktaUserService().getUser(auth.getName()); return getFactorLifecycleService().enroll(enrollRequestDTO.getFactorType(), enrollRequestDTO.getProvider(), enrollRequestDTO.getProfile(), user); } catch (Exception e) { LOGGER.error("Failed to enroll user [{}] in factor [{}]", auth.getName(), enrollRequestDTO.getFactorType().name(), e); throw new OktaException(e.getMessage(), e); } } }
FactorEnrollmentAPIController { @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Factor enrollFactor(@RequestBody Factor enrollRequestDTO, Authentication auth) throws OktaException { Preconditions.checkNotNull(enrollRequestDTO, "enrollRequestDTO is null"); Preconditions.checkNotNull(enrollRequestDTO.getFactorType(), "factor type is null"); Preconditions.checkNotNull(enrollRequestDTO.getProvider(), "provider is null"); try { LOGGER.info("Enrolling user [{}] in factor [{}]", auth.getName(), enrollRequestDTO.getFactorType().name()); OktaUser user = getOktaUserService().getUser(auth.getName()); return getFactorLifecycleService().enroll(enrollRequestDTO.getFactorType(), enrollRequestDTO.getProvider(), enrollRequestDTO.getProfile(), user); } catch (Exception e) { LOGGER.error("Failed to enroll user [{}] in factor [{}]", auth.getName(), enrollRequestDTO.getFactorType().name(), e); throw new OktaException(e.getMessage(), e); } }  }
FactorEnrollmentAPIController { @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Factor enrollFactor(@RequestBody Factor enrollRequestDTO, Authentication auth) throws OktaException { Preconditions.checkNotNull(enrollRequestDTO, "enrollRequestDTO is null"); Preconditions.checkNotNull(enrollRequestDTO.getFactorType(), "factor type is null"); Preconditions.checkNotNull(enrollRequestDTO.getProvider(), "provider is null"); try { LOGGER.info("Enrolling user [{}] in factor [{}]", auth.getName(), enrollRequestDTO.getFactorType().name()); OktaUser user = getOktaUserService().getUser(auth.getName()); return getFactorLifecycleService().enroll(enrollRequestDTO.getFactorType(), enrollRequestDTO.getProvider(), enrollRequestDTO.getProfile(), user); } catch (Exception e) { LOGGER.error("Failed to enroll user [{}] in factor [{}]", auth.getName(), enrollRequestDTO.getFactorType().name(), e); throw new OktaException(e.getMessage(), e); } }  @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody List<Factor> findEnrolledFactors(Authentication auth); @RequestMapping(value = "available", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody List<Factor> findAvailableFactors(Authentication auth); @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody Factor enrollFactor(@RequestBody Factor enrollRequestDTO, Authentication auth); @RequestMapping(value = "activate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody Factor activateFactor(@RequestBody ActivateRequestDTO activateRequestDTO, Authentication auth); @RequestMapping(value = "reset", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody void activateFactor(Authentication auth); @RequestMapping(method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody void deleteFactor(@RequestParam("factorId") String id, Authentication auth); FactorService getFactorService(); void setFactorService(FactorService factorService); FactorLifecycleService getFactorLifecycleService(); void setFactorLifecycleService(FactorLifecycleService factorLifecycleService); OktaUserService getOktaUserService(); void setOktaUserService(OktaUserService oktaUserService); }
FactorEnrollmentAPIController { @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Factor enrollFactor(@RequestBody Factor enrollRequestDTO, Authentication auth) throws OktaException { Preconditions.checkNotNull(enrollRequestDTO, "enrollRequestDTO is null"); Preconditions.checkNotNull(enrollRequestDTO.getFactorType(), "factor type is null"); Preconditions.checkNotNull(enrollRequestDTO.getProvider(), "provider is null"); try { LOGGER.info("Enrolling user [{}] in factor [{}]", auth.getName(), enrollRequestDTO.getFactorType().name()); OktaUser user = getOktaUserService().getUser(auth.getName()); return getFactorLifecycleService().enroll(enrollRequestDTO.getFactorType(), enrollRequestDTO.getProvider(), enrollRequestDTO.getProfile(), user); } catch (Exception e) { LOGGER.error("Failed to enroll user [{}] in factor [{}]", auth.getName(), enrollRequestDTO.getFactorType().name(), e); throw new OktaException(e.getMessage(), e); } }  @RequestMapping(method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody List<Factor> findEnrolledFactors(Authentication auth); @RequestMapping(value = "available", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody List<Factor> findAvailableFactors(Authentication auth); @RequestMapping(method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody Factor enrollFactor(@RequestBody Factor enrollRequestDTO, Authentication auth); @RequestMapping(value = "activate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody Factor activateFactor(@RequestBody ActivateRequestDTO activateRequestDTO, Authentication auth); @RequestMapping(value = "reset", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody void activateFactor(Authentication auth); @RequestMapping(method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody void deleteFactor(@RequestParam("factorId") String id, Authentication auth); FactorService getFactorService(); void setFactorService(FactorService factorService); FactorLifecycleService getFactorLifecycleService(); void setFactorLifecycleService(FactorLifecycleService factorLifecycleService); OktaUserService getOktaUserService(); void setOktaUserService(OktaUserService oktaUserService);  }