test_class.declarator: filter
test_class.original_string: OAuth2Filter filter;
test_class.type: OAuth2Filter
test_class.var_name: filter
test_class.declarator: connectionDaoProvider = new ProviderReference<>()
test_class.modifier: final
test_class.original_string: final ProviderReference<DConnectionDaoBean> connectionDaoProvider = new ProviderReference<>();
test_class.type: ProviderReference<DConnectionDaoBean>
test_class.var_name: connectionDaoProvider
test_class.declarator: userDaoProvider = new ProviderReference<>()
test_class.modifier: final
test_class.original_string: final ProviderReference<DOAuth2UserDaoBean> userDaoProvider = new ProviderReference<>();
test_class.type: ProviderReference<DOAuth2UserDaoBean>
test_class.var_name: userDaoProvider
test_class.declarator: connectionDaoMock
test_class.original_string: DConnectionDaoBean connectionDaoMock;
test_class.type: DConnectionDaoBean
test_class.var_name: connectionDaoMock
test_class.declarator: userDaoMock
test_class.original_string: DOAuth2UserDaoBean userDaoMock;
test_class.type: DOAuth2UserDaoBean
test_class.var_name: userDaoMock
test_class.declarator: chainMock
test_class.modifier: private
test_class.original_string: private FilterChain chainMock;
test_class.type: FilterChain
test_class.var_name: chainMock
test_class.declarator: request
test_class.original_string: MockHttpServletRequest request;
test_class.type: MockHttpServletRequest
test_class.var_name: request
test_class.declarator: response
test_class.original_string: MockHttpServletResponse response;
test_class.type: MockHttpServletResponse
test_class.var_name: response
test_class.file: guja-core/src/test/java/com/wadpam/guja/oauth2/web/OAuth2FilterTest.java
test_class.identifier: OAuth2FilterTest
test_case.body: @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"));
      }
test_case.class_method_signature: OAuth2FilterTest.testExpiredToken()
test_case.full_signature: @Test public void testExpiredToken()
test_case.identifier: testExpiredToken
test_case.invocations: setParameter once andReturn expect findByAccessToken replayAll doFilter assertEquals getStatus assertEquals getHeader
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testExpiredToken()
focal_class.declarator: NAME_ACCESS_TOKEN = "access_token"
focal_class.modifier: public static final
focal_class.original_string: public static final String NAME_ACCESS_TOKEN = "access_token";
focal_class.type: String
focal_class.var_name: NAME_ACCESS_TOKEN
focal_class.declarator: NAME_USER_ID = "oauth2user.id"
focal_class.modifier: public static final
focal_class.original_string: public static final String NAME_USER_ID = "oauth2user.id";
focal_class.type: String
focal_class.var_name: NAME_USER_ID
focal_class.declarator: NAME_CONNECTION = "oauth2connection"
focal_class.modifier: public static final
focal_class.original_string: public static final String NAME_CONNECTION = "oauth2connection";
focal_class.type: String
focal_class.var_name: NAME_CONNECTION
focal_class.declarator: NAME_ROLES = "oauth2user.roles"
focal_class.modifier: public static final
focal_class.original_string: public static final String NAME_ROLES = "oauth2user.roles";
focal_class.type: String
focal_class.var_name: NAME_ROLES
focal_class.declarator: HEADER_AUTHORIZATION = "Authorization"
focal_class.modifier: public static final
focal_class.original_string: public static final String HEADER_AUTHORIZATION = "Authorization";
focal_class.type: String
focal_class.var_name: HEADER_AUTHORIZATION
focal_class.declarator: HEADER_WWW_AUTHENTICATE = "WWW-Authenticate"
focal_class.modifier: public static final
focal_class.original_string: public static final String HEADER_WWW_AUTHENTICATE = "WWW-Authenticate";
focal_class.type: String
focal_class.var_name: HEADER_WWW_AUTHENTICATE
focal_class.declarator: PREFIX_BEARER = "Bearer "
focal_class.modifier: public static final
focal_class.original_string: public static final String PREFIX_BEARER = "Bearer ";
focal_class.type: String
focal_class.var_name: PREFIX_BEARER
focal_class.declarator: ERROR_INVALID_TOKEN = "error=\"invalid_token\""
focal_class.modifier: public static final
focal_class.original_string: public static final String ERROR_INVALID_TOKEN = "error=\"invalid_token\"";
focal_class.type: String
focal_class.var_name: ERROR_INVALID_TOKEN
focal_class.declarator: ERROR_INSUFFICIENT_SCOPE = "error=\"insufficient_scope\""
focal_class.modifier: public static final
focal_class.original_string: public static final String ERROR_INSUFFICIENT_SCOPE = "error=\"insufficient_scope\"";
focal_class.type: String
focal_class.var_name: ERROR_INSUFFICIENT_SCOPE
focal_class.declarator: LOGGER = LoggerFactory.getLogger(OAuth2Filter.class)
focal_class.modifier: static final
focal_class.original_string: static final Logger LOGGER = LoggerFactory.getLogger(OAuth2Filter.class);
focal_class.type: Logger
focal_class.var_name: LOGGER
focal_class.declarator: connectionDaoProvider
focal_class.modifier: private final
focal_class.original_string: private final Provider<DConnectionDaoBean> connectionDaoProvider;
focal_class.type: Provider<DConnectionDaoBean>
focal_class.var_name: connectionDaoProvider
focal_class.file: guja-core/src/main/java/com/wadpam/guja/oauth2/web/OAuth2Filter.java
focal_class.identifier: OAuth2Filter
focal_class.interfaces: implements Filter
focal_class.class_method_signature: OAuth2Filter.OAuth2Filter(Provider<DConnectionDaoBean> connectionDaoProvider)
focal_class.full_signature: @Inject public  OAuth2Filter(Provider<DConnectionDaoBean> connectionDaoProvider)
focal_class.identifier: OAuth2Filter
focal_class.modifiers: @Inject public
focal_class.parameters: (Provider<DConnectionDaoBean> connectionDaoProvider)
focal_class.signature:  OAuth2Filter(Provider<DConnectionDaoBean> connectionDaoProvider)
focal_class.class_method_signature: OAuth2Filter.init(FilterConfig filterConfig)
focal_class.full_signature: @Override public void init(FilterConfig filterConfig)
focal_class.identifier: init
focal_class.modifiers: @Override public
focal_class.parameters: (FilterConfig filterConfig)
focal_class.return: void
focal_class.signature: void init(FilterConfig filterConfig)
focal_class.class_method_signature: OAuth2Filter.doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
focal_class.full_signature: @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
focal_class.identifier: doFilter
focal_class.modifiers: @Override public
focal_class.parameters: (ServletRequest req, ServletResponse res, FilterChain chain)
focal_class.return: void
focal_class.signature: void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
focal_class.class_method_signature: OAuth2Filter.destroy()
focal_class.full_signature: @Override public void destroy()
focal_class.identifier: destroy
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void destroy()
focal_class.class_method_signature: OAuth2Filter.getAccessToken(HttpServletRequest request)
focal_class.full_signature: private static String getAccessToken(HttpServletRequest request)
focal_class.identifier: getAccessToken
focal_class.modifiers: private static
focal_class.parameters: (HttpServletRequest request)
focal_class.return: String
focal_class.signature: String getAccessToken(HttpServletRequest request)
focal_class.class_method_signature: OAuth2Filter.getUserId(HttpServletRequest request)
focal_class.full_signature: public static Long getUserId(HttpServletRequest request)
focal_class.identifier: getUserId
focal_class.modifiers: public static
focal_class.parameters: (HttpServletRequest request)
focal_class.return: Long
focal_class.signature: Long getUserId(HttpServletRequest request)
focal_class.class_method_signature: OAuth2Filter.verifyAccessToken(String accessToken)
focal_class.full_signature: private DConnection verifyAccessToken(String accessToken)
focal_class.identifier: verifyAccessToken
focal_class.modifiers: private
focal_class.parameters: (String accessToken)
focal_class.return: DConnection
focal_class.signature: DConnection verifyAccessToken(String accessToken)
focal_method.body: @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);
  
        // access_token used here for app authentication must be issued by self:
        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);
  
          // User is authenticated
          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);
      }
  
      // Not possible to change response header after chain.doFilter(...) has been called
      // http://stackoverflow.com/questions/23084182/change-contenttype-or-characterencoding-in-java-filter-only-if-contenttype-j
      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);
  
    }
focal_method.class_method_signature: OAuth2Filter.doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
focal_method.full_signature: @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
focal_method.identifier: doFilter
focal_method.invocations: getAccessToken setAttribute verifyAccessToken equals getProviderId debug getUserId getRoles getDisplayName setPrincipalName getUserId toString getUserId setAttribute setAttribute getUserId setAttribute getRoles debug setStatus setHeader debug setAttribute getStatus setHeader getStatus setHeader getOutputStream doFilter
focal_method.modifiers: @Override public
focal_method.parameters: (ServletRequest req, ServletResponse res, FilterChain chain)
focal_method.return: void
focal_method.signature: void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)