2 * Copyright (c) 2014, 2015 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.basic;
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.HashMap;
19 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.aaa.ClaimBuilder;
25 import org.opendaylight.aaa.PasswordCredentialBuilder;
26 import org.opendaylight.aaa.api.AuthenticationException;
27 import org.opendaylight.aaa.api.Claim;
28 import org.opendaylight.aaa.api.CredentialAuth;
30 import com.sun.jersey.core.util.Base64;
32 public class HttpBasicAuthTest {
33 private static final String USERNAME = "admin";
34 private static final String PASSWORD = "admin";
35 private static final String DOMAIN = "sdn";
36 private HttpBasicAuth auth;
38 @SuppressWarnings("unchecked")
41 auth = new HttpBasicAuth();
42 auth.credentialAuth = mock(CredentialAuth.class);
44 auth.credentialAuth.authenticate(new PasswordCredentialBuilder()
45 .setUserName(USERNAME).setPassword(PASSWORD).setDomain(DOMAIN).build())).thenReturn(
46 new ClaimBuilder().setUser("admin").addRole("admin").setUserId("123").build());
48 auth.credentialAuth.authenticate(new PasswordCredentialBuilder()
49 .setUserName(USERNAME).setPassword("bozo").setDomain(DOMAIN).build())).thenThrow(new AuthenticationException("barf"));
53 public void testValidateOk() throws UnsupportedEncodingException {
54 String data = USERNAME + ":" + PASSWORD + ":" + DOMAIN;
55 Map<String, List<String>> headers = new HashMap<>();
58 Arrays.asList("Basic "
59 + new String(Base64.encode(data.getBytes("utf-8")))));
60 Claim claim = auth.validate(headers);
62 assertEquals(USERNAME, claim.user());
63 assertEquals("admin", claim.roles().iterator().next());
66 @Test(expected = AuthenticationException.class)
67 public void testValidateBadPassword() throws UnsupportedEncodingException {
68 String data = USERNAME + ":bozo:" + DOMAIN;
69 Map<String, List<String>> headers = new HashMap<>();
72 Arrays.asList("Basic "
73 + new String(Base64.encode(data.getBytes("utf-8")))));
74 auth.validate(headers);
77 @Test(expected = AuthenticationException.class)
78 public void testValidateBadPasswordNoDOMAIN() throws UnsupportedEncodingException {
79 String data = USERNAME + ":bozo";
80 Map<String, List<String>> headers = new HashMap<>();
83 Arrays.asList("Basic "
84 + new String(Base64.encode(data.getBytes("utf-8")))));
85 auth.validate(headers);
88 @Test(expected = AuthenticationException.class)
89 public void testBadHeaderFormatNoPassword() throws UnsupportedEncodingException {
90 // just provide the username
91 String data = USERNAME;
92 Map<String, List<String>> headers = new HashMap<>();
95 Arrays.asList("Basic "
96 + new String(Base64.encode(data.getBytes("utf-8")))));
97 auth.validate(headers);
100 @Test(expected = AuthenticationException.class)
101 public void testBadHeaderFormat() throws UnsupportedEncodingException {
103 String data = USERNAME + "$" + PASSWORD;
104 Map<String, List<String>> headers = new HashMap<>();
107 Arrays.asList("Basic "
108 + new String(Base64.encode(data.getBytes("utf-8")))));
109 auth.validate(headers);