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