Merge pull request 'Fix invalid admin credentials issue in token endpoint' (#14) from vigneshan/device-mgt-core:fix/key-mgt-api into master

Reviewed-on: community/device-mgt-core#14
pull/5/head
Pahansith Gunathilake 2 years ago
commit 6458443c03

@ -26,9 +26,11 @@ public class TokenRequest {
private String grantType; private String grantType;
private String assertion; private String assertion;
private String admin_access_token; private String admin_access_token;
private String username;
private String password;
public TokenRequest(String clientId, String clientSecret, String refreshToken, String scope, String grantType, 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.clientId = clientId;
this.clientSecret = clientSecret; this.clientSecret = clientSecret;
this.refreshToken = refreshToken; this.refreshToken = refreshToken;
@ -36,6 +38,8 @@ public class TokenRequest {
this.grantType = grantType; this.grantType = grantType;
this.assertion = assertion; this.assertion = assertion;
this.admin_access_token = admin_access_token; this.admin_access_token = admin_access_token;
this.username = username;
this.password = password;
} }
public String getClientId() { public String getClientId() {
@ -93,4 +97,20 @@ public class TokenRequest {
public void setAdminAccessToken(String admin_access_token) { public void setAdminAccessToken(String admin_access_token) {
this.admin_access_token = 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;
}
} }

@ -33,6 +33,13 @@ public class TokenResponse {
this.expires_in = expires_in; 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() { public String getAccessToken() {
return access_token; return access_token;
} }

@ -160,37 +160,40 @@ public class KeyMgtServiceImpl implements KeyMgtService {
String tenantDomain = MultitenantUtils.getTenantDomain(application.getOwner()); String tenantDomain = MultitenantUtils.getTenantDomain(application.getOwner());
String username, password; // String username, password;
if (KeyMgtConstants.SUPER_TENANT.equals(tenantDomain)) { // if (KeyMgtConstants.SUPER_TENANT.equals(tenantDomain)) {
kmConfig = getKeyManagerConfig(); // kmConfig = getKeyManagerConfig();
username = kmConfig.getAdminUsername(); // username = kmConfig.getAdminUsername();
password = kmConfig.getAdminUsername(); // password = kmConfig.getAdminPassword();
} else { // } else {
try { // try {
username = getRealmService() // username = getRealmService()
.getTenantUserRealm(-1234).getRealmConfiguration() // .getTenantUserRealm(-1234).getRealmConfiguration()
.getRealmProperty("reserved_tenant_user_username") + "@" + tenantDomain; // .getRealmProperty("reserved_tenant_user_username") + "@" + tenantDomain;
password = getRealmService() // password = getRealmService()
.getTenantUserRealm(-1234).getRealmConfiguration() // .getTenantUserRealm(-1234).getRealmConfiguration()
.getRealmProperty("reserved_tenant_user_password"); // .getRealmProperty("reserved_tenant_user_password");
} catch (UserStoreException e) { // } catch (UserStoreException e) {
msg = "Error while loading user realm configuration"; // msg = "Error while loading user realm configuration";
log.error(msg); // log.error(msg);
throw new KeyMgtException(msg); // throw new KeyMgtException(msg);
} // }
} // }
RequestBody appTokenPayload; RequestBody appTokenPayload;
switch (tokenRequest.getGrantType()) { switch (tokenRequest.getGrantType()) {
case "client_credentials": case "client_credentials":
appTokenPayload = new FormBody.Builder()
.add("grant_type", "client_credentials")
.add("scope", tokenRequest.getScope()).build();
break;
case "password": case "password":
appTokenPayload = new FormBody.Builder() appTokenPayload = new FormBody.Builder()
.add("grant_type", "password") .add("grant_type", "password")
.add("username", username) .add("username", tokenRequest.getUsername())
.add("password", password) .add("password", tokenRequest.getPassword())
.add("scope", tokenRequest.getScope()).build(); .add("scope", tokenRequest.getScope()).build();
break; break;
case "refresh_token": case "refresh_token":
appTokenPayload = new FormBody.Builder() appTokenPayload = new FormBody.Builder()
.add("grant_type", "refresh_token") .add("grant_type", "refresh_token")
@ -239,12 +242,19 @@ public class KeyMgtServiceImpl implements KeyMgtService {
.getTenantManager().getTenantId(tenantDomain); .getTenantManager().getTenantId(tenantDomain);
accessToken = tenantId + "_" + responseObj.getString("access_token"); 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) { } catch (APIManagementException e) {
msg = "Error occurred while retrieving application"; msg = "Error occurred while retrieving application";
log.error(msg); log.error(msg);

Loading…
Cancel
Save