@Test public void testExpiredToken() throws IOException, ServletException { request.setParameter("access_token", "abc123"); expect(connectionDaoMock.findByAccessToken("abc123")).andReturn(null).once(); replayAll(); filter.doFilter(request, response, chainMock); assertEquals(401, response.getStatus()); assertEquals("Bearer error=\"invalid_token\"", response.getHeader("WWW-Authenticate")); }
@Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; final String accessToken = getAccessToken(request); if (null != accessToken) { request.setAttribute(NAME_ACCESS_TOKEN, accessToken); final DConnection conn = verifyAccessToken(accessToken); if (null != conn && FactoryResource.PROVIDER_ID_SELF.equals(conn.getProviderId())) { LOGGER.debug("Authenticated. userId={}, roles={}, displayName={}", new Object[] { conn.getUserId(), conn.getRoles(), conn.getDisplayName()}); AbstractDao.setPrincipalName(null != conn.getUserId() ? conn.getUserId().toString() : null); request = new SecurityContextRequestWrapper(request, conn); request.setAttribute(NAME_CONNECTION, conn); request.setAttribute(NAME_USER_ID, conn.getUserId()); request.setAttribute(NAME_ROLES, conn.getRoles()); } else { LOGGER.debug("Unauthorised"); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INVALID_TOKEN); return; } } else { LOGGER.debug("Anonymous"); request.setAttribute(NAME_ROLES, OAuth2UserResource.ROLE_ANONYMOUS); request = new SecurityContextRequestWrapper(request); } response = new HttpStatusResponseWrapper(response) { @Override public ServletOutputStream getOutputStream() throws java.io.IOException { if (getStatus() == HttpServletResponse.SC_FORBIDDEN) { setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INSUFFICIENT_SCOPE); } else if (getStatus() == HttpServletResponse.SC_UNAUTHORIZED) { setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INVALID_TOKEN); } return super.getOutputStream(); } }; chain.doFilter(request, response); }
OAuth2Filter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; final String accessToken = getAccessToken(request); if (null != accessToken) { request.setAttribute(NAME_ACCESS_TOKEN, accessToken); final DConnection conn = verifyAccessToken(accessToken); if (null != conn && FactoryResource.PROVIDER_ID_SELF.equals(conn.getProviderId())) { LOGGER.debug("Authenticated. userId={}, roles={}, displayName={}", new Object[] { conn.getUserId(), conn.getRoles(), conn.getDisplayName()}); AbstractDao.setPrincipalName(null != conn.getUserId() ? conn.getUserId().toString() : null); request = new SecurityContextRequestWrapper(request, conn); request.setAttribute(NAME_CONNECTION, conn); request.setAttribute(NAME_USER_ID, conn.getUserId()); request.setAttribute(NAME_ROLES, conn.getRoles()); } else { LOGGER.debug("Unauthorised"); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INVALID_TOKEN); return; } } else { LOGGER.debug("Anonymous"); request.setAttribute(NAME_ROLES, OAuth2UserResource.ROLE_ANONYMOUS); request = new SecurityContextRequestWrapper(request); } response = new HttpStatusResponseWrapper(response) { @Override public ServletOutputStream getOutputStream() throws java.io.IOException { if (getStatus() == HttpServletResponse.SC_FORBIDDEN) { setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INSUFFICIENT_SCOPE); } else if (getStatus() == HttpServletResponse.SC_UNAUTHORIZED) { setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INVALID_TOKEN); } return super.getOutputStream(); } }; chain.doFilter(request, response); } }
OAuth2Filter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; final String accessToken = getAccessToken(request); if (null != accessToken) { request.setAttribute(NAME_ACCESS_TOKEN, accessToken); final DConnection conn = verifyAccessToken(accessToken); if (null != conn && FactoryResource.PROVIDER_ID_SELF.equals(conn.getProviderId())) { LOGGER.debug("Authenticated. userId={}, roles={}, displayName={}", new Object[] { conn.getUserId(), conn.getRoles(), conn.getDisplayName()}); AbstractDao.setPrincipalName(null != conn.getUserId() ? conn.getUserId().toString() : null); request = new SecurityContextRequestWrapper(request, conn); request.setAttribute(NAME_CONNECTION, conn); request.setAttribute(NAME_USER_ID, conn.getUserId()); request.setAttribute(NAME_ROLES, conn.getRoles()); } else { LOGGER.debug("Unauthorised"); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INVALID_TOKEN); return; } } else { LOGGER.debug("Anonymous"); request.setAttribute(NAME_ROLES, OAuth2UserResource.ROLE_ANONYMOUS); request = new SecurityContextRequestWrapper(request); } response = new HttpStatusResponseWrapper(response) { @Override public ServletOutputStream getOutputStream() throws java.io.IOException { if (getStatus() == HttpServletResponse.SC_FORBIDDEN) { setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INSUFFICIENT_SCOPE); } else if (getStatus() == HttpServletResponse.SC_UNAUTHORIZED) { setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INVALID_TOKEN); } return super.getOutputStream(); } }; chain.doFilter(request, response); } @Inject  OAuth2Filter(Provider<DConnectionDaoBean> connectionDaoProvider); }
OAuth2Filter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; final String accessToken = getAccessToken(request); if (null != accessToken) { request.setAttribute(NAME_ACCESS_TOKEN, accessToken); final DConnection conn = verifyAccessToken(accessToken); if (null != conn && FactoryResource.PROVIDER_ID_SELF.equals(conn.getProviderId())) { LOGGER.debug("Authenticated. userId={}, roles={}, displayName={}", new Object[] { conn.getUserId(), conn.getRoles(), conn.getDisplayName()}); AbstractDao.setPrincipalName(null != conn.getUserId() ? conn.getUserId().toString() : null); request = new SecurityContextRequestWrapper(request, conn); request.setAttribute(NAME_CONNECTION, conn); request.setAttribute(NAME_USER_ID, conn.getUserId()); request.setAttribute(NAME_ROLES, conn.getRoles()); } else { LOGGER.debug("Unauthorised"); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INVALID_TOKEN); return; } } else { LOGGER.debug("Anonymous"); request.setAttribute(NAME_ROLES, OAuth2UserResource.ROLE_ANONYMOUS); request = new SecurityContextRequestWrapper(request); } response = new HttpStatusResponseWrapper(response) { @Override public ServletOutputStream getOutputStream() throws java.io.IOException { if (getStatus() == HttpServletResponse.SC_FORBIDDEN) { setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INSUFFICIENT_SCOPE); } else if (getStatus() == HttpServletResponse.SC_UNAUTHORIZED) { setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INVALID_TOKEN); } return super.getOutputStream(); } }; chain.doFilter(request, response); } @Inject  OAuth2Filter(Provider<DConnectionDaoBean> connectionDaoProvider); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest req, ServletResponse res, FilterChain chain); @Override void destroy(); static Long getUserId(HttpServletRequest request); }
OAuth2Filter implements Filter { @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; final String accessToken = getAccessToken(request); if (null != accessToken) { request.setAttribute(NAME_ACCESS_TOKEN, accessToken); final DConnection conn = verifyAccessToken(accessToken); if (null != conn && FactoryResource.PROVIDER_ID_SELF.equals(conn.getProviderId())) { LOGGER.debug("Authenticated. userId={}, roles={}, displayName={}", new Object[] { conn.getUserId(), conn.getRoles(), conn.getDisplayName()}); AbstractDao.setPrincipalName(null != conn.getUserId() ? conn.getUserId().toString() : null); request = new SecurityContextRequestWrapper(request, conn); request.setAttribute(NAME_CONNECTION, conn); request.setAttribute(NAME_USER_ID, conn.getUserId()); request.setAttribute(NAME_ROLES, conn.getRoles()); } else { LOGGER.debug("Unauthorised"); response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INVALID_TOKEN); return; } } else { LOGGER.debug("Anonymous"); request.setAttribute(NAME_ROLES, OAuth2UserResource.ROLE_ANONYMOUS); request = new SecurityContextRequestWrapper(request); } response = new HttpStatusResponseWrapper(response) { @Override public ServletOutputStream getOutputStream() throws java.io.IOException { if (getStatus() == HttpServletResponse.SC_FORBIDDEN) { setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INSUFFICIENT_SCOPE); } else if (getStatus() == HttpServletResponse.SC_UNAUTHORIZED) { setHeader(HEADER_WWW_AUTHENTICATE, PREFIX_BEARER + ERROR_INVALID_TOKEN); } return super.getOutputStream(); } }; chain.doFilter(request, response); } @Inject  OAuth2Filter(Provider<DConnectionDaoBean> connectionDaoProvider); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest req, ServletResponse res, FilterChain chain); @Override void destroy(); static Long getUserId(HttpServletRequest request); static final String NAME_ACCESS_TOKEN; static final String NAME_USER_ID; static final String NAME_CONNECTION; static final String NAME_ROLES; static final String HEADER_AUTHORIZATION; static final String HEADER_WWW_AUTHENTICATE; static final String PREFIX_BEARER; static final String ERROR_INVALID_TOKEN; static final String ERROR_INSUFFICIENT_SCOPE; }