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
fix/key-mgt-api
Pahansith Gunathilake 2 years ago
commit 6458443c03

@ -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;
}
}

@ -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;
}

@ -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");
}
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);

Loading…
Cancel
Save