JWTtoken generation fixes in progress

minorFixes
Pasindu Rupasinghe 1 year ago
parent ab681eb915
commit 17d701baf8

@ -30,7 +30,7 @@ public final class Constants {
public static final String CLIENT_NAME = "rest_api_publisher_code";
public static final String SERVER_USER = "WorkflowConfigurations.ServerUser";
public static final String SERVER_PASSWORD = "WorkflowConfigurations.ServerPassword";
public static final String GRANT_TYPE = "client_credentials password refresh_token";
public static final String GRANT_TYPE = "client_credentials password refresh_token urn:ietf:params:oauth:grant-type:jwt-bearer";
public static final String REFRESH_TOKEN_GRANT_TYPE_PARAM_NAME = "refresh_token";
public static final String OAUTH_EXPIRES_IN = "expires_in";
public static final String OAUTH_TOKEN_SCOPE = "scope";

@ -344,7 +344,7 @@ public class KeyMgtServiceImpl implements KeyMgtService {
Response response = client.newCall(request).execute();
return gson.fromJson(response.body().string(), OAuthApplication.class);
} catch (IOException e) {
msg = "Error occurred while processing the response";
msg = "Error occurred while processing the response, "+ e;
throw new KeyMgtException(msg);
}
}

@ -23,6 +23,10 @@ import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProvi
import io.entgra.device.mgt.core.apimgt.application.extension.APIManagementProviderServiceImpl;
import io.entgra.device.mgt.core.apimgt.application.extension.dto.ApiApplicationKey;
import io.entgra.device.mgt.core.apimgt.application.extension.exception.APIManagerException;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServices;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.APIApplicationServicesImpl;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.dto.APIApplicationKey;
import io.entgra.device.mgt.core.apimgt.extension.rest.api.exceptions.APIServicesException;
import io.entgra.device.mgt.core.apimgt.keymgt.extension.DCRResponse;
import io.entgra.device.mgt.core.apimgt.keymgt.extension.TokenRequest;
import io.entgra.device.mgt.core.apimgt.keymgt.extension.TokenResponse;
@ -794,21 +798,27 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
}
String tenantDomain = CarbonContext.getThreadLocalCarbonContext().getTenantDomain();
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain(true);
String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
String applicationName = type.replace(" ", "").replace("_", "")
+ "_" + tenantDomain;
if (!username.contains("@")) {
username += "@" + tenantDomain;
}
KeyMgtService keyMgtService = new KeyMgtServiceImpl();
try {
//todo - lasantha - can't get password from here
ApiApplicationKey apiApplicationKey;
try {
DCRResponse adminDCRResponse = keyMgtService.dynamicClientRegistration(applicationName,
PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
.getRealmConfiguration().getAdminUserName(),
"client_credentials", null, new String[] {"device_management"}, false, validityTime, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
.getRealmConfiguration().getAdminPassword());
// DCRResponse adminDCRResponse = keyMgtService.dynamicClientRegistration("TestDevice01",
// PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
// .getRealmConfiguration().getAdminUserName(),
// "jwt", null, new String[] {"device_management"}, false, validityTime, PrivilegedCarbonContext.getThreadLocalCarbonContext().getUserRealm()
// .getRealmConfiguration().getAdminPassword());
APIApplicationServices apiApplicationServices = new APIApplicationServicesImpl();
APIApplicationKey adminDCRResponse = apiApplicationServices.createAndRetrieveApplicationCredentials();
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
JWTClientManagerService jwtClientManagerService = (JWTClientManagerService) ctx.
@ -816,7 +826,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
JWTClient jwtClient = jwtClientManagerService.getJWTClient();
AccessTokenInfo accessTokenInfo = jwtClient.getAccessToken(adminDCRResponse.getClientId(),
adminDCRResponse.getClientSecret(),
username, "appm:subscribe");
username, "appm:subscribe apim:subscribe openid");
APIManagementProviderService apiManagementProviderService = DeviceMgtAPIUtils.getAPIManagementService();
apiApplicationKey = apiManagementProviderService.generateAndRetrieveApplicationKeys(applicationName,
@ -828,7 +838,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
log.error(msg, e);
return Response.serverError().entity(
new ErrorResponse.ErrorResponseBuilder().setMessage(msg).build()).build();
} catch (UserStoreException e) {
} catch (APIServicesException e) {
String msg = "Error while getting user credentials.";
log.error(msg, e);
return Response.serverError().entity(

@ -138,7 +138,7 @@ public class JWTClient {
if (jwtConfig == null) {
return null;
}
URL tokenEndpoint = new URL(jwtConfig.getTokenEndpoint());
URL tokenEndpoint = new URL(jwtConfig.getTokenEndpoint());// try calling /token endpoint- https://localhost:9443/oauth2/token
HttpClient httpClient = JWTClientUtil.getHttpClient(tokenEndpoint.getProtocol());
HttpPost postMethod = new HttpPost(tokenEndpoint.toString());
postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));

Loading…
Cancel
Save