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 com.sun.jersey.core.util.Base64;
17 import java.io.UnsupportedEncodingException;
18 import java.util.Arrays;
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.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 public class HttpBasicAuthTest {
31 private static final String USERNAME = "admin";
32 private static final String PASSWORD = "admin";
33 private static final String DOMAIN = "sdn";
34 private HttpBasicAuth auth;
36 @SuppressWarnings("unchecked")
39 auth = new HttpBasicAuth();
40 auth.credentialAuth = mock(CredentialAuth.class);
42 auth.credentialAuth.authenticate(new PasswordCredentialBuilder()
43 .setUserName(USERNAME).setPassword(PASSWORD).setDomain(DOMAIN).build()))
45 new ClaimBuilder().setUser("admin").addRole("admin").setUserId("123")
48 auth.credentialAuth.authenticate(new PasswordCredentialBuilder()
49 .setUserName(USERNAME).setPassword("bozo").setDomain(DOMAIN).build()))
50 .thenThrow(new AuthenticationException("barf"));
54 public void testValidateOk() throws UnsupportedEncodingException {
55 String data = USERNAME + ":" + PASSWORD + ":" + DOMAIN;
56 Map<String, List<String>> headers = new HashMap<>();
57 headers.put("Authorization",
58 Arrays.asList("Basic " + new String(Base64.encode(data.getBytes("utf-8")))));
59 Claim claim = auth.validate(headers);
61 assertEquals(USERNAME, claim.user());
62 assertEquals("admin", claim.roles().iterator().next());
65 @Test(expected = AuthenticationException.class)
66 public void testValidateBadPassword() throws UnsupportedEncodingException {
67 String data = USERNAME + ":bozo:" + DOMAIN;
68 Map<String, List<String>> headers = new HashMap<>();
69 headers.put("Authorization",
70 Arrays.asList("Basic " + new String(Base64.encode(data.getBytes("utf-8")))));
71 auth.validate(headers);
74 @Test(expected = AuthenticationException.class)
75 public void testValidateBadPasswordNoDOMAIN() throws UnsupportedEncodingException {
76 String data = USERNAME + ":bozo";
77 Map<String, List<String>> headers = new HashMap<>();
78 headers.put("Authorization",
79 Arrays.asList("Basic " + new String(Base64.encode(data.getBytes("utf-8")))));
80 auth.validate(headers);
83 @Test(expected = AuthenticationException.class)
84 public void testBadHeaderFormatNoPassword() throws UnsupportedEncodingException {
85 // just provide the username
86 String data = USERNAME;
87 Map<String, List<String>> headers = new HashMap<>();
88 headers.put("Authorization",
89 Arrays.asList("Basic " + new String(Base64.encode(data.getBytes("utf-8")))));
90 auth.validate(headers);
93 @Test(expected = AuthenticationException.class)
94 public void testBadHeaderFormat() throws UnsupportedEncodingException {
96 String data = USERNAME + "$" + PASSWORD;
97 Map<String, List<String>> headers = new HashMap<>();
98 headers.put("Authorization",
99 Arrays.asList("Basic " + new String(Base64.encode(data.getBytes("utf-8")))));
100 auth.validate(headers);