@Test public void handleShouldRejectRequestWithInvalidBearerHeaders() throws Exception { Request request = mock(Request.class); when(request.requestMethod()).thenReturn("GET"); when(request.headers(JwtFilter.AUTHORIZATION_HEADER_NAME)).thenReturn("Bearer value"); when(jwtTokenVerifier.verify("value")).thenReturn(false); expectedException.expect(HaltException.class); expectedException.expect(STATUS_CODE_MATCHER_401); jwtFilter.handle(request, mock(Response.class)); }
@Override public void handle(Request request, Response response) throws Exception { if (!request.requestMethod().equals(OPTIONS)) { Optional<String> bearer = Optional.ofNullable(request.headers(AUTHORIZATION_HEADER_NAME)) .filter(value -> value.startsWith(AUTHORIZATION_HEADER_PREFIX)) .map(value -> value.substring(AUTHORIZATION_HEADER_PREFIX.length())); checkHeaderPresent(bearer); checkValidSignature(bearer); checkIsAdmin(bearer); String login = jwtTokenVerifier.extractLogin(bearer.get()); request.attribute(LOGIN, login); } }
JwtFilter implements AuthenticationFilter { @Override public void handle(Request request, Response response) throws Exception { if (!request.requestMethod().equals(OPTIONS)) { Optional<String> bearer = Optional.ofNullable(request.headers(AUTHORIZATION_HEADER_NAME)) .filter(value -> value.startsWith(AUTHORIZATION_HEADER_PREFIX)) .map(value -> value.substring(AUTHORIZATION_HEADER_PREFIX.length())); checkHeaderPresent(bearer); checkValidSignature(bearer); checkIsAdmin(bearer); String login = jwtTokenVerifier.extractLogin(bearer.get()); request.attribute(LOGIN, login); } } }
JwtFilter implements AuthenticationFilter { @Override public void handle(Request request, Response response) throws Exception { if (!request.requestMethod().equals(OPTIONS)) { Optional<String> bearer = Optional.ofNullable(request.headers(AUTHORIZATION_HEADER_NAME)) .filter(value -> value.startsWith(AUTHORIZATION_HEADER_PREFIX)) .map(value -> value.substring(AUTHORIZATION_HEADER_PREFIX.length())); checkHeaderPresent(bearer); checkValidSignature(bearer); checkIsAdmin(bearer); String login = jwtTokenVerifier.extractLogin(bearer.get()); request.attribute(LOGIN, login); } } @Inject  JwtFilter(JwtTokenVerifier jwtTokenVerifier); }
JwtFilter implements AuthenticationFilter { @Override public void handle(Request request, Response response) throws Exception { if (!request.requestMethod().equals(OPTIONS)) { Optional<String> bearer = Optional.ofNullable(request.headers(AUTHORIZATION_HEADER_NAME)) .filter(value -> value.startsWith(AUTHORIZATION_HEADER_PREFIX)) .map(value -> value.substring(AUTHORIZATION_HEADER_PREFIX.length())); checkHeaderPresent(bearer); checkValidSignature(bearer); checkIsAdmin(bearer); String login = jwtTokenVerifier.extractLogin(bearer.get()); request.attribute(LOGIN, login); } } @Inject  JwtFilter(JwtTokenVerifier jwtTokenVerifier); @Override void handle(Request request, Response response); }
JwtFilter implements AuthenticationFilter { @Override public void handle(Request request, Response response) throws Exception { if (!request.requestMethod().equals(OPTIONS)) { Optional<String> bearer = Optional.ofNullable(request.headers(AUTHORIZATION_HEADER_NAME)) .filter(value -> value.startsWith(AUTHORIZATION_HEADER_PREFIX)) .map(value -> value.substring(AUTHORIZATION_HEADER_PREFIX.length())); checkHeaderPresent(bearer); checkValidSignature(bearer); checkIsAdmin(bearer); String login = jwtTokenVerifier.extractLogin(bearer.get()); request.attribute(LOGIN, login); } } @Inject  JwtFilter(JwtTokenVerifier jwtTokenVerifier); @Override void handle(Request request, Response response); static final String AUTHORIZATION_HEADER_PREFIX; static final String AUTHORIZATION_HEADER_NAME; static final String OPTIONS; }