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.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;
 
  53 public class TestRealm {
 
  55     private static OAuth2RealmToTest realm;
 
  56     private static TokenCreator tokenCreator;
 
  59     public static void init() throws IllegalArgumentException, Exception {
 
  60         ThreadLocals.AUTH_SETVICE_TL.set(new AuthenticationManager());
 
  61         ThreadLocals.TOKEN_AUTHENICATORS_TL.set(new TokenAuthenticators());
 
  63             Config config = Config.getInstance(TestConfig.TEST_CONFIG_FILENAME);
 
  64             tokenCreator = TokenCreator.getInstance(config);
 
  65             realm = new OAuth2RealmToTest();
 
  66         } catch (IOException e) {
 
  73     public void testTokenSupport() {
 
  74         assertTrue(realm.supports(new UsernamePasswordToken()));
 
  75         assertTrue(realm.supports(new BearerToken("")));
 
  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);
 
  86         DecodedJWT decodedJwt = tokenCreator.verify(tokenCreator.createNewJWT(userData).getToken());
 
  87         when(c.getPrimaryPrincipal()).thenReturn(decodedJwt);
 
  89         AuthorizationInfo ai = realm.doGetAuthorizationInfo(c);
 
  90         for (String role : roles) {
 
  91             assertTrue(ai.getRoles().contains(role));
 
  93         assertEquals(roles.size(), ai.getRoles().size());
 
  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));
 
 103         assertEquals(roles.size(), ai.getRoles().size());
 
 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;
 
 115         assertEquals(testUrl1, AuthService.trimUrl(internalUrl, testUrl1));
 
 116         assertEquals(testUrl1, AuthService.trimUrl(internalUrl, testUrl2));
 
 117         assertEquals(testUrl1, AuthService.trimUrl(externalUrl, testUrl3));
 
 119         assertEquals(testUrl2, AuthService.extendUrl(internalUrl, testUrl3));
 
 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;
 
 131             realm.assertCredentialsMatch(atoken, ai);
 
 132         } catch (AuthenticationException e) {
 
 133             fail(e.getMessage());
 
 136         atoken = new UsernamePasswordToken("admin", "admin");
 
 138             realm.assertCredentialsMatch(atoken, ai);
 
 139         } catch (AuthenticationException e) {
 
 140             fail(e.getMessage());
 
 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;
 
 151             ai = realm.doGetAuthenticationInfo(atoken);
 
 152         } catch (AuthenticationException e) {
 
 153             fail(e.getMessage());
 
 157         atoken = new UsernamePasswordToken("admin", "admin");
 
 159             ai = realm.doGetAuthenticationInfo(atoken);
 
 160         } catch (AuthenticationException e) {
 
 161             fail(e.getMessage());
 
 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);
 
 175     public static class OAuth2RealmToTest extends OAuth2Realm {
 
 177         public OAuth2RealmToTest() throws IllegalArgumentException, Exception {
 
 182         public AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg) {
 
 183             return super.doGetAuthorizationInfo(arg);
 
 187         public void assertCredentialsMatch(AuthenticationToken atoken, AuthenticationInfo ai)
 
 188                 throws AuthenticationException {
 
 189             super.assertCredentialsMatch(atoken, ai);
 
 193         public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
 
 194             return super.doGetAuthenticationInfo(token);