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
9 package org.opendaylight.aaa.impl.shiro.tokenauthrealm.auth;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
16 import java.io.UnsupportedEncodingException;
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;
28 public class HttpBasicAuthTest {
29 private static final String USERNAME = "admin";
30 private static final String PASSWORD = "admin";
31 private static final String DOMAIN = "sdn";
32 private HttpBasicAuth auth;
34 @SuppressWarnings("unchecked")
37 auth = new HttpBasicAuth();
38 auth.credentialAuth = mock(CredentialAuth.class);
40 auth.credentialAuth.authenticate(new PasswordCredentialBuilder()
41 .setUserName(USERNAME).setPassword(PASSWORD).setDomain(DOMAIN).build()))
43 new ClaimBuilder().setUser("admin").addRole("admin").setUserId("123")
46 auth.credentialAuth.authenticate(new PasswordCredentialBuilder()
47 .setUserName(USERNAME).setPassword("bozo").setDomain(DOMAIN).build()))
48 .thenThrow(new AuthenticationException("barf"));
52 public void testValidateOk() throws UnsupportedEncodingException {
53 String data = USERNAME + ":" + PASSWORD + ":" + DOMAIN;
54 Map<String, List<String>> headers = new HashMap<>();
55 headers.put("Authorization",
56 Arrays.asList("Basic " + new String(Base64.getEncoder().encode(data.getBytes("utf-8")))));
57 Claim claim = auth.validate(headers);
59 assertEquals(USERNAME, claim.user());
60 assertEquals("admin", claim.roles().iterator().next());
63 @Test(expected = AuthenticationException.class)
64 public void testValidateBadPassword() throws UnsupportedEncodingException {
65 String data = USERNAME + ":bozo:" + DOMAIN;
66 Map<String, List<String>> headers = new HashMap<>();
67 headers.put("Authorization",
68 Arrays.asList("Basic " + new String(Base64.getEncoder().encode(data.getBytes("utf-8")))));
69 auth.validate(headers);
72 @Test(expected = AuthenticationException.class)
73 public void testValidateBadPasswordNoDomain() throws UnsupportedEncodingException {
74 String data = USERNAME + ":bozo";
75 Map<String, List<String>> headers = new HashMap<>();
76 headers.put("Authorization",
77 Arrays.asList("Basic " + new String(Base64.getEncoder().encode(data.getBytes("utf-8")))));
78 auth.validate(headers);
81 @Test(expected = AuthenticationException.class)
82 public void testBadHeaderFormatNoPassword() throws UnsupportedEncodingException {
83 // just provide the username
84 String data = USERNAME;
85 Map<String, List<String>> headers = new HashMap<>();
86 headers.put("Authorization",
87 Arrays.asList("Basic " + new String(Base64.getEncoder().encode(data.getBytes("utf-8")))));
88 auth.validate(headers);
91 @Test(expected = AuthenticationException.class)
92 public void testBadHeaderFormat() throws UnsupportedEncodingException {
94 String data = USERNAME + "$" + PASSWORD;
95 Map<String, List<String>> headers = new HashMap<>();
96 headers.put("Authorization",
97 Arrays.asList("Basic " + new String(Base64.getEncoder().encode(data.getBytes("utf-8")))));
98 auth.validate(headers);