@ -19,7 +19,6 @@
package org.wso2.carbon.apimgt.handlers.utils ;
package org.wso2.carbon.apimgt.handlers.utils ;
import com.google.gson.Gson ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.Log ;
import org.apache.commons.logging.LogFactory ;
import org.apache.commons.logging.LogFactory ;
import org.apache.ws.security.util.Base64 ;
import org.apache.ws.security.util.Base64 ;
@ -118,8 +117,7 @@ public class Utils {
tokenHeaders . put ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
tokenHeaders . put ( "Content-Type" , "application/x-www-form-urlencoded" ) ;
RESTInvoker restInvoker = new RESTInvoker ( ) ;
RESTInvoker restInvoker = new RESTInvoker ( ) ;
RESTResponse response = restInvoker . invokePOST ( tokenUrl , tokenHeaders , null ,
RESTResponse response = restInvoker . invokePOST ( tokenUrl , tokenHeaders , null , null , tokenContent ) ;
null , tokenContent ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "Token response:" + response . getContent ( ) ) ;
log . debug ( "Token response:" + response . getContent ( ) ) ;
}
}
@ -144,31 +142,32 @@ public class Utils {
private static void getClientSecretes ( IOTServerConfiguration iotServerConfiguration )
private static void getClientSecretes ( IOTServerConfiguration iotServerConfiguration )
throws APIMCertificateMGTException {
throws APIMCertificateMGTException {
try {
try {
String username = iotServerConfiguration . getUsername ( ) ;
String password = iotServerConfiguration . getPassword ( ) ;
DCR dcr = new DCR ( ) ;
DCR dcr = new DCR ( ) ;
dcr . setOwner ( iotServerConfiguration . getUsername ( ) ) ;
dcr . setOwner ( iotServerConfiguration . getUsername ( ) ) ;
dcr . setClientName ( "IOT-API-MANAGER" ) ;
dcr . setClientName ( AuthConstants . CLIENT_NAME ) ;
dcr . setGrantType ( "refresh_token password client_credentials" ) ;
dcr . setGrantType ( AuthConstants . GRANT_TYPE ) ;
dcr . setTokenScope ( "default" ) ;
dcr . setTokenScope ( AuthConstants . TOKEN_SCOPE ) ;
Gson gson = new Gson ( ) ;
dcr . setCallbackUrl ( AuthConstants . CALLBACK_URL ) ;
String dcrContent = gson . toJson ( dcr ) ;
dcr . setIsSaasApp ( true ) ;
Map < String , String > drcHeaders = new HashMap < String , String > ( ) ;
String dcrContent = dcr . toJSON ( ) ;
drcHeaders . put ( "Content-Type" , "application/json" ) ;
Map < String , String > dcrHeaders = new HashMap < String , String > ( ) ;
String basicAuth = Base64 . encode ( ( username + ":" + password ) . getBytes ( ) ) ;
dcrHeaders . put ( AuthConstants . CONTENT_TYPE_HEADER , AuthConstants . CONTENT_TYPE ) ;
dcrHeaders . put ( AuthConstants . AUTHORIZATION_HEADER , AuthConstants . BASIC_AUTH_PREFIX + basicAuth ) ;
URI dcrUrl = new URI ( iotServerConfiguration . getDynamicClientRegistrationEndpoint ( ) ) ;
URI dcrUrl = new URI ( iotServerConfiguration . getDynamicClientRegistrationEndpoint ( ) ) ;
RESTInvoker restInvoker = new RESTInvoker ( ) ;
RESTInvoker restInvoker = new RESTInvoker ( ) ;
RESTResponse response = restInvoker . invokePOST ( dcrUrl , drcHeaders , null ,
RESTResponse response = restInvoker . invokePOST ( dcrUrl , dcrHeaders , null , null , dcrContent ) ;
null , dcrContent ) ;
if ( log . isDebugEnabled ( ) ) {
if ( log . isDebugEnabled ( ) ) {
log . debug ( "DCR response :" + response . getContent ( ) ) ;
log . debug ( "DCR response :" + response . getContent ( ) ) ;
}
}
JSONObject jsonResponse = new JSONObject ( response . getContent ( ) ) ;
JSONObject jsonResponse = new JSONObject ( response . getContent ( ) ) ;
clientId = jsonResponse . getString ( "client_id" ) ;
clientId = jsonResponse . getString ( AuthConstants . CLIENT_ID ) ;
clientSecret = jsonResponse . getString ( "client_secret" ) ;
clientSecret = jsonResponse . getString ( AuthConstants . CLIENT_SECRET ) ;
} catch ( JSONException e ) {
} catch ( JSONException e ) {
throw new APIMCertificateMGTException ( "Error occurred while converting the json to object" , e ) ;
throw new APIMCertificateMGTException ( "Error occurred while converting the json to object" , e ) ;
} catch ( IOException e ) {
} catch ( IOException | URISyntaxException e ) {
throw new APIMCertificateMGTException ( "Error occurred while trying to call DCR endpoint" , e ) ;
} catch ( URISyntaxException e ) {
throw new APIMCertificateMGTException ( "Error occurred while trying to call DCR endpoint" , e ) ;
throw new APIMCertificateMGTException ( "Error occurred while trying to call DCR endpoint" , e ) ;
}
}