2 * ============LICENSE_START=======================================================
3 * ONAP : ccsdk features
4 * ================================================================================
5 * Copyright (C) 2021 highstreet technologies GmbH Intellectual Property.
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.ccsdk.features.sdnr.wt.oauthprovider.test;
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;
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;
54 public class TestRealm {
56 private static OAuth2RealmToTest realm;
57 private static TokenCreator tokenCreator;
60 public static void init() throws IllegalArgumentException, Exception {
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) {
74 public void testTokenSupport() {
75 assertTrue(realm.supports(new UsernamePasswordToken()));
76 assertTrue(realm.supports(new BearerToken("")));
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);
87 DecodedJWT decodedJwt = tokenCreator.verify(tokenCreator.createNewJWT(userData).getToken());
88 when(c.getPrimaryPrincipal()).thenReturn(decodedJwt);
90 AuthorizationInfo ai = realm.doGetAuthorizationInfo(c);
91 for (String role : roles) {
92 assertTrue(ai.getRoles().contains(role));
94 assertEquals(roles.size(), ai.getRoles().size());
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));
104 assertEquals(roles.size(), ai.getRoles().size());
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;
116 assertEquals(testUrl1, AuthService.trimUrl(internalUrl, testUrl1));
117 assertEquals(testUrl1, AuthService.trimUrl(internalUrl, testUrl2));
118 assertEquals(testUrl1, AuthService.trimUrl(externalUrl, testUrl3));
120 assertEquals(testUrl2, AuthService.extendUrl(internalUrl, testUrl3));
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;
132 realm.assertCredentialsMatch(atoken, ai);
133 } catch (AuthenticationException e) {
134 fail(e.getMessage());
137 atoken = new UsernamePasswordToken("admin", "admin");
139 realm.assertCredentialsMatch(atoken, ai);
140 } catch (AuthenticationException e) {
141 fail(e.getMessage());
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;
152 ai = realm.doGetAuthenticationInfo(atoken);
153 } catch (AuthenticationException e) {
154 fail(e.getMessage());
158 atoken = new UsernamePasswordToken("admin", "admin");
160 ai = realm.doGetAuthenticationInfo(atoken);
161 } catch (AuthenticationException e) {
162 fail(e.getMessage());
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);
176 public static class OAuth2RealmToTest extends OAuth2Realm {
178 public OAuth2RealmToTest() throws IllegalArgumentException, Exception {
183 public AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg) {
184 return super.doGetAuthorizationInfo(arg);
188 public void assertCredentialsMatch(AuthenticationToken atoken, AuthenticationInfo ai)
189 throws AuthenticationException {
190 super.assertCredentialsMatch(atoken, ai);
194 public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
195 return super.doGetAuthenticationInfo(token);