@Override public void afterPropertiesSet() throws Exception { if (tokenServices==null) { throw new NullPointerException("tokenServices property is null!"); } if (clientId==null) { throw new NullPointerException("clientId property is null!"); } if (clientDetailsService==null) { throw new NullPointerException("clientDetailsService property is null!"); } if (!isVerifySsl()) { skipSslValidation(); } } }
protected void internalSendMessage(String email, MessageType messageType, String subject, String htmlContent) { if (!getIsNotificationsRegistered()) { registerNotifications(); } Map<String, String> request = new HashMap<>();; String kindId = (String) notifications.get(messageType).get("id"); request.put("kind_id", kindId); request.put("to", email); request.put("subject", subject); request.put("html", htmlContent); HttpEntity<Map<String, String>> requestEntity = new HttpEntity<>(request); notificationsTemplate.exchange(notificationsUrl + "/emails", HttpMethod.POST, requestEntity, Void.class); }
@Override public void sendMessage(String email, MessageType messageType, String subject, String htmlContent) { IdentityZone current = IdentityZoneHolder.get(); try { if (isSendInDefaultZone()) { IdentityZoneHolder.set(IdentityZone.getUaa()); } internalSendMessage(email, messageType, subject, htmlContent); } finally { IdentityZoneHolder.set(current); } }
@Test public void testSendOssMimeMessage() throws Exception { EmailService emailService = new EmailService(mailSender, "http://login.example.com/login", null); emailService.sendMessage("user@example.com", MessageType.CHANGE_EMAIL, "Test Message", "<html><body>hi</body></html>"); assertThat(mailSender.getSentMessages(), hasSize(1)); FakeJavaMailSender.MimeMessageWrapper mimeMessageWrapper = mailSender.getSentMessages().get(0); assertThat(mimeMessageWrapper.getFrom(), hasSize(1)); InternetAddress fromAddress = (InternetAddress) mimeMessageWrapper.getFrom().get(0); assertThat(fromAddress.getAddress(), equalTo("admin@login.example.com")); assertThat(fromAddress.getPersonal(), equalTo("Cloud Foundry")); assertThat(mimeMessageWrapper.getRecipients(Message.RecipientType.TO), hasSize(1)); assertThat(mimeMessageWrapper.getRecipients(Message.RecipientType.TO).get(0), equalTo((Address) new InternetAddress("user@example.com"))); assertThat(mimeMessageWrapper.getContentString(), equalTo("<html><body>hi</body></html>")); }
private String captorEmailBody(String subject) { ArgumentCaptor<String> emailBodyArgument = ArgumentCaptor.forClass(String.class); verify(messageService).sendMessage( eq("user@example.com"), eq(MessageType.CREATE_ACCOUNT_CONFIRMATION), eq(subject), emailBodyArgument.capture() ); return emailBodyArgument.getValue(); } }
@Before public void setUpFakeMailServer() throws Exception { originalSender = getWebApplicationContext().getBean("emailService", EmailService.class).getMailSender(); getWebApplicationContext().getBean("emailService", EmailService.class).setMailSender(fakeJavaMailSender); }
@AfterEach void clearEmails() { webApplicationContext.getBean("emailService", EmailService.class).setMailSender(originalEmailSender); fakeJavaMailSender.clearMessage(); }
@Override protected void internalSendMessage(String email, MessageType messageType, String subject, String htmlContent) { assertEquals(IdentityZone.getUaa(), IdentityZoneHolder.get()); super.internalSendMessage(email, messageType, subject, htmlContent); } };
@Test public void testSendingMessageToEmailAddress() throws Exception { mockNotificationsServer.expect(requestTo("http://notifications.example.com/emails")) .andExpect(method(POST)) .andExpect(jsonPath("$.kind_id").value("kind-id-01")) .andExpect(jsonPath("$.to").value("user@example.com")) .andExpect(jsonPath("$.subject").value("First message")) .andExpect(jsonPath("$.html").value("<p>Message</p>")) .andRespond(withSuccess()); notificationsService.sendMessage("user@example.com", MessageType.PASSWORD_RESET, "First message", "<p>Message</p>"); mockNotificationsServer.verify(); }
@Override public void sendMessage(String email, MessageType messageType, String subject, String htmlContent) { MimeMessage message = mailSender.createMimeMessage(); try { message.addFrom(getSenderAddresses()); message.addRecipients(Message.RecipientType.TO, email); message.setSubject(subject); message.setContent(htmlContent, "text/html"); } catch (MessagingException e) { logger.error("Exception raised while sending message to " + email, e); } catch (UnsupportedEncodingException e) { logger.error("Exception raised while sending message to " + email, e); } mailSender.send(message); } }
protected void skipSslValidation() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException { SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build(); CloseableHttpClient httpClient = HttpClients.custom().setSslcontext(sslContext).build(); ClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); this.setRequestFactory(requestFactory); }
@After public void restoreMailServer() throws Exception { getWebApplicationContext().getBean("emailService", EmailService.class).setMailSender(originalSender); }
@Test public void testLocalUaaRestTemplateAcquireToken() throws Exception { LocalUaaRestTemplate restTemplate = getWebApplicationContext().getBean(LocalUaaRestTemplate.class); OAuth2AccessToken token = restTemplate.acquireAccessToken(new DefaultOAuth2ClientContext()); assertTrue("Scopes should contain oauth.login", token.getScope().contains("oauth.login")); assertTrue("Scopes should contain notifications.write", token.getScope().contains("notifications.write")); assertTrue("Scopes should contain critical_notifications.write", token.getScope().contains("critical_notifications.write")); }
private void generateAndSendCode(String email, String clientId, String subject, String userId, String redirectUri) throws IOException { ExpiringCode expiringCode = ScimUtils.getExpiringCode(codeStore, userId, email, clientId, redirectUri, REGISTRATION); String htmlContent = getEmailHtml(expiringCode.getCode(), email); messageService.sendMessage(email, MessageType.CREATE_ACCOUNT_CONFIRMATION, subject, htmlContent); }
@BeforeEach void setUp() { FilterChainProxy springSecurityFilterChain = webApplicationContext.getBean("springSecurityFilterChain", FilterChainProxy.class); mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext) .alwaysDo(print()) .addFilter(springSecurityFilterChain) .build(); testClient = new TestClient(mockMvc); EmailService emailService = webApplicationContext.getBean("emailService", EmailService.class); originalEmailSender = emailService.getMailSender(); emailService.setMailSender(fakeJavaMailSender); userEmail = "user" + new RandomValueStringGenerator().generate() + "@example.com"; assertNotNull(webApplicationContext.getBean("messageService")); IdentityZoneHolder.setProvisioning(webApplicationContext.getBean(IdentityZoneProvisioning.class)); mockMvcTestClient = new MockMvcTestClient(mockMvc); }
@Test public void testSendingMessageInAnotherZoneResets() throws Exception { IdentityZone zone = MultitenancyFixture.identityZone("zone", "zone"); IdentityZoneHolder.set(zone); mockNotificationsServer.expect(requestTo("http://notifications.example.com/emails")) .andExpect(method(POST)) .andExpect(jsonPath("$.to").value("user@example.com")) .andExpect(jsonPath("$.subject").value("First message")) .andExpect(jsonPath("$.html").value("<p>Message</p>")) .andRespond(withSuccess()); notificationsService.sendMessage("user@example.com", MessageType.PASSWORD_RESET, "First message", "<p>Message</p>"); mockNotificationsServer.verify(); assertSame(zone, IdentityZoneHolder.get()); }
@Test public void beginEmailChange() throws Exception { setUpForBeginEmailChange(); Mockito.verify(messageService).sendMessage( eq("new@example.com"), eq(MessageType.CHANGE_EMAIL), eq(String.format("%s Email change verification", IDENTITY_ZONE_NAME)), contains("<a href=\"http://localhost/login/verify_email?code=the_secret_code\">Verify your email</a>") ); }
@Test public void testSendingMessageInAnotherZoneResetsWhenError() throws Exception { IdentityZone zone = MultitenancyFixture.identityZone("zone", "zone"); IdentityZoneHolder.set(zone); mockNotificationsServer.expect(requestTo("http://notifications.example.com/emails")) .andExpect(method(POST)) .andExpect(jsonPath("$.to").value("user@example.com")) .andExpect(jsonPath("$.subject").value("First message")) .andExpect(jsonPath("$.html").value("<p>Message</p>")) .andRespond(withBadRequest()); try { notificationsService.sendMessage("user@example.com", MessageType.PASSWORD_RESET, "First message", "<p>Message</p>"); fail(); } catch (HttpClientErrorException x) { } mockNotificationsServer.verify(); assertSame(zone, IdentityZoneHolder.get()); } }
@Override public void beginEmailChange(String userId, String email, String newEmail, String clientId, String redirectUri) { ScimUser user = scimUserProvisioning.retrieve(userId, IdentityZoneHolder.get().getId()); List<ScimUser> results = scimUserProvisioning.query("userName eq \"" + newEmail + "\" and origin eq \"" + OriginKeys.UAA + "\"", IdentityZoneHolder.get().getId()); if (user.getUserName().equals(user.getPrimaryEmail())) { if (!results.isEmpty()) { throw new UaaException("Conflict", 409); } } String code = generateExpiringCode(userId, newEmail, clientId, redirectUri); String htmlContent = getEmailChangeEmailHtml(email, newEmail, code); if(htmlContent != null) { String subject = getSubjectText(); messageService.sendMessage(newEmail, MessageType.CHANGE_EMAIL, subject, htmlContent); } }
private void forgotPassword(String username, String clientId, String redirectUri) { String subject = getSubjectText(); String htmlContent = null; String userId = null; String email = null; try { ForgotPasswordInfo forgotPasswordInfo = resetPasswordService.forgotPassword(username, clientId, redirectUri); userId = forgotPasswordInfo.getUserId(); email = forgotPasswordInfo.getEmail(); htmlContent = getCodeSentEmailHtml(forgotPasswordInfo.getResetPasswordCode().getCode()); } catch (ConflictException e) { email = e.getEmail(); htmlContent = getResetUnavailableEmailHtml(email); userId = e.getUserId(); } catch (NotFoundException e) { logger.error("User with email address " + username + " not found."); } if (htmlContent != null && userId != null) { messageService.sendMessage(email, MessageType.PASSWORD_RESET, subject, htmlContent); } }