### Question:
HttpBasicAuth implements TokenAuth { @Override public Authentication validate(final Map<String, List<String>> headers) throws AuthenticationException { if (headers.containsKey(AUTH_HEADER)) { final String authHeader = extractAuthHeader(headers); if (checkAuthHeaderFormat(authHeader)) { String[] creds = extractCredentialArray(authHeader); if (verifyCredentialArray(creds)) { creds = addDomainToCredentialArray(creds); } try { return generateAuthentication(this.credentialAuth, creds); } catch (ArrayIndexOutOfBoundsException e) { final String message = "Login Attempt in Bad Format." + " Please provide user:password in Base64 format."; LOG.info(message); throw new AuthenticationException(message, e); } } } return null; } HttpBasicAuth(CredentialAuth<PasswordCredentials> credentialAuth); @Override Authentication validate(final Map<String, List<String>> headers); static final String AUTH_HEADER; static final String AUTH_SEP; static final String BASIC_PREFIX; static final String DEFAULT_DOMAIN; }### Answer:
@Test public void testValidateOk() throws UnsupportedEncodingException { String data = USERNAME + ":" + PASSWORD + ":" + DOMAIN; Map<String, List<String>> headers = new HashMap<>(); headers.put("Authorization", Arrays.asList( "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8))))); Claim claim = auth.validate(headers); assertNotNull(claim); assertEquals(USERNAME, claim.user()); assertEquals("admin", claim.roles().iterator().next()); }
@Test(expected = AuthenticationException.class) public void testValidateBadPassword() throws UnsupportedEncodingException { String data = USERNAME + ":bozo:" + DOMAIN; Map<String, List<String>> headers = new HashMap<>(); headers.put("Authorization", Arrays.asList( "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8))))); auth.validate(headers); }
@Test(expected = AuthenticationException.class) public void testValidateBadPasswordNoDomain() throws UnsupportedEncodingException { String data = USERNAME + ":bozo"; Map<String, List<String>> headers = new HashMap<>(); headers.put("Authorization", Arrays.asList( "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8))))); auth.validate(headers); }
@Test(expected = AuthenticationException.class) public void testBadHeaderFormatNoPassword() throws UnsupportedEncodingException { String data = USERNAME; Map<String, List<String>> headers = new HashMap<>(); headers.put("Authorization", Arrays.asList( "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8))))); auth.validate(headers); }
@Test(expected = AuthenticationException.class) public void testBadHeaderFormat() throws UnsupportedEncodingException { String data = USERNAME + "$" + PASSWORD; Map<String, List<String>> headers = new HashMap<>(); headers.put("Authorization", Arrays.asList( "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8))))); auth.validate(headers); }