use of run.halo.app.controller.content.auth.ContentAuthenticationRequest in project halo by ruibaby.
the class ContentContentController method authenticateCategory.
private String authenticateCategory(String slug, String type, String password, HttpServletRequest request) {
ContentAuthenticationRequest authRequest = new ContentAuthenticationRequest();
authRequest.setPassword(password);
Category category = categoryService.getBySlugOfNonNull(slug);
authRequest.setId(category.getId());
authRequest.setPrincipal(EncryptTypeEnum.CATEGORY.getName());
try {
providerManager.authenticate(authRequest);
CategoryDTO categoryDto = categoryService.convertTo(category);
return "redirect:" + buildRedirectUrl(categoryDto.getFullPath());
} catch (AuthenticationException e) {
request.setAttribute("errorMsg", e.getMessage());
request.setAttribute("type", type);
request.setAttribute("slug", slug);
return getPasswordPageUriToForward();
}
}
use of run.halo.app.controller.content.auth.ContentAuthenticationRequest in project halo by ruibaby.
the class ContentContentController method authenticatePost.
private String authenticatePost(String slug, String type, String password, HttpServletRequest request) {
ContentAuthenticationRequest authRequest = new ContentAuthenticationRequest();
authRequest.setPassword(password);
Post post = postService.getBy(PostStatus.INTIMATE, slug);
authRequest.setId(post.getId());
authRequest.setPrincipal(EncryptTypeEnum.POST.getName());
try {
providerManager.authenticate(authRequest);
BasePostMinimalDTO basePostMinimal = postRenderAssembler.convertToMinimal(post);
return "redirect:" + buildRedirectUrl(basePostMinimal.getFullPath());
} catch (AuthenticationException e) {
request.setAttribute("errorMsg", e.getMessage());
request.setAttribute("type", type);
request.setAttribute("slug", slug);
return getPasswordPageUriToForward();
}
}
use of run.halo.app.controller.content.auth.ContentAuthenticationRequest in project halo by ruibaby.
the class ContentAuthenticationManagerTest method authenticateCategoryTest.
@Test
public void authenticateCategoryTest() {
/*
* category-1(加密)
* | |-category-2(未设密码)
*/
Category category1 = new Category();
category1.setId(1);
category1.setPassword("123");
category1.setName("category-1");
category1.setSlug("category-1");
category1.setParentId(0);
Category category2 = new Category();
category2.setId(2);
category2.setPassword(null);
category2.setName("category-2");
category2.setSlug("category-2");
category2.setParentId(1);
// piling object
when(categoryService.lookupFirstEncryptedBy(2)).thenReturn(Optional.of(category1));
when(categoryService.getById(1)).thenReturn(category1);
when(categoryService.getById(2)).thenReturn(category2);
// build parameter
ContentAuthenticationRequest authRequest = ContentAuthenticationRequest.of(2, "", EncryptTypeEnum.CATEGORY.getName());
// test empty password
assertThatThrownBy(() -> contentAuthenticationManager.authenticate(authRequest)).isInstanceOf(AuthenticationException.class).hasMessage("密码不正确");
// test null password
authRequest.setPassword(null);
assertThatThrownBy(() -> contentAuthenticationManager.authenticate(authRequest)).isInstanceOf(AuthenticationException.class).hasMessage("密码不正确");
// test incorrect password
authRequest.setPassword("ABCD");
assertThatThrownBy(() -> contentAuthenticationManager.authenticate(authRequest)).isInstanceOf(AuthenticationException.class).hasMessage("密码不正确");
// test correct password
authRequest.setPassword("123");
ContentAuthentication authentication = contentAuthenticationManager.authenticate(authRequest);
assertThat(authentication).isNotNull();
}
use of run.halo.app.controller.content.auth.ContentAuthenticationRequest in project halo by halo-dev.
the class ContentAuthenticationManagerTest method authenticateCategoryTest.
@Test
public void authenticateCategoryTest() {
/*
* category-1(加密)
* | |-category-2(未设密码)
*/
Category category1 = new Category();
category1.setId(1);
category1.setPassword("123");
category1.setName("category-1");
category1.setSlug("category-1");
category1.setParentId(0);
Category category2 = new Category();
category2.setId(2);
category2.setPassword(null);
category2.setName("category-2");
category2.setSlug("category-2");
category2.setParentId(1);
// piling object
when(categoryService.lookupFirstEncryptedBy(2)).thenReturn(Optional.of(category1));
when(categoryService.getById(1)).thenReturn(category1);
when(categoryService.getById(2)).thenReturn(category2);
// build parameter
ContentAuthenticationRequest authRequest = ContentAuthenticationRequest.of(2, "", EncryptTypeEnum.CATEGORY.getName());
// test empty password
assertThatThrownBy(() -> contentAuthenticationManager.authenticate(authRequest)).isInstanceOf(AuthenticationException.class).hasMessage("密码不正确");
// test null password
authRequest.setPassword(null);
assertThatThrownBy(() -> contentAuthenticationManager.authenticate(authRequest)).isInstanceOf(AuthenticationException.class).hasMessage("密码不正确");
// test incorrect password
authRequest.setPassword("ABCD");
assertThatThrownBy(() -> contentAuthenticationManager.authenticate(authRequest)).isInstanceOf(AuthenticationException.class).hasMessage("密码不正确");
// test correct password
authRequest.setPassword("123");
ContentAuthentication authentication = contentAuthenticationManager.authenticate(authRequest);
assertThat(authentication).isNotNull();
}
use of run.halo.app.controller.content.auth.ContentAuthenticationRequest in project halo by halo-dev.
the class ContentContentController method authenticatePost.
private String authenticatePost(String slug, String type, String password, HttpServletRequest request) {
ContentAuthenticationRequest authRequest = new ContentAuthenticationRequest();
authRequest.setPassword(password);
Post post = postService.getBy(PostStatus.INTIMATE, slug);
post.setSlug(URLEncoder.encode(post.getSlug(), StandardCharsets.UTF_8));
authRequest.setId(post.getId());
authRequest.setPrincipal(EncryptTypeEnum.POST.getName());
try {
providerManager.authenticate(authRequest);
BasePostMinimalDTO basePostMinimal = postRenderAssembler.convertToMinimal(post);
return "redirect:" + buildRedirectUrl(basePostMinimal.getFullPath());
} catch (AuthenticationException e) {
request.setAttribute("errorMsg", e.getMessage());
request.setAttribute("type", type);
request.setAttribute("slug", slug);
return getPasswordPageUriToForward();
}
}
Aggregations