-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#8804 Management port returns 403 when no authentication is provided
- Loading branch information
Showing
7 changed files
with
192 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
modules/web/web-impl/src/main/java/com/enonic/xp/web/impl/auth/AuthRequiredFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.enonic.xp.web.impl.auth; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.FilterChain; | ||
import javax.servlet.annotation.WebFilter; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.osgi.service.component.annotations.Component; | ||
|
||
import com.google.common.net.HttpHeaders; | ||
|
||
import com.enonic.xp.annotation.Order; | ||
import com.enonic.xp.web.filter.OncePerRequestFilter; | ||
|
||
@Component(immediate = true, service = Filter.class, property = {"connector=api"}) | ||
@Order(-20) | ||
@WebFilter("/*") | ||
public class AuthRequiredFilter | ||
extends OncePerRequestFilter | ||
{ | ||
@Override | ||
protected void doHandle( final HttpServletRequest req, final HttpServletResponse res, final FilterChain chain ) | ||
throws Exception | ||
{ | ||
if ( req.getUserPrincipal() == null ) | ||
{ | ||
res.addHeader( HttpHeaders.WWW_AUTHENTICATE, "Basic"); | ||
res.addHeader( HttpHeaders.WWW_AUTHENTICATE, "Bearer"); | ||
res.sendError( HttpServletResponse.SC_UNAUTHORIZED ); | ||
return; | ||
} | ||
chain.doFilter( req, res ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
modules/web/web-impl/src/test/java/com/enonic/xp/web/impl/auth/AuthRequiredFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package com.enonic.xp.web.impl.auth; | ||
|
||
import javax.servlet.FilterChain; | ||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
|
||
import com.google.common.net.HttpHeaders; | ||
|
||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.never; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class AuthRequiredFilterTest | ||
{ | ||
private AuthRequiredFilter authRequiredFilter; | ||
|
||
@Mock | ||
private HttpServletRequest request; | ||
|
||
@Mock | ||
private HttpServletResponse response; | ||
|
||
@Mock | ||
private FilterChain filterChain; | ||
|
||
@BeforeEach | ||
void setUp() | ||
{ | ||
authRequiredFilter = new AuthRequiredFilter(); | ||
} | ||
|
||
@Test | ||
void doHandle_whenUserPrincipalIsNull_sendsUnauthorizedError() | ||
throws Exception | ||
{ | ||
when( request.getUserPrincipal() ).thenReturn( null ); | ||
|
||
authRequiredFilter.doHandle( request, response, filterChain ); | ||
|
||
verify( response ).addHeader( HttpHeaders.WWW_AUTHENTICATE, "Basic" ); | ||
verify( response ).addHeader( HttpHeaders.WWW_AUTHENTICATE, "Bearer" ); | ||
verify( response ).sendError( HttpServletResponse.SC_UNAUTHORIZED ); | ||
verify( filterChain, never() ).doFilter( request, response ); | ||
} | ||
|
||
@Test | ||
void doHandle_whenUserPrincipalIsNotNull_proceedsWithFilterChain() | ||
throws Exception | ||
{ | ||
when( request.getUserPrincipal() ).thenReturn( () -> "user" ); | ||
|
||
authRequiredFilter.doHandle( request, response, filterChain ); | ||
|
||
verify( response, never() ).addHeader( anyString(), anyString() ); | ||
verify( response, never() ).sendError( anyInt() ); | ||
verify( filterChain ).doFilter( request, response ); | ||
} | ||
} |
Oops, something went wrong.