Search in sources :

Example 1 with ContentAuthenticationRequest

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();
    }
}
Also used : CategoryDTO(run.halo.app.model.dto.CategoryDTO) Category(run.halo.app.model.entity.Category) AuthenticationException(run.halo.app.exception.AuthenticationException) ContentAuthenticationRequest(run.halo.app.controller.content.auth.ContentAuthenticationRequest)

Example 2 with ContentAuthenticationRequest

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();
    }
}
Also used : BasePostMinimalDTO(run.halo.app.model.dto.post.BasePostMinimalDTO) AuthenticationException(run.halo.app.exception.AuthenticationException) Post(run.halo.app.model.entity.Post) ContentAuthenticationRequest(run.halo.app.controller.content.auth.ContentAuthenticationRequest)

Example 3 with ContentAuthenticationRequest

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();
}
Also used : ContentAuthentication(run.halo.app.controller.content.auth.ContentAuthentication) Category(run.halo.app.model.entity.Category) AuthenticationException(run.halo.app.exception.AuthenticationException) ContentAuthenticationRequest(run.halo.app.controller.content.auth.ContentAuthenticationRequest) Test(org.junit.jupiter.api.Test)

Example 4 with ContentAuthenticationRequest

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();
}
Also used : ContentAuthentication(run.halo.app.controller.content.auth.ContentAuthentication) Category(run.halo.app.model.entity.Category) AuthenticationException(run.halo.app.exception.AuthenticationException) ContentAuthenticationRequest(run.halo.app.controller.content.auth.ContentAuthenticationRequest) Test(org.junit.jupiter.api.Test)

Example 5 with ContentAuthenticationRequest

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();
    }
}
Also used : BasePostMinimalDTO(run.halo.app.model.dto.post.BasePostMinimalDTO) AuthenticationException(run.halo.app.exception.AuthenticationException) Post(run.halo.app.model.entity.Post) ContentAuthenticationRequest(run.halo.app.controller.content.auth.ContentAuthenticationRequest)

Aggregations

ContentAuthenticationRequest (run.halo.app.controller.content.auth.ContentAuthenticationRequest)8 AuthenticationException (run.halo.app.exception.AuthenticationException)6 Category (run.halo.app.model.entity.Category)4 Test (org.junit.jupiter.api.Test)2 ContentAuthentication (run.halo.app.controller.content.auth.ContentAuthentication)2 ForbiddenException (run.halo.app.exception.ForbiddenException)2 CategoryDTO (run.halo.app.model.dto.CategoryDTO)2 BasePostMinimalDTO (run.halo.app.model.dto.post.BasePostMinimalDTO)2 Post (run.halo.app.model.entity.Post)2