/*************************************************************************
* *
* EJBCA Community: The OpenSource Certificate Authority *
* *
* This software is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or any later version. *
* *
* See terms of license at gnu.org. *
* *
*************************************************************************/
package org.ejbca.samples;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.ejbca.ui.web.RequestHelper;
/**
* Servlet to authenticate a user. Simple database using a file to keep users in format:
* instance;username;password;DN DN is in form: dn-c:dn-o:dn-ou:dn-ln:dn-gn:dn-cn where parts can
* be left out as desired. Expects these parameters when called: (error 500 if any missing)
*
*
*
* user=<username>
*
*
* password=<password>
*
*
* version=<major>.<minor>
*
*
*
*
* Returns a logic token stating that user is authenticated followed by the information to use for
* this user's certificate.
*
*
* @author Original code by Peter Neemeth
* @version $Id: RemoteVerifyServlet.java 19901 2014-09-30 14:29:38Z anatom $
*/
public class RemoteVerifyServlet extends HttpServlet {
private static final long serialVersionUID = -2870243590371650403L;
private static Logger log = Logger.getLogger(RemoteVerifyServlet.class);
/** Status code for successful communication */
public static final String MSG_OK = "200 OK";
/** Status code for failed communication */
public static final String MSG_PROTOCOL_MISMATCH = "400 Wrong protocol version";
/** Status code for generic error */
public static final String MSG_GENERIC_ERROR = "500 ERROR (Missing parameter?) : ";
/** Name of user id parameter */
public static final String REQUEST_USERNAME = "username";
/** Name of password parameter */
public static final String REQUEST_PASSWORD = "password";
/** Name of version parameter */
public static final String REQUEST_VERSION = "version";
/** Token for protocol */
public static final String RESPONSE_END = "end";
/** Token for protocol */
public static final String RESPONSE_STATUS = "status";
/** Token for protocol */
public static final String RESPONSE_RESULT = "result";
/** Token for protocol */
public static final String RESPONSE_MESSAGE = "message";
/** Status code for granting of certificate. */
public static final String GRANT = "grant";
/** Status code for rejecting certificate request. */
public static final String REJECT = "reject";
/** Version of the protocol used when communicating back to requestor */
protected static final int PROTOCOL_VERSION_MAJOR = 1;
/** Version of the protocol used when communicating back to requestor */
protected static final int PROTOCOL_VERSION_MINOR = 0;
/**
* Basic structure containing users. Top level keyed on instance gives new HashMap keyed on
* username with String[] = { password, result } as data.
*/
protected static HashMap users;
/**
* Delimiter between parts in DN
*
*
* Can be controlled via properties file.
*
*/
protected static final String DNPART_DELIMITER = ":";
/**
* Separator between name and value in DN name = value
*
*
* Can be controlled via properties file.
*
*/
protected static final String DNPART_NAME_VALUE_SEPARATOR = "=";
/**
* For easy export from Excel and others.
*
*
* Can be controlled via properties file.
*
*/
protected static final String RECORD_SEPARATOR = ";";
/**
* Ignored lines in DBUSER_file start with this character.
*
*
* Can be controlled via properties file.
*
*/
protected static final String LINE_COMMENT = ";";
/** What parameter to send when using GET to show status. */
protected static final String STATUS_KEY = "status";
/** Count total accesses */
protected static int countAccess = 0;
/** Count granted accesses */
protected static int countGranted = 0;
/** Count rejected accesses */
protected static int countRejected = 0;
/**
* Updates result with name-value-pairs extracted from dnPartsString
*
* @param result where the result is stuffed
* @param dnPartsString name-value-pairs separated by delimiter
*/
void addUserDataToResult(AuthResult result, final String dnPartsString) {
if (dnPartsString == null) {
return;
}
Enumeration