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