a8838480aea55605b5761f49e29d02a08487c9a6
[aaa.git] / aaa-shiro / impl / src / test / java / org / opendaylight / aaa / shiro / tokenauthrealm / auth / HttpBasicAuthTest.java
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 package org.opendaylight.aaa.shiro.tokenauthrealm.auth;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14
15 import java.io.UnsupportedEncodingException;
16 import java.nio.charset.StandardCharsets;
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 import org.opendaylight.aaa.api.PasswordCredentials;
28
29 public class HttpBasicAuthTest {
30     private static final String USERNAME = "admin";
31     private static final String PASSWORD = "admin";
32     private static final String DOMAIN = "sdn";
33     private HttpBasicAuth auth;
34
35     @SuppressWarnings("unchecked")
36     @Before
37     public void setup() {
38         CredentialAuth<PasswordCredentials> mockCredentialAuth = mock(CredentialAuth.class);
39         auth = new HttpBasicAuth(mockCredentialAuth);
40         when(mockCredentialAuth.authenticate(
41                 new PasswordCredentialBuilder().setUserName(USERNAME).setPassword(PASSWORD).setDomain(DOMAIN).build()))
42                         .thenReturn(new ClaimBuilder().setUser("admin").addRole("admin").setUserId("123").build());
43         when(mockCredentialAuth.authenticate(
44                 new PasswordCredentialBuilder().setUserName(USERNAME).setPassword("bozo").setDomain(DOMAIN).build()))
45                         .thenThrow(new AuthenticationException("barf"));
46     }
47
48     @Test
49     public void testValidateOk() throws UnsupportedEncodingException {
50         String data = USERNAME + ":" + PASSWORD + ":" + DOMAIN;
51         Map<String, List<String>> headers = new HashMap<>();
52         headers.put("Authorization", Arrays.asList(
53             "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)))));
54         Claim claim = auth.validate(headers);
55         assertNotNull(claim);
56         assertEquals(USERNAME, claim.user());
57         assertEquals("admin", claim.roles().iterator().next());
58     }
59
60     @Test(expected = AuthenticationException.class)
61     public void testValidateBadPassword() throws UnsupportedEncodingException {
62         String data = USERNAME + ":bozo:" + DOMAIN;
63         Map<String, List<String>> headers = new HashMap<>();
64         headers.put("Authorization", Arrays.asList(
65             "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)))));
66         auth.validate(headers);
67     }
68
69     @Test(expected = AuthenticationException.class)
70     public void testValidateBadPasswordNoDomain() throws UnsupportedEncodingException {
71         String data = USERNAME + ":bozo";
72         Map<String, List<String>> headers = new HashMap<>();
73         headers.put("Authorization", Arrays.asList(
74             "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)))));
75         auth.validate(headers);
76     }
77
78     @Test(expected = AuthenticationException.class)
79     public void testBadHeaderFormatNoPassword() throws UnsupportedEncodingException {
80         // just provide the username
81         String data = USERNAME;
82         Map<String, List<String>> headers = new HashMap<>();
83         headers.put("Authorization", Arrays.asList(
84             "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)))));
85         auth.validate(headers);
86     }
87
88     @Test(expected = AuthenticationException.class)
89     public void testBadHeaderFormat() throws UnsupportedEncodingException {
90         // provide username:
91         String data = USERNAME + "$" + PASSWORD;
92         Map<String, List<String>> headers = new HashMap<>();
93         headers.put("Authorization", Arrays.asList(
94             "Basic " + new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)))));
95         auth.validate(headers);
96     }
97 }