@Test void givenWrongContext_whenProcess_thenReturnWhenCmdIsNull() throws RequestAbortException { AuthenticationCommand universalCmd = mock(ServiceAuthenticationServiceImpl.UniversalAuthenticationCommand.class); prepareWrongContextForCmdNull(universalCmd); decorator.process(request); verify(serviceAuthenticationService, atLeastOnce()).getAuthentication(info); verify(universalCmd, times(0)).applyToRequest(request); }
public void process(HttpRequest request) { final RequestContext context = RequestContext.getCurrentContext(); if (context.get(AUTHENTICATION_COMMAND_KEY) instanceof ServiceAuthenticationServiceImpl.UniversalAuthenticationCommand) { InstanceInfo info = RequestContextUtils.getInstanceInfo().orElseThrow( () -> new RequestContextNotPreparedException("InstanceInfo of loadbalanced instance is not present in RequestContext") ); final Authentication authentication = serviceAuthenticationService.getAuthentication(info); AuthenticationCommand cmd; try { final String jwtToken = authenticationService.getJwtTokenFromRequest(context.getRequest()).orElse(null); cmd = serviceAuthenticationService.getAuthenticationCommand(authentication, jwtToken); if (cmd == null) { return; } if (cmd.isRequiredValidJwt()) { if (jwtToken == null || !authenticationService.validateJwtToken(jwtToken).isAuthenticated()) { throw new RequestAbortException(new TokenNotValidException("JWT Token is not authenticated")); } } } catch (AuthenticationException ae) { throw new RequestAbortException(ae); } cmd.applyToRequest(request); } }
ServiceAuthenticationDecorator { public void process(HttpRequest request) { final RequestContext context = RequestContext.getCurrentContext(); if (context.get(AUTHENTICATION_COMMAND_KEY) instanceof ServiceAuthenticationServiceImpl.UniversalAuthenticationCommand) { InstanceInfo info = RequestContextUtils.getInstanceInfo().orElseThrow( () -> new RequestContextNotPreparedException("InstanceInfo of loadbalanced instance is not present in RequestContext") ); final Authentication authentication = serviceAuthenticationService.getAuthentication(info); AuthenticationCommand cmd; try { final String jwtToken = authenticationService.getJwtTokenFromRequest(context.getRequest()).orElse(null); cmd = serviceAuthenticationService.getAuthenticationCommand(authentication, jwtToken); if (cmd == null) { return; } if (cmd.isRequiredValidJwt()) { if (jwtToken == null || !authenticationService.validateJwtToken(jwtToken).isAuthenticated()) { throw new RequestAbortException(new TokenNotValidException("JWT Token is not authenticated")); } } } catch (AuthenticationException ae) { throw new RequestAbortException(ae); } cmd.applyToRequest(request); } } }
ServiceAuthenticationDecorator { public void process(HttpRequest request) { final RequestContext context = RequestContext.getCurrentContext(); if (context.get(AUTHENTICATION_COMMAND_KEY) instanceof ServiceAuthenticationServiceImpl.UniversalAuthenticationCommand) { InstanceInfo info = RequestContextUtils.getInstanceInfo().orElseThrow( () -> new RequestContextNotPreparedException("InstanceInfo of loadbalanced instance is not present in RequestContext") ); final Authentication authentication = serviceAuthenticationService.getAuthentication(info); AuthenticationCommand cmd; try { final String jwtToken = authenticationService.getJwtTokenFromRequest(context.getRequest()).orElse(null); cmd = serviceAuthenticationService.getAuthenticationCommand(authentication, jwtToken); if (cmd == null) { return; } if (cmd.isRequiredValidJwt()) { if (jwtToken == null || !authenticationService.validateJwtToken(jwtToken).isAuthenticated()) { throw new RequestAbortException(new TokenNotValidException("JWT Token is not authenticated")); } } } catch (AuthenticationException ae) { throw new RequestAbortException(ae); } cmd.applyToRequest(request); } }  }
ServiceAuthenticationDecorator { public void process(HttpRequest request) { final RequestContext context = RequestContext.getCurrentContext(); if (context.get(AUTHENTICATION_COMMAND_KEY) instanceof ServiceAuthenticationServiceImpl.UniversalAuthenticationCommand) { InstanceInfo info = RequestContextUtils.getInstanceInfo().orElseThrow( () -> new RequestContextNotPreparedException("InstanceInfo of loadbalanced instance is not present in RequestContext") ); final Authentication authentication = serviceAuthenticationService.getAuthentication(info); AuthenticationCommand cmd; try { final String jwtToken = authenticationService.getJwtTokenFromRequest(context.getRequest()).orElse(null); cmd = serviceAuthenticationService.getAuthenticationCommand(authentication, jwtToken); if (cmd == null) { return; } if (cmd.isRequiredValidJwt()) { if (jwtToken == null || !authenticationService.validateJwtToken(jwtToken).isAuthenticated()) { throw new RequestAbortException(new TokenNotValidException("JWT Token is not authenticated")); } } } catch (AuthenticationException ae) { throw new RequestAbortException(ae); } cmd.applyToRequest(request); } }  void process(HttpRequest request); }
ServiceAuthenticationDecorator { public void process(HttpRequest request) { final RequestContext context = RequestContext.getCurrentContext(); if (context.get(AUTHENTICATION_COMMAND_KEY) instanceof ServiceAuthenticationServiceImpl.UniversalAuthenticationCommand) { InstanceInfo info = RequestContextUtils.getInstanceInfo().orElseThrow( () -> new RequestContextNotPreparedException("InstanceInfo of loadbalanced instance is not present in RequestContext") ); final Authentication authentication = serviceAuthenticationService.getAuthentication(info); AuthenticationCommand cmd; try { final String jwtToken = authenticationService.getJwtTokenFromRequest(context.getRequest()).orElse(null); cmd = serviceAuthenticationService.getAuthenticationCommand(authentication, jwtToken); if (cmd == null) { return; } if (cmd.isRequiredValidJwt()) { if (jwtToken == null || !authenticationService.validateJwtToken(jwtToken).isAuthenticated()) { throw new RequestAbortException(new TokenNotValidException("JWT Token is not authenticated")); } } } catch (AuthenticationException ae) { throw new RequestAbortException(ae); } cmd.applyToRequest(request); } }  void process(HttpRequest request);  }