2 * Copyright (c) 2014, 2017 Hewlett-Packard Development Company, L.P. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.aaa.shiro.tokenauthrealm.auth;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
15 import java.io.UnsupportedEncodingException;
16 import java.nio.charset.StandardCharsets;
17 import java.util.Arrays;
18 import java.util.Base64;
19 import java.util.HashMap;
20 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.aaa.api.AuthenticationException;
25 import org.opendaylight.aaa.api.Claim;
26 import org.opendaylight.aaa.api.CredentialAuth;
27 import org.opendaylight.aaa.api.PasswordCredentials;
29 public class HttpBasicAuthTest {
30 private static final String USERNAME = "admin";
31 private static final String PASSWORD = "admin";
32 private static final String DOMAIN = "sdn";
33 private HttpBasicAuth auth;
35 @SuppressWarnings("unchecked")
38 CredentialAuth<PasswordCredentials> mockCredentialAuth = mock(CredentialAuth.class);
39 auth = new HttpBasicAuth(mockCredentialAuth);
40 when(mockCredentialAuth.authenticate(
41 new PasswordCredentialBuilder().setUserName(USERNAME).setPassword(PASSWORD).setDomain(DOMAIN).build()))
42 .thenReturn(new ClaimBuilder().setUser("admin").addRole("admin").setUserId("123").build());
43 when(mockCredentialAuth.authenticate(
44 new PasswordCredentialBuilder().setUserName(USERNAME).setPassword("bozo").setDomain(DOMAIN).build()))
45 .thenThrow(new AuthenticationException("barf"));
49 public void testValidateOk() throws UnsupportedEncodingException {
50 String data = USERNAME + ":" + PASSWORD + ":" + DOMAIN;
51 Map<String, List<String>> headers = new HashMap<>();
52 headers.put("Authorization", Arrays.asList(
53 "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)))));
54 Claim claim = auth.validate(headers);
56 assertEquals(USERNAME, claim.user());
57 assertEquals("admin", claim.roles().iterator().next());
60 @Test(expected = AuthenticationException.class)
61 public void testValidateBadPassword() throws UnsupportedEncodingException {
62 String data = USERNAME + ":bozo:" + DOMAIN;
63 Map<String, List<String>> headers = new HashMap<>();
64 headers.put("Authorization", Arrays.asList(
65 "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)))));
66 auth.validate(headers);
69 @Test(expected = AuthenticationException.class)
70 public void testValidateBadPasswordNoDomain() throws UnsupportedEncodingException {
71 String data = USERNAME + ":bozo";
72 Map<String, List<String>> headers = new HashMap<>();
73 headers.put("Authorization", Arrays.asList(
74 "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)))));
75 auth.validate(headers);
78 @Test(expected = AuthenticationException.class)
79 public void testBadHeaderFormatNoPassword() throws UnsupportedEncodingException {
80 // just provide the username
81 String data = USERNAME;
82 Map<String, List<String>> headers = new HashMap<>();
83 headers.put("Authorization", Arrays.asList(
84 "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)))));
85 auth.validate(headers);
88 @Test(expected = AuthenticationException.class)
89 public void testBadHeaderFormat() throws UnsupportedEncodingException {
91 String data = USERNAME + "$" + PASSWORD;
92 Map<String, List<String>> headers = new HashMap<>();
93 headers.put("Authorization", Arrays.asList(
94 "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)))));
95 auth.validate(headers);