|
| 1 | +/* |
| 2 | + * Copyright (c) 2024, WSO2 LLC. (https://www.wso2.com) |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.wso2.choreo.connect.enforcer.security.jwt; |
| 20 | + |
| 21 | +import com.google.common.cache.LoadingCache; |
| 22 | +import org.junit.Assert; |
| 23 | +import org.junit.Before; |
| 24 | +import org.junit.Test; |
| 25 | +import org.junit.runner.RunWith; |
| 26 | +import org.mockito.Mockito; |
| 27 | +import org.powermock.api.mockito.PowerMockito; |
| 28 | +import org.powermock.core.classloader.annotations.PrepareForTest; |
| 29 | +import org.powermock.modules.junit4.PowerMockRunner; |
| 30 | +import org.wso2.carbon.apimgt.common.gateway.dto.JWTConfigurationDto; |
| 31 | +import org.wso2.choreo.connect.enforcer.common.CacheProvider; |
| 32 | +import org.wso2.choreo.connect.enforcer.commons.model.APIConfig; |
| 33 | +import org.wso2.choreo.connect.enforcer.commons.model.RequestContext; |
| 34 | +import org.wso2.choreo.connect.enforcer.config.ConfigHolder; |
| 35 | +import org.wso2.choreo.connect.enforcer.config.EnforcerConfig; |
| 36 | +import org.wso2.choreo.connect.enforcer.config.dto.APIKeyDTO; |
| 37 | +import org.wso2.choreo.connect.enforcer.config.dto.CacheDto; |
| 38 | +import org.wso2.choreo.connect.enforcer.exception.APISecurityException; |
| 39 | + |
| 40 | +import java.util.HashMap; |
| 41 | +import java.util.Map; |
| 42 | +import java.util.Optional; |
| 43 | + |
| 44 | +@RunWith(PowerMockRunner.class) |
| 45 | +@PrepareForTest({APIKeyUtils.class, CacheProvider.class, ConfigHolder.class}) |
| 46 | +public class APIKeyAuthenticatorTest { |
| 47 | + |
| 48 | + @Before |
| 49 | + public void setup() { |
| 50 | + PowerMockito.mockStatic(ConfigHolder.class); |
| 51 | + ConfigHolder configHolder = PowerMockito.mock(ConfigHolder.class); |
| 52 | + PowerMockito.when(ConfigHolder.getInstance()).thenReturn(configHolder); |
| 53 | + EnforcerConfig enforcerConfig = PowerMockito.mock(EnforcerConfig.class); |
| 54 | + PowerMockito.when(configHolder.getConfig()).thenReturn(enforcerConfig); |
| 55 | + APIKeyDTO apiKeyDTO = PowerMockito.mock(APIKeyDTO.class); |
| 56 | + PowerMockito.when(enforcerConfig.getApiKeyConfig()).thenReturn(apiKeyDTO); |
| 57 | + PowerMockito.when(ConfigHolder.getInstance().getConfig().getApiKeyConfig() |
| 58 | + .getApiKeyInternalHeader()).thenReturn("choreo-api-key"); |
| 59 | + CacheDto cacheDto = Mockito.mock(CacheDto.class); |
| 60 | + Mockito.when(cacheDto.isEnabled()).thenReturn(true); |
| 61 | + Mockito.when(enforcerConfig.getCacheDto()).thenReturn(cacheDto); |
| 62 | + JWTConfigurationDto jwtConfigurationDto = Mockito.mock(JWTConfigurationDto.class); |
| 63 | + Mockito.when(jwtConfigurationDto.isEnabled()).thenReturn(false); |
| 64 | + Mockito.when(enforcerConfig.getJwtConfigurationDto()).thenReturn(jwtConfigurationDto); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void retrieveTokenFromRequestCtxTest_invalidKey() { |
| 69 | + |
| 70 | + RequestContext.Builder requestContextBuilder = new RequestContext.Builder("/api-key"); |
| 71 | + requestContextBuilder.matchedAPI(new APIConfig.Builder("Petstore") |
| 72 | + .basePath("/test") |
| 73 | + .apiType("REST") |
| 74 | + .build()); |
| 75 | + Map<String, String> headersMap = new HashMap<>(); |
| 76 | + headersMap.put("choreo-api-key", |
| 77 | + "chk_eyJrZXkiOiJieTlpYXQ5d3MycDY0dWF6anFkbzQ4cnAyYnY3aWoxdWRuYmRzNzN6ZWx5OWNoZHJ2YiJ97JYpag"); |
| 78 | + requestContextBuilder.headers(headersMap); |
| 79 | + RequestContext requestContext = requestContextBuilder.build(); |
| 80 | + |
| 81 | + APIKeyAuthenticator apiKeyAuthenticator = new APIKeyAuthenticator(); |
| 82 | + Assert.assertThrows(APISecurityException.class, () -> |
| 83 | + apiKeyAuthenticator.retrieveTokenFromRequestCtx(requestContext)); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void retrieveTokenFromRequestCtxTest_cached_validKey() throws APISecurityException { |
| 88 | + |
| 89 | + String mockJWT = "eyJrZXkiOiJieTlpYXQ5d3MycDY0dWF6anFkbzQ4cnAyYnY3aWoxdWRuYmRzNzN6ZWx5OWNoZHJ2YiJ97JYPAg"; |
| 90 | + PowerMockito.mockStatic(APIKeyUtils.class); |
| 91 | + PowerMockito.when(APIKeyUtils.isValidAPIKey(Mockito.anyString())).thenReturn(true); |
| 92 | + PowerMockito.when(APIKeyUtils.generateAPIKeyHash(Mockito.anyString())).thenReturn("key_hash"); |
| 93 | + PowerMockito.when(APIKeyUtils.isJWTExpired(Mockito.anyString())).thenReturn(false); |
| 94 | + |
| 95 | + PowerMockito.mockStatic(CacheProvider.class); |
| 96 | + LoadingCache gatewayAPIKeyJWTCache = PowerMockito.mock(LoadingCache.class); |
| 97 | + PowerMockito.when(CacheProvider.getGatewayAPIKeyJWTCache()).thenReturn(gatewayAPIKeyJWTCache); |
| 98 | + PowerMockito.when(gatewayAPIKeyJWTCache.getIfPresent(Mockito.anyString())).thenReturn(mockJWT); |
| 99 | + |
| 100 | + RequestContext.Builder requestContextBuilder = new RequestContext.Builder("/api-key"); |
| 101 | + requestContextBuilder.matchedAPI(new APIConfig.Builder("Petstore") |
| 102 | + .basePath("/test") |
| 103 | + .apiType("REST") |
| 104 | + .build()); |
| 105 | + Map<String, String> headersMap = new HashMap<>(); |
| 106 | + headersMap.put("choreo-api-key", |
| 107 | + "chk_eyJhdHRyMSI6InYxIiwiY29ubmVjdGlvbklkIjoiNjAwM2EzYjctYWYwZi00ZmIzLTg1M2UtYTY1NjJiMjM0N" + |
| 108 | + "WYyIiwia2V5IjoieG5lcGVxZmZ4eWx2Y2Q4a3FnNHprZDFpMHoxMnA2dTBqcW50aDUyM3JlN292a2pudncifQBdZRRQ"); |
| 109 | + requestContextBuilder.headers(headersMap); |
| 110 | + RequestContext requestContext = requestContextBuilder.build(); |
| 111 | + |
| 112 | + APIKeyAuthenticator apiKeyAuthenticator = new APIKeyAuthenticator(); |
| 113 | + String token = apiKeyAuthenticator.retrieveTokenFromRequestCtx(requestContext); |
| 114 | + Assert.assertEquals(mockJWT, token); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void retrieveTokenFromRequestCtxTest_validKey() throws APISecurityException { |
| 119 | + |
| 120 | + PowerMockito.mockStatic(APIKeyUtils.class); |
| 121 | + String mockJWT = "eyJrZXkiOiJieTlpYXQ5d3MycDY0dWF6anFkbzQ4cnAyYnY3aWoxdWRuYmRzNzN6ZWx5OWNoZHJ2YiJ97JYPAg"; |
| 122 | + PowerMockito.when(APIKeyUtils.exchangeAPIKeyToJWT(Mockito.anyString())).thenReturn(Optional.of(mockJWT)); |
| 123 | + PowerMockito.when(APIKeyUtils.isValidAPIKey(Mockito.anyString())).thenReturn(true); |
| 124 | + PowerMockito.when(APIKeyUtils.generateAPIKeyHash(Mockito.anyString())).thenReturn("key_hash"); |
| 125 | + |
| 126 | + PowerMockito.mockStatic(CacheProvider.class); |
| 127 | + LoadingCache gatewayAPIKeyJWTCache = PowerMockito.mock(LoadingCache.class); |
| 128 | + PowerMockito.when(CacheProvider.getGatewayAPIKeyJWTCache()).thenReturn(gatewayAPIKeyJWTCache); |
| 129 | + PowerMockito.when(gatewayAPIKeyJWTCache.getIfPresent(Mockito.anyString())).thenReturn(null); |
| 130 | + |
| 131 | + RequestContext.Builder requestContextBuilder = new RequestContext.Builder("/api-key"); |
| 132 | + requestContextBuilder.matchedAPI(new APIConfig.Builder("Petstore") |
| 133 | + .basePath("/test") |
| 134 | + .apiType("REST") |
| 135 | + .build()); |
| 136 | + Map<String, String> headersMap = new HashMap<>(); |
| 137 | + headersMap.put("choreo-api-key", |
| 138 | + "chk_eyJhdHRyMSI6InYxIiwiY29ubmVjdGlvbklkIjoiNjAwM2EzYjctYWYwZi00ZmIzLTg1M2UtYTY1NjJiMjM0N" + |
| 139 | + "WYyIiwia2V5IjoieG5lcGVxZmZ4eWx2Y2Q4a3FnNHprZDFpMHoxMnA2dTBqcW50aDUyM3JlN292a2pudncifQBdZRRQ"); |
| 140 | + requestContextBuilder.headers(headersMap); |
| 141 | + RequestContext requestContext = requestContextBuilder.build(); |
| 142 | + |
| 143 | + APIKeyAuthenticator apiKeyAuthenticator = new APIKeyAuthenticator(); |
| 144 | + String token = apiKeyAuthenticator.retrieveTokenFromRequestCtx(requestContext); |
| 145 | + Assert.assertEquals(mockJWT, token); |
| 146 | + } |
| 147 | +} |
0 commit comments