TrustedUserAuth extends UserAuth { public Principal getPrincipal(HttpRequest httpRequest) { String username = AuthUtil.getHeader(httpRequest, USER_HEADER); if (username == null || username.isEmpty()) { log.debug("No username header provided, returning null principal."); return null; } String lookupPermsHeader = AuthUtil.getHeader(httpRequest, LOOKUP_PERMISSIONS_HEADER); if (lookupPermsHeader != null && lookupPermsHeader.equals("true")) { log.debug("Looking up user permissions from user service."); return createPrincipal(username); } log.debug("Returning full trusted user principal."); return new TrustedUserPrincipal(username); } @Inject  TrustedUserAuth(UserServiceAdapter userServiceAdaper, Provider<I18n> i18n,
        PermissionFactory permissionFactory); Principal getPrincipal(HttpRequest httpRequest); static final String USER_HEADER; static final String LOOKUP_PERMISSIONS_HEADER; }
@Test public void missingUsernameHeader() throws Exception { Principal p = auth.getPrincipal(request); assertNull(p); }
@Test public void normalTrustedAuth() throws Exception { headerMap.add(TrustedUserAuth.USER_HEADER, USERNAME); TrustedUserPrincipal p = (TrustedUserPrincipal) auth.getPrincipal(request); verify(userService, never()).validateUser(any(String.class), any(String.class)); verify(userService, never()).findByLogin(any(String.class)); assertTrue(p.hasFullAccess()); }
@Test public void trustedAuthWithPermissionsLookup() throws Exception { headerMap.add(TrustedUserAuth.USER_HEADER, USERNAME); headerMap.add(TrustedUserAuth.LOOKUP_PERMISSIONS_HEADER, "true"); User u = new User(USERNAME, "pass"); when(userService.findByLogin(eq(USERNAME))).thenReturn(u); UserPrincipal p = (UserPrincipal) auth.getPrincipal(request); verify(userService, never()).validateUser(any(String.class), any(String.class)); verify(userService).findByLogin(eq(USERNAME)); assertFalse(p.hasFullAccess()); assertEquals(USERNAME, p.getUsername()); }