07efbcec3c862d3ecab33c86c96bf95a23590669
[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.AuthService;
47 import org.onap.ccsdk.features.sdnr.wt.oauthprovider.providers.TokenCreator;
48 import org.opendaylight.aaa.api.shiro.principal.ODLPrincipal;
49 import org.opendaylight.aaa.shiro.web.env.ThreadLocals;
50 import org.opendaylight.aaa.tokenauthrealm.auth.AuthenticationManager;
51 import org.opendaylight.aaa.tokenauthrealm.auth.TokenAuthenticators;
52
53 public class TestRealm {
54
55     private static OAuth2RealmToTest realm;
56     private static TokenCreator tokenCreator;
57
58     @BeforeClass
59     public static void init() throws IllegalArgumentException, Exception {
60         ThreadLocals.AUTH_SETVICE_TL.set(new AuthenticationManager());
61         ThreadLocals.TOKEN_AUTHENICATORS_TL.set(new TokenAuthenticators());
62         try {
63             Config config = Config.getInstance(TestConfig.TEST_CONFIG_FILENAME);
64             tokenCreator = TokenCreator.getInstance(config);
65             realm = new OAuth2RealmToTest();
66         } catch (IOException e) {
67             fail(e.getMessage());
68         }
69     }
70
71
72     @Test
73     public void testTokenSupport() {
74         assertTrue(realm.supports(new UsernamePasswordToken()));
75         assertTrue(realm.supports(new BearerToken("")));
76     }
77
78
79     @Test
80     public void testAuthorizationInfo() {
81         //bearer token use case
82         PrincipalCollection c = mock(PrincipalCollection.class);
83         final List<String> roles = Arrays.asList("admin", "provision");
84         UserTokenPayload userData = createUserData("", roles);
85
86         DecodedJWT decodedJwt = tokenCreator.verify(tokenCreator.createNewJWT(userData).getToken());
87         when(c.getPrimaryPrincipal()).thenReturn(decodedJwt);
88
89         AuthorizationInfo ai = realm.doGetAuthorizationInfo(c);
90         for (String role : roles) {
91             assertTrue(ai.getRoles().contains(role));
92         }
93         assertEquals(roles.size(), ai.getRoles().size());
94         //odl token use case
95         ODLPrincipal principal = mock(ODLPrincipal.class);
96         when(principal.getRoles()).thenReturn(new HashSet<String>(roles));
97         PrincipalCollection c2 = mock(PrincipalCollection.class);
98         when(c2.getPrimaryPrincipal()).thenReturn(principal);
99         ai = realm.doGetAuthorizationInfo(c2);
100         for (String role : roles) {
101             assertTrue(ai.getRoles().contains(role));
102         }
103         assertEquals(roles.size(), ai.getRoles().size());
104
105     }
106
107     @Test
108     public void testUrlTrimming(){
109         final String internalUrl="https://test.identity.onap:49333";
110         final String externalUrl="https://test.identity.onap:49333";
111         final String testUrl1 = "/my/token/endpoint";
112         final String testUrl2 = internalUrl+testUrl1;
113         final String testUrl3 = externalUrl+testUrl1;
114
115         assertEquals(testUrl1, AuthService.trimUrl(internalUrl, testUrl1));
116         assertEquals(testUrl1, AuthService.trimUrl(internalUrl, testUrl2));
117         assertEquals(testUrl1, AuthService.trimUrl(externalUrl, testUrl3));
118
119         assertEquals(testUrl2, AuthService.extendUrl(internalUrl, testUrl3));
120
121
122
123     }
124     @Test
125     public void testAssertCredentialsMatch() {
126         //bearer token use case
127         UserTokenPayload userData = createUserData("", Arrays.asList("admin", "provision"));
128         AuthenticationToken atoken = new BearerToken(tokenCreator.createNewJWT(userData).getToken());
129         AuthenticationInfo ai = null;
130         try {
131             realm.assertCredentialsMatch(atoken, ai);
132         } catch (AuthenticationException e) {
133             fail(e.getMessage());
134         }
135         //odl token use case
136         atoken = new UsernamePasswordToken("admin", "admin");
137         try {
138             realm.assertCredentialsMatch(atoken, ai);
139         } catch (AuthenticationException e) {
140             fail(e.getMessage());
141         }
142     }
143
144     @Test
145     public void testAuthenticationInfo() {
146         //bearer token use case
147         UserTokenPayload userData = createUserData("", Arrays.asList("admin", "provision"));
148         AuthenticationToken atoken = new BearerToken(tokenCreator.createNewJWT(userData).getToken());
149         AuthenticationInfo ai = null;
150         try {
151             ai = realm.doGetAuthenticationInfo(atoken);
152         } catch (AuthenticationException e) {
153             fail(e.getMessage());
154         }
155         //odl token use case
156         ai=null;
157         atoken = new UsernamePasswordToken("admin", "admin");
158         try {
159             ai = realm.doGetAuthenticationInfo(atoken);
160         } catch (AuthenticationException e) {
161             fail(e.getMessage());
162         }
163     }
164
165     private static UserTokenPayload createUserData(String username, List<String> roles) {
166         UserTokenPayload userData = new UserTokenPayload();
167         userData.setExp(tokenCreator.getDefaultExp());
168         userData.setFamilyName("");
169         userData.setGivenName("");
170         userData.setPreferredUsername(username);
171         userData.setRoles(roles);
172         return userData;
173     }
174
175     public static class OAuth2RealmToTest extends OAuth2Realm {
176
177         public OAuth2RealmToTest() throws IllegalArgumentException, Exception {
178             super();
179         }
180
181         @Override
182         public AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg) {
183             return super.doGetAuthorizationInfo(arg);
184         }
185
186         @Override
187         public void assertCredentialsMatch(AuthenticationToken atoken, AuthenticationInfo ai)
188                 throws AuthenticationException {
189             super.assertCredentialsMatch(atoken, ai);
190         }
191
192         @Override
193         public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
194             return super.doGetAuthenticationInfo(token);
195         }
196     }
197 }