@Test public void testChannelReadGetAuthToken() throws Exception { final ChannelHandlerContext ctx = createMock(ChannelHandlerContext.class); final FullHttpRequest msg = createMock(FullHttpRequest.class); final HttpHeaders headers = createMock(HttpHeaders.class); final Authenticator authenticator = createMock(Authenticator.class); final ChannelFuture cf = createMock(ChannelFuture.class); final String encodedUserNameAndPass = Base64.getEncoder().encodeToString("user:pass".getBytes()); final Capture<Map<String, String>> credMap = EasyMock.newCapture(CaptureType.ALL); expect(msg.getMethod()).andReturn(HttpMethod.GET); expect(msg.headers()).andReturn(headers).anyTimes(); expect(msg.getUri()).andReturn("/session"); expect(headers.get(eq("Authorization"))).andReturn("Basic " + encodedUserNameAndPass); expect(authenticator.authenticate(and(isA(Map.class), capture(credMap)))).andReturn(new AuthenticatedUser("foo")); expect(ctx.writeAndFlush(eqHttpStatus(OK))).andReturn(cf); expect(cf.addListener(ChannelFutureListener.CLOSE)).andReturn(null); expect(msg.release()).andReturn(false); final HttpHMACAuthenticationHandler handler = new HttpHMACAuthenticationHandler(authenticator); replayAll(); handler.channelRead(ctx, msg); verifyAll(); assertNotNull(credMap.getValue().get(HttpHMACAuthenticationHandler.PROPERTY_GENERATE_TOKEN)); }
@Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) { if (msg instanceof FullHttpRequest) { final FullHttpRequest req = (FullHttpRequest) msg; final HttpMethod method = req.getMethod(); final Map<String, String> credentialsMap = getCredentialsMap(ctx, req); if (credentialsMap == null) { sendError(ctx, msg); return; } if ("/session".equals(req.getUri()) && method.equals(HttpMethod.GET)) { try { credentialsMap.put(PROPERTY_GENERATE_TOKEN, "true"); authenticator.authenticate(credentialsMap); } catch (Exception e) { sendError(ctx, msg); return; } replyWithToken(ctx, msg, credentialsMap.get(PROPERTY_TOKEN)); } else { try { authenticator.authenticate(credentialsMap); ctx.fireChannelRead(req); } catch (Exception e) { sendError(ctx, msg); } } } }
HttpHMACAuthenticationHandler extends AbstractAuthenticationHandler { @Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) { if (msg instanceof FullHttpRequest) { final FullHttpRequest req = (FullHttpRequest) msg; final HttpMethod method = req.getMethod(); final Map<String, String> credentialsMap = getCredentialsMap(ctx, req); if (credentialsMap == null) { sendError(ctx, msg); return; } if ("/session".equals(req.getUri()) && method.equals(HttpMethod.GET)) { try { credentialsMap.put(PROPERTY_GENERATE_TOKEN, "true"); authenticator.authenticate(credentialsMap); } catch (Exception e) { sendError(ctx, msg); return; } replyWithToken(ctx, msg, credentialsMap.get(PROPERTY_TOKEN)); } else { try { authenticator.authenticate(credentialsMap); ctx.fireChannelRead(req); } catch (Exception e) { sendError(ctx, msg); } } } } }
HttpHMACAuthenticationHandler extends AbstractAuthenticationHandler { @Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) { if (msg instanceof FullHttpRequest) { final FullHttpRequest req = (FullHttpRequest) msg; final HttpMethod method = req.getMethod(); final Map<String, String> credentialsMap = getCredentialsMap(ctx, req); if (credentialsMap == null) { sendError(ctx, msg); return; } if ("/session".equals(req.getUri()) && method.equals(HttpMethod.GET)) { try { credentialsMap.put(PROPERTY_GENERATE_TOKEN, "true"); authenticator.authenticate(credentialsMap); } catch (Exception e) { sendError(ctx, msg); return; } replyWithToken(ctx, msg, credentialsMap.get(PROPERTY_TOKEN)); } else { try { authenticator.authenticate(credentialsMap); ctx.fireChannelRead(req); } catch (Exception e) { sendError(ctx, msg); } } } } HttpHMACAuthenticationHandler(final Authenticator authenticator); }
HttpHMACAuthenticationHandler extends AbstractAuthenticationHandler { @Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) { if (msg instanceof FullHttpRequest) { final FullHttpRequest req = (FullHttpRequest) msg; final HttpMethod method = req.getMethod(); final Map<String, String> credentialsMap = getCredentialsMap(ctx, req); if (credentialsMap == null) { sendError(ctx, msg); return; } if ("/session".equals(req.getUri()) && method.equals(HttpMethod.GET)) { try { credentialsMap.put(PROPERTY_GENERATE_TOKEN, "true"); authenticator.authenticate(credentialsMap); } catch (Exception e) { sendError(ctx, msg); return; } replyWithToken(ctx, msg, credentialsMap.get(PROPERTY_TOKEN)); } else { try { authenticator.authenticate(credentialsMap); ctx.fireChannelRead(req); } catch (Exception e) { sendError(ctx, msg); } } } } HttpHMACAuthenticationHandler(final Authenticator authenticator); @Override void channelRead(final ChannelHandlerContext ctx, final Object msg); }
HttpHMACAuthenticationHandler extends AbstractAuthenticationHandler { @Override public void channelRead(final ChannelHandlerContext ctx, final Object msg) { if (msg instanceof FullHttpRequest) { final FullHttpRequest req = (FullHttpRequest) msg; final HttpMethod method = req.getMethod(); final Map<String, String> credentialsMap = getCredentialsMap(ctx, req); if (credentialsMap == null) { sendError(ctx, msg); return; } if ("/session".equals(req.getUri()) && method.equals(HttpMethod.GET)) { try { credentialsMap.put(PROPERTY_GENERATE_TOKEN, "true"); authenticator.authenticate(credentialsMap); } catch (Exception e) { sendError(ctx, msg); return; } replyWithToken(ctx, msg, credentialsMap.get(PROPERTY_TOKEN)); } else { try { authenticator.authenticate(credentialsMap); ctx.fireChannelRead(req); } catch (Exception e) { sendError(ctx, msg); } } } } HttpHMACAuthenticationHandler(final Authenticator authenticator); @Override void channelRead(final ChannelHandlerContext ctx, final Object msg); static final String PROPERTY_TOKEN; static final String PROPERTY_GENERATE_TOKEN; }