/** * This method will create {@code OAuthApplicationInfo} object from a Map of Attributes. * * @param responseMap Response returned from server as a Map * @return OAuthApplicationInfo object will return. */ private OAuthApplicationInfo createOAuthAppfromResponse(Map responseMap) { // Sample response returned by client registration endpoint. // {"id":305,"creationDate":1430486098086,"modificationDate":1430486098086,"name":"TestClient_2", // "clientId":"testclient_2","secret":"3b4dbfb6-0ad9-403e-8ed6-715459fc8c78", // "description":null,"contactName":"John Doe","contactEmail":"*****@*****.**", // "scopes":["scope1"],"attributes":{},"thumbNailUrl":null,"redirectUris":[], // "skipConsent":false,"includePrincipal":false,"expireDuration":0,"useRefreshTokens":false, // "allowedImplicitGrant":false,"allowedClientCredentials":false} OAuthApplicationInfo info = new OAuthApplicationInfo(); Object clientId = responseMap.get(OAuthTwoConstants.CLIENT_ID); info.setClientId((String) clientId); Object clientSecret = responseMap.get(OAuthTwoConstants.CLIENT_SECRET); info.setClientSecret((String) clientSecret); Object id = responseMap.get("id"); info.addParameter("id", id); Object contactName = responseMap.get(OAuthTwoConstants.CLIENT_CONTACT_NAME); if (contactName != null) { info.addParameter("contactName", contactName); } Object contactMail = responseMap.get(OAuthTwoConstants.CLIENT_CONTAT_EMAIL); if (contactMail != null) { info.addParameter("contactMail", contactMail); } Object scopes = responseMap.get(OAuthTwoConstants.SCOPES); if (scopes != null) { info.addParameter("scopes", scopes); } return info; }
/** * 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; }
/** * 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); }