Search in sources :

Example 1 with ClientCredentials

use of org.wso2.mdm.qsg.dto.ClientCredentials in project product-iots by wso2.

the class QSGUtils method getClientCredentials.

private static ClientCredentials getClientCredentials() {
    ClientCredentials clientCredentials = null;
    HashMap<String, String> headers = new HashMap<String, String>();
    EMMQSGConfig emmqsgConfig = EMMQSGConfig.getInstance();
    String dcrEndPoint = emmqsgConfig.getDcrEndPoint();
    // Set the DCR payload
    JSONObject obj = new JSONObject();
    JSONArray arr = new JSONArray();
    arr.add("android");
    arr.add("device_management");
    obj.put("applicationName", "qsg");
    obj.put("tags", arr);
    obj.put("isAllowedToAllDomains", false);
    obj.put("isMappingAnExistingOAuthApp", false);
    String authorizationStr = emmqsgConfig.getUsername() + ":" + emmqsgConfig.getPassword();
    String authHeader = "Basic " + new String(Base64.encodeBase64(authorizationStr.getBytes()));
    headers.put(Constants.Header.AUTH, authHeader);
    // Set the headers
    headers.put(Constants.Header.CONTENT_TYPE, Constants.ContentType.APPLICATION_JSON);
    HTTPResponse httpResponse = HTTPInvoker.sendHTTPPost(dcrEndPoint, obj.toJSONString(), headers);
    if (httpResponse.getResponseCode() == Constants.HTTPStatus.CREATED) {
        try {
            JSONObject jsonObject = (JSONObject) new JSONParser().parse(httpResponse.getResponse());
            clientCredentials = new ClientCredentials();
            clientCredentials.setClientKey((String) jsonObject.get("client_id"));
            clientCredentials.setClientSecret((String) jsonObject.get("client_secret"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    return clientCredentials;
}
Also used : JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) HTTPResponse(org.wso2.mdm.qsg.dto.HTTPResponse) JSONArray(org.json.simple.JSONArray) JSONParser(org.json.simple.parser.JSONParser) EMMQSGConfig(org.wso2.mdm.qsg.dto.EMMQSGConfig) ParseException(org.json.simple.parser.ParseException) ClientCredentials(org.wso2.mdm.qsg.dto.ClientCredentials)

Example 2 with ClientCredentials

use of org.wso2.mdm.qsg.dto.ClientCredentials in project product-iots by wso2.

the class QSGUtils method getOAuthToken.

public static String getOAuthToken() {
    QSGUtils.initConfig();
    ClientCredentials clientCredentials = getClientCredentials();
    String authorizationStr = clientCredentials.getClientKey() + ":" + clientCredentials.getClientSecret();
    String authHeader = "Basic " + new String(Base64.encodeBase64(authorizationStr.getBytes()));
    HashMap<String, String> headers = new HashMap<String, String>();
    // Set the form params
    List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
    urlParameters.add(new BasicNameValuePair("username", EMMQSGConfig.getInstance().getUsername()));
    urlParameters.add(new BasicNameValuePair("password", EMMQSGConfig.getInstance().getPassword()));
    urlParameters.add(new BasicNameValuePair("grant_type", "password"));
    urlParameters.add(new BasicNameValuePair("scope", "user:view user:manage user:admin:reset-password role:view role:manage policy:view policy:manage " + "application:manage appm:administration appm:create appm:publish appm:update appm:read"));
    // Set the headers
    headers.put(Constants.Header.CONTENT_TYPE, Constants.ContentType.APPLICATION_URL_ENCODED);
    headers.put(Constants.Header.AUTH, authHeader);
    HTTPResponse httpResponse = HTTPInvoker.sendHTTPPostWithURLParams(EMMQSGConfig.getInstance().getOauthEndPoint(), urlParameters, headers);
    if (httpResponse.getResponseCode() == Constants.HTTPStatus.OK) {
        try {
            JSONObject jsonObject = (JSONObject) new JSONParser().parse(httpResponse.getResponse());
            return (String) jsonObject.get("access_token");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
    return null;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HTTPResponse(org.wso2.mdm.qsg.dto.HTTPResponse) ArrayList(java.util.ArrayList) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException) ClientCredentials(org.wso2.mdm.qsg.dto.ClientCredentials)

Aggregations

HashMap (java.util.HashMap)2 JSONObject (org.json.simple.JSONObject)2 JSONParser (org.json.simple.parser.JSONParser)2 ParseException (org.json.simple.parser.ParseException)2 ClientCredentials (org.wso2.mdm.qsg.dto.ClientCredentials)2 HTTPResponse (org.wso2.mdm.qsg.dto.HTTPResponse)2 ArrayList (java.util.ArrayList)1 NameValuePair (org.apache.http.NameValuePair)1 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)1 JSONArray (org.json.simple.JSONArray)1 EMMQSGConfig (org.wso2.mdm.qsg.dto.EMMQSGConfig)1