/** * This is used to handle the OAuthAdminService exceptions. This will log the error message and return an * IdentityOAuthAdminException exception * @param message error message * @param exception Exception. * @return */ public static IdentityOAuthAdminException handleError(String message, Exception exception) { log.error(message); if (exception == null) { return new IdentityOAuthAdminException(message); } else { if (log.isDebugEnabled()) { log.debug(exception); } return new IdentityOAuthAdminException(message, exception); } }
private OAuthConsumerAppDTO getApplicationById(String clientId) throws DCRMException { if (StringUtils.isEmpty(clientId)) { String errorMessage = "Invalid client_id"; throw DCRMUtils.generateClientException( DCRMConstants.ErrorMessages.BAD_REQUEST_INVALID_INPUT, errorMessage); } try { OAuthConsumerAppDTO dto = oAuthAdminService.getOAuthApplicationData(clientId); if (dto == null || StringUtils.isEmpty(dto.getApplicationName())) { throw DCRMUtils.generateClientException( DCRMConstants.ErrorMessages.NOT_FOUND_APPLICATION_WITH_ID, clientId); } else if (!isUserAuthorized(clientId)) { throw DCRMUtils.generateClientException( DCRMConstants.ErrorMessages.FORBIDDEN_UNAUTHORIZED_USER, clientId); } return dto; } catch (IdentityOAuthAdminException e) { if (e.getCause() instanceof InvalidOAuthClientException) { throw DCRMUtils.generateClientException(DCRMConstants.ErrorMessages.NOT_FOUND_APPLICATION_WITH_ID, clientId); } throw DCRMUtils.generateServerException( DCRMConstants.ErrorMessages.FAILED_TO_GET_APPLICATION_BY_ID, clientId, e); } }
} catch (IdentityOAuthAdminException e) { throw IdentityException.error(DCRException.class, ErrorCodes.META_DATA_VALIDATION_FAILED.toString(), e.getMessage());
public void removeConsumerApplication(String consumerKey) throws IdentityOAuthAdminException { Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; try { prepStmt = connection.prepareStatement(SQLQueries.OAuthAppDAOSQLQueries.REMOVE_APPLICATION); prepStmt.setString(1, consumerKey); prepStmt.execute(); connection.commit(); } catch (SQLException e) {; throw new IdentityOAuthAdminException("Error when executing the SQL : " + SQLQueries.OAuthAppDAOSQLQueries.REMOVE_APPLICATION, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, prepStmt); } }
private String getCallbackURLOfApp(String consumerKey) throws IdentityOAuthAdminException { String callbackURL = null; Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; ResultSet resultSet = null; try { prepStmt = connection.prepareStatement(SQLQueries.OAuthConsumerDAOSQLQueries.GET_REGISTERED_CALLBACK_URL); prepStmt.setString(1, consumerKey); resultSet = prepStmt.executeQuery(); if (resultSet.next()) { callbackURL = resultSet.getString(1); } connection.commit(); } catch (SQLException e) { throw new IdentityOAuthAdminException("Error when reading the callback url for consumer key : " + consumerKey, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, resultSet, prepStmt); } return callbackURL; }
private String getCallbackURLOfReqToken(String oauthToken) throws IdentityOAuthAdminException { String callbackURL = null; Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; ResultSet resultSet = null; try { prepStmt = connection.prepareStatement(SQLQueries.OAuthConsumerDAOSQLQueries.GET_CALLBACK_URL_OF_REQ_TOKEN); prepStmt.setString(1, oauthToken); resultSet = prepStmt.executeQuery(); if (resultSet.next()) { callbackURL = resultSet.getString(1); } connection.commit(); } catch (SQLException e) { throw new IdentityOAuthAdminException("Error when reading the callback url for OAuth Token : " + oauthToken, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, resultSet, prepStmt); } return callbackURL; }
private String getCallbackURLOfReqToken(String oauthToken) throws IdentityOAuthAdminException { String callbackURL = null; Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; ResultSet resultSet = null; try { prepStmt = connection.prepareStatement(SQLQueries.OAuthConsumerDAOSQLQueries.GET_CALLBACK_URL_OF_REQ_TOKEN); prepStmt.setString(1, oauthToken); resultSet = prepStmt.executeQuery(); if (resultSet.next()) { callbackURL = resultSet.getString(1); } connection.commit(); } catch (SQLException e) { throw new IdentityOAuthAdminException("Error when reading the callback url for OAuth Token : " + oauthToken, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, resultSet, prepStmt); } return callbackURL; }
private String getCallbackURLOfApp(String consumerKey) throws IdentityOAuthAdminException { String callbackURL = null; Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; ResultSet resultSet = null; try { prepStmt = connection.prepareStatement(SQLQueries.OAuthConsumerDAOSQLQueries.GET_REGISTERED_CALLBACK_URL); prepStmt.setString(1, consumerKey); resultSet = prepStmt.executeQuery(); if (resultSet.next()) { callbackURL = resultSet.getString(1); } connection.commit(); } catch (SQLException e) { throw new IdentityOAuthAdminException("Error when reading the callback url for consumer key : " + consumerKey, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, resultSet, prepStmt); } return callbackURL; }
private boolean isDuplicateConsumer(String consumerKey) throws IdentityOAuthAdminException { Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement prepStmt = null; ResultSet rSet = null; boolean isDuplicateConsumer = false; try { prepStmt = connection.prepareStatement(SQLQueries.OAuthAppDAOSQLQueries.CHECK_EXISTING_CONSUMER); prepStmt.setString(1, persistenceProcessor.getProcessedClientId(consumerKey)); rSet = prepStmt.executeQuery(); if (rSet.next()) { isDuplicateConsumer = true; } connection.commit(); } catch (IdentityOAuth2Exception e) { throw new IdentityOAuthAdminException("Error occurred while processing the client id by TokenPersistenceProcessor"); } catch (SQLException e) { throw new IdentityOAuthAdminException("Error when executing the SQL : " + SQLQueries .OAuthAppDAOSQLQueries.CHECK_EXISTING_CONSUMER, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, rSet, prepStmt); } return isDuplicateConsumer; }
/** * Get the scope validators registered by the user and filter the allowed ones. * * @param application Application user have registered. * @return List of scope validators. * @throws IdentityOAuthAdminException Identity OAuthAdmin exception. */ private String[] filterScopeValidators(OAuthConsumerAppDTO application) throws IdentityOAuthAdminException { List<String> scopeValidators = new ArrayList<>(Arrays.asList(getAllowedScopeValidators())); String[] requestedScopeValidators = application.getScopeValidators(); if (requestedScopeValidators == null) { requestedScopeValidators = new String[0]; } for (String requestedScopeValidator : requestedScopeValidators) { if (!scopeValidators.contains(requestedScopeValidator)) { throw new IdentityOAuthAdminException(requestedScopeValidator + " not allowed"); } } return requestedScopeValidators; }
/** * Generates a random number using two UUIDs and HMAC-SHA1 * * @return generated secure random number * @throws IdentityOAuthAdminException Invalid Algorithm or Invalid Key */ public static String getRandomNumber() throws IdentityOAuthAdminException { try { String secretKey = UUIDGenerator.generateUUID(); String baseString = UUIDGenerator.generateUUID(); SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(Charsets.UTF_8), ALGORITHM); Mac mac = Mac.getInstance(ALGORITHM); mac.init(key); byte[] rawHmac = mac.doFinal(baseString.getBytes(Charsets.UTF_8)); String random = Base64.encode(rawHmac); // Registry doesn't have support for these character. random = random.replace("/", "_"); random = random.replace("=", "a"); random = random.replace("+", "f"); return random; } catch (Exception e) { throw new IdentityOAuthAdminException("Error when generating a random number.", e); } }
public void issueAccessToken(String consumerKey, String accessToken, String accessTokenSecret, String requestToken, String authorizedUser, String scope) throws IdentityOAuthAdminException { Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement removeReqTokStmt = null; PreparedStatement issueAccessTokStmt = null; try { removeReqTokStmt = connection.prepareStatement(SQLQueries.OAuthConsumerDAOSQLQueries.REMOVE_REQUEST_TOKEN); removeReqTokStmt.setString(1, requestToken); removeReqTokStmt.execute(); issueAccessTokStmt = connection.prepareStatement(SQLQueries.OAuthConsumerDAOSQLQueries.ADD_ACCESS_TOKEN); issueAccessTokStmt.setString(1, accessToken); issueAccessTokStmt.setString(2, accessTokenSecret); issueAccessTokStmt.setString(3, consumerKey); issueAccessTokStmt.setString(4, scope); issueAccessTokStmt.setString(5, authorizedUser); issueAccessTokStmt.execute(); connection.commit(); } catch (SQLException e) { log.error(e.getMessage(), e); throw new IdentityOAuthAdminException("Error when creating the request token for consumer : " + consumerKey); } finally { IdentityDatabaseUtil.closeStatement(issueAccessTokStmt); IdentityDatabaseUtil.closeAllConnections(connection, null, removeReqTokStmt); } }
public void issueAccessToken(String consumerKey, String accessToken, String accessTokenSecret, String requestToken, String authorizedUser, String scope) throws IdentityOAuthAdminException { Connection connection = IdentityDatabaseUtil.getDBConnection(); PreparedStatement removeReqTokStmt = null; PreparedStatement issueAccessTokStmt = null; try { removeReqTokStmt = connection.prepareStatement(SQLQueries.OAuthConsumerDAOSQLQueries.REMOVE_REQUEST_TOKEN); removeReqTokStmt.setString(1, requestToken); removeReqTokStmt.execute(); issueAccessTokStmt = connection.prepareStatement(SQLQueries.OAuthConsumerDAOSQLQueries.ADD_ACCESS_TOKEN); issueAccessTokStmt.setString(1, accessToken); issueAccessTokStmt.setString(2, accessTokenSecret); issueAccessTokStmt.setString(3, consumerKey); issueAccessTokStmt.setString(4, scope); issueAccessTokStmt.setString(5, authorizedUser); issueAccessTokStmt.execute(); connection.commit(); } catch (SQLException e) { log.error(e.getMessage(), e); throw new IdentityOAuthAdminException("Error when creating the request token for consumer : " + consumerKey); } finally { IdentityDatabaseUtil.closeStatement(issueAccessTokStmt); IdentityDatabaseUtil.closeAllConnections(connection, null, removeReqTokStmt); } }
/** * Generates a random number using two UUIDs and HMAC-SHA1 * * @return generated secure random number * @throws IdentityOAuthAdminException Invalid Algorithm or Invalid Key */ public static String getRandomNumber() throws IdentityOAuthAdminException { try { String secretKey = UUIDGenerator.generateUUID(); String baseString = UUIDGenerator.generateUUID(); SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(Charsets.UTF_8), ALGORITHM); Mac mac = Mac.getInstance(ALGORITHM); mac.init(key); byte[] rawHmac = mac.doFinal(baseString.getBytes(Charsets.UTF_8)); String random = Base64.encode(rawHmac); // Registry doesn't have support for these character. random = random.replace("/", "_"); random = random.replace("=", "a"); random = random.replace("+", "f"); return random; } catch (Exception e) { throw new IdentityOAuthAdminException("Error when generating a random number.", e); } }
log.error("Error when executing the SQL : " + SQLQueries.OAuthConsumerDAOSQLQueries.GET_USERNAME_FOR_KEY_AND_SECRET); log.error(e.getMessage(), e); throw new IdentityOAuthAdminException("Error while reading username for client id : " + clientId + ", and consumer secret : " + clientSecret); } finally {
log.error("Error when executing the SQL : " + SQLQueries.OAuthConsumerDAOSQLQueries.GET_USERNAME_FOR_KEY_AND_SECRET); log.error(e.getMessage(), e); throw new IdentityOAuthAdminException("Error while reading username for client id : " + clientId + ", and consumer secret : " + clientSecret); } finally {
throw new IdentityOAuthAdminException("Error when reading the consumer secret for consumer key : " + consumerKey, e); } catch (IdentityOAuth2Exception e) { throw new IdentityOAuthAdminException("Error occurred while processing client id and client secret by " + "TokenPersistenceProcessor", e); } finally {
log.error("Error when executing the SQL : " + SQLQueries.OAuthConsumerDAOSQLQueries.ADD_OAUTH_REQ_TOKEN); log.error(e.getMessage(), e); throw new IdentityOAuthAdminException("Error when creating the request token for consumer : " + consumerKey); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, prepStmt);
log.error("Error when executing the SQL : " + SQLQueries.OAuthConsumerDAOSQLQueries.ADD_OAUTH_REQ_TOKEN); log.error(e.getMessage(), e); throw new IdentityOAuthAdminException("Error when creating the request token for consumer : " + consumerKey); } finally { IdentityDatabaseUtil.closeAllConnections(connection, null, prepStmt);
throw new IdentityOAuthAdminException("Error when executing the SQL : " + SQLQueries.OAuthAppDAOSQLQueries.CHECK_EXISTING_APPLICATION, e); } finally { IdentityDatabaseUtil.closeAllConnections(connection, rSet, prepStmt);