d6a9ac5b006d5112c7be4243c713e7d6febc7a83
[ccsdk/features.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : ccsdk features
4  * ================================================================================
5  * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
6  * All rights reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  *
21  */
22 package org.onap.ccsdk.features.sdnr.wt.oauthprovider.test;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
29 import com.auth0.jwt.interfaces.DecodedJWT;
30 import java.io.IOException;
31 import java.util.Arrays;
32 import java.util.HashSet;
33 import java.util.List;
34 import org.apache.shiro.authc.AuthenticationException;
35 import org.apache.shiro.authc.AuthenticationInfo;
36 import org.apache.shiro.authc.AuthenticationToken;
37 import org.apache.shiro.authc.BearerToken;
38 import org.apache.shiro.authc.UsernamePasswordToken;
39 import org.apache.shiro.authz.AuthorizationInfo;
40 import org.apache.shiro.subject.PrincipalCollection;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.OAuth2Realm;
44 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.data.Config;
45 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.data.UserTokenPayload;
46 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.providers.TokenCreator;
47 import org.opendaylight.aaa.api.shiro.principal.ODLPrincipal;
48 import org.opendaylight.aaa.shiro.web.env.ThreadLocals;
49 import org.opendaylight.aaa.tokenauthrealm.auth.AuthenticationManager;
50 import org.opendaylight.aaa.tokenauthrealm.auth.TokenAuthenticators;
51
52 public class TestRealm {
53
54     private static OAuth2RealmToTest realm;
55     private static TokenCreator tokenCreator;
56
57     @BeforeClass
58     public static void init() throws IllegalArgumentException, Exception {
59         ThreadLocals.AUTH_SETVICE_TL.set(new AuthenticationManager());
60         ThreadLocals.TOKEN_AUTHENICATORS_TL.set(new TokenAuthenticators());
61         try {
62             Config config = Config.getInstance(TestConfig.TEST_CONFIG_FILENAME);
63             tokenCreator = TokenCreator.getInstance(config);
64             realm = new OAuth2RealmToTest();
65         } catch (IOException e) {
66             fail(e.getMessage());
67         }
68     }
69
70
71     @Test
72     public void testTokenSupport() {
73         assertTrue(realm.supports(new UsernamePasswordToken()));
74         assertTrue(realm.supports(new BearerToken("")));
75     }
76
77
78     @Test
79     public void testAuthorizationInfo() {
80         //bearer token use case
81         PrincipalCollection c = mock(PrincipalCollection.class);
82         final List<String> roles = Arrays.asList("admin", "provision");
83         UserTokenPayload userData = createUserData("", roles);
84
85         DecodedJWT decodedJwt = tokenCreator.verify(tokenCreator.createNewJWT(userData).getToken());
86         when(c.getPrimaryPrincipal()).thenReturn(decodedJwt);
87
88         AuthorizationInfo ai = realm.doGetAuthorizationInfo(c);
89         for (String role : roles) {
90             assertTrue(ai.getRoles().contains(role));
91         }
92         assertEquals(roles.size(), ai.getRoles().size());
93         //odl token use case
94         ODLPrincipal principal = mock(ODLPrincipal.class);
95         when(principal.getRoles()).thenReturn(new HashSet<String>(roles));
96         PrincipalCollection c2 = mock(PrincipalCollection.class);
97         when(c2.getPrimaryPrincipal()).thenReturn(principal);
98         ai = realm.doGetAuthorizationInfo(c2);
99         for (String role : roles) {
100             assertTrue(ai.getRoles().contains(role));
101         }
102         assertEquals(roles.size(), ai.getRoles().size());
103
104     }
105
106     @Test
107     public void testAssertCredentialsMatch() {
108         //bearer token use case
109         UserTokenPayload userData = createUserData("", Arrays.asList("admin", "provision"));
110         AuthenticationToken atoken = new BearerToken(tokenCreator.createNewJWT(userData).getToken());
111         AuthenticationInfo ai = null;
112         try {
113             realm.assertCredentialsMatch(atoken, ai);
114         } catch (AuthenticationException e) {
115             fail(e.getMessage());
116         }
117         //odl token use case
118         atoken = new UsernamePasswordToken("admin", "admin");
119         try {
120             realm.assertCredentialsMatch(atoken, ai);
121         } catch (AuthenticationException e) {
122             fail(e.getMessage());
123         }
124     }
125
126     @Test
127     public void testAuthenticationInfo() {
128         //bearer token use case
129         UserTokenPayload userData = createUserData("", Arrays.asList("admin", "provision"));
130         AuthenticationToken atoken = new BearerToken(tokenCreator.createNewJWT(userData).getToken());
131         AuthenticationInfo ai = null;
132         try {
133             ai = realm.doGetAuthenticationInfo(atoken);
134         } catch (AuthenticationException e) {
135             fail(e.getMessage());
136         }
137         //odl token use case
138         ai=null;
139         atoken = new UsernamePasswordToken("admin", "admin");
140         try {
141             ai = realm.doGetAuthenticationInfo(atoken);
142         } catch (AuthenticationException e) {
143             fail(e.getMessage());
144         }
145     }
146
147     private static UserTokenPayload createUserData(String username, List<String> roles) {
148         UserTokenPayload userData = new UserTokenPayload();
149         userData.setExp(tokenCreator.getDefaultExp());
150         userData.setFamilyName("");
151         userData.setGivenName("");
152         userData.setPreferredUsername(username);
153         userData.setRoles(roles);
154         return userData;
155     }
156
157     public static class OAuth2RealmToTest extends OAuth2Realm {
158
159         public OAuth2RealmToTest() throws IllegalArgumentException, Exception {
160             super();
161         }
162
163         @Override
164         public AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg) {
165             return super.doGetAuthorizationInfo(arg);
166         }
167
168         @Override
169         public void assertCredentialsMatch(AuthenticationToken atoken, AuthenticationInfo ai)
170                 throws AuthenticationException {
171             super.assertCredentialsMatch(atoken, ai);
172         }
173
174         @Override
175         public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
176             return super.doGetAuthenticationInfo(token);
177         }
178     }
179 }