42ea1f9957824a45ebc80ac7389720f20839919f
[aaa.git] /
1 /*
2  * Copyright (c) 2014, 2017 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.impl.shiro.tokenauthrealm.auth;
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 java.io.UnsupportedEncodingException;
17 import java.util.Arrays;
18 import java.util.Base64;
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.api.AuthenticationException;
25 import org.opendaylight.aaa.api.Claim;
26 import org.opendaylight.aaa.api.CredentialAuth;
27
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;
33
34     @SuppressWarnings("unchecked")
35     @Before
36     public void setup() {
37         auth = new HttpBasicAuth();
38         auth.credentialAuth = mock(CredentialAuth.class);
39         when(
40                 auth.credentialAuth.authenticate(new PasswordCredentialBuilder()
41                         .setUserName(USERNAME).setPassword(PASSWORD).setDomain(DOMAIN).build()))
42                 .thenReturn(
43                         new ClaimBuilder().setUser("admin").addRole("admin").setUserId("123")
44                                 .build());
45         when(
46                 auth.credentialAuth.authenticate(new PasswordCredentialBuilder()
47                         .setUserName(USERNAME).setPassword("bozo").setDomain(DOMAIN).build()))
48                 .thenThrow(new AuthenticationException("barf"));
49     }
50
51     @Test
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);
58         assertNotNull(claim);
59         assertEquals(USERNAME, claim.user());
60         assertEquals("admin", claim.roles().iterator().next());
61     }
62
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);
70     }
71
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);
79     }
80
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);
89     }
90
91     @Test(expected = AuthenticationException.class)
92     public void testBadHeaderFormat() throws UnsupportedEncodingException {
93         // provide username:
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);
99     }
100 }