AppKeyExecutor implements IAuthExecutor { @Override public AuthReport checkAllowed(Exchange exchange, Api api) { AuthReport authReport = new AuthReport(); Object keyObj = null; Map<?, ?> parameters = exchange.getProperty(ExchangeConstantKeys.E3_REQUEST_PARAMETERS.toString(), Map.class); if (parameters == null) { if(logger.isDebugEnabled()) { logger.debug("Request parameters not set"); } authReport.setBadRequest(true); } else { keyObj = parameters.get(keyName); if (keyObj == null) { keyObj = exchange.getIn().getHeader(headerName, String.class); if (keyObj == null) { if(logger.isDebugEnabled()) { logger.debug("Unable to find url parameter or header matching the provisioned api key name"); } authReport.setBadRequest(true); } } } if(!authReport.isBadRequest()) { String authKey = keyObj.toString(); if(logger.isDebugEnabled()) { logger.debug("authKey= " + authKey); } removeHttpQueryValue(exchange, keyName+"="+authKey); exchange.getIn().removeHeader(headerName); try { authReport = dataAccess.checkAllowed(api, authKey); } catch(DataAccessRuntimeException e) { logger.error("Data Access Issue", e); } catch(Exception e) { logger.error("Data Access Issue", e); } } return authReport; } AppKeyExecutor(String keyname, String headerName, IAuthDataAccess dataAccess); @Override AuthReport checkAllowed(Exchange exchange, Api api); @Override AuthType getType(); @Override GatewayExceptionCode getErrorCode();  }
@Test public void testWin() { Exchange exchange = new DefaultExchange(context); Api api = new Api(); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put(appKeyName, "asdf"); exchange.setProperty(ExchangeConstantKeys.E3_REQUEST_PARAMETERS.toString(), parameters); exchange.getIn().setHeader(Exchange.HTTP_QUERY, appKeyName + "=asdf"); AppKeyExecutor executor = new AppKeyExecutor(appKeyName, appHeaderName, new MockAuthDataAccess("asdf", null, null)); AuthReport authReport = executor.checkAllowed(exchange, api); assertNotNull("This authentication should have succeeded", authReport.getAuthIdentity()); assertNull("The query parameter should have been removed", exchange.getIn().getHeader(Exchange.HTTP_QUERY)); }
@Test public void testWinHeader() { Exchange exchange = new DefaultExchange(context); exchange.setProperty(ExchangeConstantKeys.E3_REQUEST_PARAMETERS.toString(), new HashMap<String, Object>()); Api api = new Api(); api.setId("1234"); exchange.getIn().setHeader(appHeaderName, "asdf"); AppKeyExecutor executor = new AppKeyExecutor(appKeyName, appHeaderName, new MockAuthDataAccess("asdf", null, null)); AuthReport authReport = executor.checkAllowed(exchange, api); assertNotNull("This authentication should have succeeded", authReport.getAuthIdentity()); assertNull("The header should have been removed", exchange.getIn().getHeader(appHeaderName)); }
@Test public void testFailNotAllowed(){ Exchange exchange = new DefaultExchange(context); Api api = new Api(); api.setId("1234"); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put(appKeyName, "asdf"); exchange.setProperty(ExchangeConstantKeys.E3_REQUEST_PARAMETERS.toString(), parameters); AppKeyExecutor executor = new AppKeyExecutor(appKeyName, appHeaderName, new MockAuthDataAccess(null, null, null)); AuthReport authReport = executor.checkAllowed(exchange, api); assertNull("This authentication should have failed", authReport.getAuthIdentity()); }
@Test public void testFailNoHeader(){ Exchange exchange = new DefaultExchange(context); exchange.setProperty(ExchangeConstantKeys.E3_REQUEST_PARAMETERS.toString(), new HashMap<String, Object>()); Api api = new Api(); api.setId("1234"); AppKeyExecutor executor = new AppKeyExecutor(appKeyName, appHeaderName, new MockAuthDataAccess("asdf", null, null)); AuthReport authReport = executor.checkAllowed(exchange, api); assertNull("This authentication should have failed", authReport.getAuthIdentity()); }
@Test public void testFailBadFormat(){ Exchange exchange = new DefaultExchange(context); Api api = new Api(); api.setId("1234"); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put(appKeyName.toUpperCase(), "asdf"); exchange.setProperty(ExchangeConstantKeys.E3_REQUEST_PARAMETERS.toString(), parameters); AppKeyExecutor executor = new AppKeyExecutor(appKeyName, appHeaderName, new MockAuthDataAccess("asdf", null, null)); AuthReport authReport = executor.checkAllowed(exchange, api); assertNull("This authentication should have failed", authReport.getAuthIdentity()); }
@Test public void testFailBadFormat2(){ Exchange exchange = new DefaultExchange(context); Api api = new Api(); api.setId("1234"); Map<String, Object> parameters = new HashMap<String, Object>(); parameters.put(appKeyName, null); exchange.setProperty(ExchangeConstantKeys.E3_REQUEST_PARAMETERS.toString(), parameters); AppKeyExecutor executor = new AppKeyExecutor(appKeyName, appHeaderName, new MockAuthDataAccess("asdf", null, null)); AuthReport authReport = executor.checkAllowed(exchange, api); assertNull("This authentication should have failed", authReport.getAuthIdentity()); }