|
19 | 19 | package org.wso2.carbon.identity.oauth2.token.handlers.grant;
|
20 | 20 |
|
21 | 21 | import org.mockito.Mock;
|
| 22 | +import org.mockito.MockedStatic; |
22 | 23 | import org.mockito.MockitoAnnotations;
|
23 | 24 | import org.testng.annotations.AfterClass;
|
24 | 25 | import org.testng.annotations.BeforeClass;
|
|
36 | 37 | import org.wso2.carbon.identity.common.testng.WithH2Database;
|
37 | 38 | import org.wso2.carbon.identity.common.testng.WithRealmService;
|
38 | 39 | import org.wso2.carbon.identity.core.util.IdentityTenantUtil;
|
| 40 | +import org.wso2.carbon.identity.core.util.IdentityUtil; |
39 | 41 | import org.wso2.carbon.identity.event.services.IdentityEventService;
|
40 | 42 | import org.wso2.carbon.identity.oauth.IdentityOAuthAdminException;
|
41 | 43 | import org.wso2.carbon.identity.oauth.common.GrantType;
|
|
47 | 49 | import org.wso2.carbon.identity.oauth.internal.OAuthComponentServiceHolder;
|
48 | 50 | import org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception;
|
49 | 51 | import org.wso2.carbon.identity.oauth2.TestConstants;
|
| 52 | +import org.wso2.carbon.identity.oauth2.dao.AccessTokenDAOImpl; |
| 53 | +import org.wso2.carbon.identity.oauth2.dao.OAuthTokenPersistenceFactory; |
50 | 54 | import org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenReqDTO;
|
51 | 55 | import org.wso2.carbon.identity.oauth2.dto.OAuth2AccessTokenRespDTO;
|
52 | 56 | import org.wso2.carbon.identity.oauth2.internal.OAuth2ServiceComponentHolder;
|
53 | 57 | import org.wso2.carbon.identity.oauth2.model.AccessTokenDO;
|
| 58 | +import org.wso2.carbon.identity.oauth2.token.JWTTokenIssuer; |
54 | 59 | import org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext;
|
| 60 | +import org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer; |
| 61 | +import org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding; |
| 62 | +import org.wso2.carbon.identity.oauth2.util.OAuth2Util; |
55 | 63 | import org.wso2.carbon.identity.oauth2.validators.OAuth2ScopeHandler;
|
56 | 64 |
|
57 | 65 | import java.util.Collections;
|
|
63 | 71 |
|
64 | 72 | import static org.mockito.ArgumentMatchers.any;
|
65 | 73 | import static org.mockito.ArgumentMatchers.anyMap;
|
| 74 | +import static org.mockito.ArgumentMatchers.anyString; |
66 | 75 | import static org.mockito.Mockito.mock;
|
| 76 | +import static org.mockito.Mockito.mockStatic; |
67 | 77 | import static org.mockito.Mockito.verify;
|
68 | 78 | import static org.mockito.Mockito.when;
|
69 | 79 | import static org.testng.Assert.assertEquals;
|
@@ -142,6 +152,58 @@ public void tearDown() {
|
142 | 152 | CentralLogMgtServiceComponentHolder.getInstance().setIdentityEventService(null);
|
143 | 153 | }
|
144 | 154 |
|
| 155 | + @Test(dataProvider = "IssueWithRenewDataProvider", expectedExceptions = IdentityOAuth2Exception.class) |
| 156 | + public void testIssueWithRenewWithoutRevokingExistingEnabled |
| 157 | + (boolean cacheEnabled, boolean cacheEntryAvailable, long cachedTokenValidity, |
| 158 | + long cachedRefreshTokenValidity, long dbTokenValidity, long dbRefreshTokenValidity, |
| 159 | + boolean dbEntryAvailable, String dbTokenState, boolean tokenLoggable, boolean isIDPIdColumnEnabled, |
| 160 | + boolean setBindingReference) throws Exception { |
| 161 | + |
| 162 | + OAuth2ServiceComponentHolder.setIDPIdColumnEnabled(isIDPIdColumnEnabled); |
| 163 | + |
| 164 | + Map<String, AuthorizationGrantHandler> supportedGrantTypes = new HashMap<>(); |
| 165 | + supportedGrantTypes.put("refresh_token", refreshGrantHandler); |
| 166 | + |
| 167 | + OAuth2AccessTokenReqDTO oAuth2AccessTokenReqDTO = new OAuth2AccessTokenReqDTO(); |
| 168 | + oAuth2AccessTokenReqDTO.setClientId(clientId); |
| 169 | + oAuth2AccessTokenReqDTO.setGrantType(PASSWORD_GRANT); // Ensure the grant type is valid for renewal |
| 170 | + |
| 171 | + OAuthTokenReqMessageContext tokReqMsgCtx = new OAuthTokenReqMessageContext(oAuth2AccessTokenReqDTO); |
| 172 | + tokReqMsgCtx.setAuthorizedUser(authenticatedUser); |
| 173 | + tokReqMsgCtx.setScope(new String[]{"scope1", "scope2"}); |
| 174 | + |
| 175 | + tokReqMsgCtx.addProperty("OAuthAppDO", oAuthAppDO); |
| 176 | + |
| 177 | + TokenBinding tokenBinding = new TokenBinding(); |
| 178 | + if (setBindingReference) { |
| 179 | + tokenBinding.setBindingReference("bindingReference"); |
| 180 | + } |
| 181 | + tokReqMsgCtx.setTokenBinding(tokenBinding); |
| 182 | + |
| 183 | + // Mocking static methods using try-with-resources |
| 184 | + try (MockedStatic<IdentityUtil> identityUtil = mockStatic(IdentityUtil.class); |
| 185 | + MockedStatic<OAuth2Util> oauth2Util = mockStatic(OAuth2Util.class)) { |
| 186 | + |
| 187 | + identityUtil.when(() -> IdentityUtil.getProperty(anyString())) |
| 188 | + .thenReturn(Boolean.TRUE.toString()); |
| 189 | + |
| 190 | + OAuthComponentServiceHolder.getInstance().setActionExecutorService(mockActionExecutionService); |
| 191 | + OAuthTokenPersistenceFactory persistenceFactory = mock(OAuthTokenPersistenceFactory.class); |
| 192 | + when(persistenceFactory.getAccessTokenDAO()).thenReturn(new AccessTokenDAOImpl()); |
| 193 | + |
| 194 | + OauthTokenIssuer oauthTokenIssuer = mock(JWTTokenIssuer.class); |
| 195 | + when(oauthTokenIssuer.getAccessTokenType()).thenReturn("jwt"); |
| 196 | + oauth2Util.when(() -> OAuth2Util.getOAuthTokenIssuerForOAuthApp(clientId)).thenReturn(oauthTokenIssuer); |
| 197 | + oauth2Util.when(() -> OAuth2Util.getAppInformationByClientId(clientId)).thenReturn(oAuthAppDO); |
| 198 | + |
| 199 | + // Set allowed grant types (ensure PASSWORD_GRANT is allowed for renewal) |
| 200 | + OAuth2ServiceComponentHolder.setJwtRenewWithoutRevokeAllowedGrantTypes( |
| 201 | + Collections.singletonList("password")); // This allows PASSWORD_GRANT |
| 202 | + |
| 203 | + OAuth2AccessTokenRespDTO tokenRespDTO = handler.issue(tokReqMsgCtx); |
| 204 | + } |
| 205 | + } |
| 206 | + |
145 | 207 | @DataProvider(name = "IssueDataProvider")
|
146 | 208 | public Object[][] issueDataProvider() {
|
147 | 209 | return new Object[][] {
|
@@ -174,6 +236,14 @@ public Object[][] issueDataProvider() {
|
174 | 236 | {false, true, 0L, 0L, -1L, 3600L, true, TOKEN_STATE_ACTIVE, true, false}};
|
175 | 237 | }
|
176 | 238 |
|
| 239 | + @DataProvider(name = "IssueWithRenewDataProvider") |
| 240 | + public Object[][] issueWithRenewDataProvider() { |
| 241 | + return new Object[][]{ |
| 242 | + {true, true, 3600L, 3600L, 0L, 0L, false, TOKEN_STATE_ACTIVE, false, true, true}, |
| 243 | + {true, true, 3600L, 3600L, 0L, 0L, false, TOKEN_STATE_ACTIVE, false, true, false} |
| 244 | + }; |
| 245 | + } |
| 246 | + |
177 | 247 | @Test(dataProvider = "IssueDataProvider")
|
178 | 248 | public void testIssue(boolean cacheEnabled, boolean cacheEntryAvailable, long cachedTokenValidity,
|
179 | 249 | long cachedRefreshTokenValidity, long dbTokenValidity, long dbRefreshTokenValidity,
|
|
0 commit comments