From d28edf1b88d43743bdb0a64dcd498acc639ff8cd Mon Sep 17 00:00:00 2001 From: Vigneshan Date: Tue, 22 Nov 2022 16:32:49 +0000 Subject: [PATCH] Modify password and client_credentials grant handling --- .../apimgt/keymgt/extension/TokenRequest.java | 22 ++++++- .../keymgt/extension/TokenResponse.java | 7 ++ .../extension/service/KeyMgtServiceImpl.java | 64 +++++++++++-------- 3 files changed, 65 insertions(+), 28 deletions(-) diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.keymgt.extension/src/main/java/org/wso2/carbon/apimgt/keymgt/extension/TokenRequest.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.keymgt.extension/src/main/java/org/wso2/carbon/apimgt/keymgt/extension/TokenRequest.java index ae5b668cba..860b267161 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.keymgt.extension/src/main/java/org/wso2/carbon/apimgt/keymgt/extension/TokenRequest.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.keymgt.extension/src/main/java/org/wso2/carbon/apimgt/keymgt/extension/TokenRequest.java @@ -26,9 +26,11 @@ public class TokenRequest { private String grantType; private String assertion; private String admin_access_token; + private String username; + private String password; public TokenRequest(String clientId, String clientSecret, String refreshToken, String scope, String grantType, - String assertion, String admin_access_token) { + String assertion, String admin_access_token, String username, String password) { this.clientId = clientId; this.clientSecret = clientSecret; this.refreshToken = refreshToken; @@ -36,6 +38,8 @@ public class TokenRequest { this.grantType = grantType; this.assertion = assertion; this.admin_access_token = admin_access_token; + this.username = username; + this.password = password; } public String getClientId() { @@ -93,4 +97,20 @@ public class TokenRequest { public void setAdminAccessToken(String admin_access_token) { this.admin_access_token = admin_access_token; } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.keymgt.extension/src/main/java/org/wso2/carbon/apimgt/keymgt/extension/TokenResponse.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.keymgt.extension/src/main/java/org/wso2/carbon/apimgt/keymgt/extension/TokenResponse.java index 0298813878..530fa1742d 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.keymgt.extension/src/main/java/org/wso2/carbon/apimgt/keymgt/extension/TokenResponse.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.keymgt.extension/src/main/java/org/wso2/carbon/apimgt/keymgt/extension/TokenResponse.java @@ -33,6 +33,13 @@ public class TokenResponse { this.expires_in = expires_in; } + public TokenResponse(String access_token, String scope, String token_type, int expires_in) { + this.access_token = access_token; + this.scope = scope; + this.token_type = token_type; + this.expires_in = expires_in; + } + public String getAccessToken() { return access_token; } diff --git a/components/apimgt-extensions/org.wso2.carbon.apimgt.keymgt.extension/src/main/java/org/wso2/carbon/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java b/components/apimgt-extensions/org.wso2.carbon.apimgt.keymgt.extension/src/main/java/org/wso2/carbon/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java index 30412867ca..18714867f1 100644 --- a/components/apimgt-extensions/org.wso2.carbon.apimgt.keymgt.extension/src/main/java/org/wso2/carbon/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java +++ b/components/apimgt-extensions/org.wso2.carbon.apimgt.keymgt.extension/src/main/java/org/wso2/carbon/apimgt/keymgt/extension/service/KeyMgtServiceImpl.java @@ -160,37 +160,40 @@ public class KeyMgtServiceImpl implements KeyMgtService { String tenantDomain = MultitenantUtils.getTenantDomain(application.getOwner()); - String username, password; - if (KeyMgtConstants.SUPER_TENANT.equals(tenantDomain)) { - kmConfig = getKeyManagerConfig(); - username = kmConfig.getAdminUsername(); - password = kmConfig.getAdminUsername(); - } else { - try { - username = getRealmService() - .getTenantUserRealm(-1234).getRealmConfiguration() - .getRealmProperty("reserved_tenant_user_username") + "@" + tenantDomain; - password = getRealmService() - .getTenantUserRealm(-1234).getRealmConfiguration() - .getRealmProperty("reserved_tenant_user_password"); - } catch (UserStoreException e) { - msg = "Error while loading user realm configuration"; - log.error(msg); - throw new KeyMgtException(msg); - } - } +// String username, password; +// if (KeyMgtConstants.SUPER_TENANT.equals(tenantDomain)) { +// kmConfig = getKeyManagerConfig(); +// username = kmConfig.getAdminUsername(); +// password = kmConfig.getAdminPassword(); +// } else { +// try { +// username = getRealmService() +// .getTenantUserRealm(-1234).getRealmConfiguration() +// .getRealmProperty("reserved_tenant_user_username") + "@" + tenantDomain; +// password = getRealmService() +// .getTenantUserRealm(-1234).getRealmConfiguration() +// .getRealmProperty("reserved_tenant_user_password"); +// } catch (UserStoreException e) { +// msg = "Error while loading user realm configuration"; +// log.error(msg); +// throw new KeyMgtException(msg); +// } +// } RequestBody appTokenPayload; switch (tokenRequest.getGrantType()) { case "client_credentials": + appTokenPayload = new FormBody.Builder() + .add("grant_type", "client_credentials") + .add("scope", tokenRequest.getScope()).build(); + break; case "password": appTokenPayload = new FormBody.Builder() .add("grant_type", "password") - .add("username", username) - .add("password", password) + .add("username", tokenRequest.getUsername()) + .add("password", tokenRequest.getPassword()) .add("scope", tokenRequest.getScope()).build(); break; - case "refresh_token": appTokenPayload = new FormBody.Builder() .add("grant_type", "refresh_token") @@ -239,12 +242,19 @@ public class KeyMgtServiceImpl implements KeyMgtService { .getTenantManager().getTenantId(tenantDomain); accessToken = tenantId + "_" + responseObj.getString("access_token"); } - return new TokenResponse(accessToken, - responseObj.getString("refresh_token"), - responseObj.getString("scope"), - responseObj.getString("token_type"), - responseObj.getInt("expires_in")); + if (tokenRequest.getGrantType().equals("client_credentials")) { + return new TokenResponse(accessToken, + responseObj.getString("scope"), + responseObj.getString("token_type"), + responseObj.getInt("expires_in")); + } else { + return new TokenResponse(accessToken, + responseObj.getString("refresh_token"), + responseObj.getString("scope"), + responseObj.getString("token_type"), + responseObj.getInt("expires_in")); + } } catch (APIManagementException e) { msg = "Error occurred while retrieving application"; log.error(msg);