/** * This method can be used to create a JSON Payload out of the Parameters defined in an OAuth * Application. * * @param oAuthApplicationInfo Object that needs to be converted. * @return */ private String createJsonPayloadFromOauthApplication(OAuthApplicationInfo oAuthApplicationInfo) throws APIManagementException { Map<String, Object> paramMap = new HashMap<String, Object>(); if (oAuthApplicationInfo.getClientName() == null || oAuthApplicationInfo.getParameter(OAuthTwoConstants.CLIENT_CONTACT_NAME) == null || oAuthApplicationInfo.getParameter(OAuthTwoConstants.CLIENT_SCOPE) == null || oAuthApplicationInfo.getParameter(OAuthTwoConstants.CLIENT_CONTAT_EMAIL) == null) { throw new APIManagementException("Mandatory parameters missing"); } // Format of the request needed. // {"name":"TestClient_1","scopes":["scope1"], // "contactName":"John Doe", // "contactEmail":"*****@*****.**"} if (oAuthApplicationInfo.getParameter("id") != null) { paramMap.put("id", (Long) oAuthApplicationInfo.getParameter("id")); } paramMap.put(OAuthTwoConstants.CLIENT_NAME, oAuthApplicationInfo.getClientName()); paramMap.put("clientId", oAuthApplicationInfo.getClientName()); paramMap.put("secret", oAuthApplicationInfo.getClientName() + "_secret_" + ++seed); paramMap.put( OAuthTwoConstants.CLIENT_CONTACT_NAME, oAuthApplicationInfo.getParameter(OAuthTwoConstants.CLIENT_CONTACT_NAME)); paramMap.put( OAuthTwoConstants.CLIENT_CONTAT_EMAIL, oAuthApplicationInfo.getParameter(OAuthTwoConstants.CLIENT_CONTAT_EMAIL)); JSONArray scopes = (JSONArray) oAuthApplicationInfo.getParameter(OAuthTwoConstants.CLIENT_SCOPE); paramMap.put("scopes", scopes); return JSONObject.toJSONString(paramMap); }
/** * This method will Register the client in Authorization Server. * * @param oauthAppRequest this object holds all parameters required to register an OAuth Client. */ @Override public OAuthApplicationInfo createApplication(OAuthAppRequest oauthAppRequest) throws APIManagementException { OAuthApplicationInfo oAuthApplicationInfo = oauthAppRequest.getOAuthApplicationInfo(); LOGGER.log(Level.INFO, "Creating a new oAuthApp in Authorization Server"); System.out.println("Creating a new oAuthApp in Authorization Server"); KeyManagerConfiguration config = KeyManagerHolder.getKeyManagerInstance().getKeyManagerConfiguration(); // Getting Client Registration Url and Access Token from Config. String registrationEndpoint = config.getParameter(OAuthTwoConstants.CLIENT_REG_ENDPOINT); String registrationToken = config.getParameter(OAuthTwoConstants.REGISTRAION_ACCESS_TOKEN); HttpPost httpPost = new HttpPost(registrationEndpoint.trim()); HttpClient httpClient = getHttpClient(); BufferedReader reader = null; try { // Create the JSON Payload that should be sent to OAuth Server. String jsonPayload = createJsonPayloadFromOauthApplication(oAuthApplicationInfo); LOGGER.log(Level.INFO, "Payload for creating new client : " + jsonPayload); System.out.println("Payload for creating new client : " + jsonPayload); httpPost.setEntity(new StringEntity(jsonPayload, OAuthTwoConstants.UTF_8)); httpPost.setHeader( OAuthTwoConstants.CONTENT_TYPE, OAuthTwoConstants.APPLICATION_JSON_CONTENT_TYPE); // Setting Authorization Header, with Access Token httpPost.setHeader( OAuthTwoConstants.AUTHORIZATION, OAuthTwoConstants.BEARER + registrationToken); HttpResponse response = httpClient.execute(httpPost); int responseCode = response.getStatusLine().getStatusCode(); JSONObject parsedObject; HttpEntity entity = response.getEntity(); reader = new BufferedReader(new InputStreamReader(entity.getContent(), OAuthTwoConstants.UTF_8)); // If successful a 201 will be returned. if (HttpStatus.SC_CREATED == responseCode) { parsedObject = getParsedObjectByReader(reader); if (parsedObject != null) { oAuthApplicationInfo = createOAuthAppfromResponse(parsedObject); // We need the id when retrieving a single OAuth Client. So // we have to maintain a mapping // between the consumer key and the ID. LOGGER.log( Level.INFO, "OAuthClient - Mapping | ClientId:" + oAuthApplicationInfo.getClientId() + " > Id:" + oAuthApplicationInfo.getParameter("id")); nameIdMapping.put( oAuthApplicationInfo.getClientId(), (Long) oAuthApplicationInfo.getParameter("id")); return oAuthApplicationInfo; } } else { handleException( "Some thing wrong here while registering the new client " + "HTTP Error response code is " + responseCode); } } catch (UnsupportedEncodingException e) { handleException("Encoding for the Response not-supported.", e); } catch (ParseException e) { handleException("Error while parsing response json", e); } catch (IOException e) { handleException("Error while reading response body ", e); } finally { // close buffer reader. if (reader != null) { IOUtils.closeQuietly(reader); } httpClient.getConnectionManager().shutdown(); } return null; }