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;
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;
52 public class TestRealm {
54 private static OAuth2RealmToTest realm;
55 private static TokenCreator tokenCreator;
58 public static void init() throws IllegalArgumentException, Exception {
59 ThreadLocals.AUTH_SETVICE_TL.set(new AuthenticationManager());
60 ThreadLocals.TOKEN_AUTHENICATORS_TL.set(new TokenAuthenticators());
62 Config config = Config.getInstance(TestConfig.TEST_CONFIG_FILENAME);
63 tokenCreator = TokenCreator.getInstance(config);
64 realm = new OAuth2RealmToTest();
65 } catch (IOException e) {
72 public void testTokenSupport() {
73 assertTrue(realm.supports(new UsernamePasswordToken()));
74 assertTrue(realm.supports(new BearerToken("")));
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);
85 DecodedJWT decodedJwt = tokenCreator.verify(tokenCreator.createNewJWT(userData).getToken());
86 when(c.getPrimaryPrincipal()).thenReturn(decodedJwt);
88 AuthorizationInfo ai = realm.doGetAuthorizationInfo(c);
89 for (String role : roles) {
90 assertTrue(ai.getRoles().contains(role));
92 assertEquals(roles.size(), ai.getRoles().size());
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));
102 assertEquals(roles.size(), ai.getRoles().size());
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;
113 realm.assertCredentialsMatch(atoken, ai);
114 } catch (AuthenticationException e) {
115 fail(e.getMessage());
118 atoken = new UsernamePasswordToken("admin", "admin");
120 realm.assertCredentialsMatch(atoken, ai);
121 } catch (AuthenticationException e) {
122 fail(e.getMessage());
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;
133 ai = realm.doGetAuthenticationInfo(atoken);
134 } catch (AuthenticationException e) {
135 fail(e.getMessage());
139 atoken = new UsernamePasswordToken("admin", "admin");
141 ai = realm.doGetAuthenticationInfo(atoken);
142 } catch (AuthenticationException e) {
143 fail(e.getMessage());
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);
157 public static class OAuth2RealmToTest extends OAuth2Realm {
159 public OAuth2RealmToTest() throws IllegalArgumentException, Exception {
164 public AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg) {
165 return super.doGetAuthorizationInfo(arg);
169 public void assertCredentialsMatch(AuthenticationToken atoken, AuthenticationInfo ai)
170 throws AuthenticationException {
171 super.assertCredentialsMatch(atoken, ai);
175 public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
176 return super.doGetAuthenticationInfo(token);