4ee439df6b43e64cea8142e65abf510c16e02c16
[moon.git] /
1 /*
2  * Copyright (c) 2014, 2015 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.aaa.basic;
10
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;
15
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;
21 import java.util.Map;
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;
29
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;
35
36     @SuppressWarnings("unchecked")
37     @Before
38     public void setup() {
39         auth = new HttpBasicAuth();
40         auth.credentialAuth = mock(CredentialAuth.class);
41         when(
42                 auth.credentialAuth.authenticate(new PasswordCredentialBuilder()
43                         .setUserName(USERNAME).setPassword(PASSWORD).setDomain(DOMAIN).build()))
44                 .thenReturn(
45                         new ClaimBuilder().setUser("admin").addRole("admin").setUserId("123")
46                                 .build());
47         when(
48                 auth.credentialAuth.authenticate(new PasswordCredentialBuilder()
49                         .setUserName(USERNAME).setPassword("bozo").setDomain(DOMAIN).build()))
50                 .thenThrow(new AuthenticationException("barf"));
51     }
52
53     @Test
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);
60         assertNotNull(claim);
61         assertEquals(USERNAME, claim.user());
62         assertEquals("admin", claim.roles().iterator().next());
63     }
64
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);
72     }
73
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);
81     }
82
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);
91     }
92
93     @Test(expected = AuthenticationException.class)
94     public void testBadHeaderFormat() throws UnsupportedEncodingException {
95         // provide username:
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);
101     }
102 }