Changeset 32369 for main/trunk


Ignore:
Timestamp:
2018-08-23T13:57:26+12:00 (6 years ago)
Author:
kjdon
Message:

lots of changes. moved to version 2 of recaptcha. moved all the error strings into the servicerack.properties file. fixed up the bug whereby a user changes his own account details, then was being shown the full user list.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/src/java/org/greenstone/gsdl3/service/Authentication.java

    r28966 r32369  
    1111import java.util.regex.Pattern;
    1212
    13 import net.tanesha.recaptcha.ReCaptchaImpl;
    14 import net.tanesha.recaptcha.ReCaptchaResponse;
     13// for verifying recaptcha
     14import java.io.BufferedReader;
     15import java.io.DataOutputStream;
     16import java.io.IOException;
     17import java.io.InputStreamReader;
     18import java.io.StringReader;
     19import java.net.URL;
     20import javax.net.ssl.HttpsURLConnection;
     21// https://developer.android.com/reference/org/json/JSONObject.html
     22// https://developer.android.com/reference/org/json/JSONArray.html
     23import org.json.JSONArray;
     24import org.json.JSONException;
     25import org.json.JSONObject;
    1526
    1627import org.apache.commons.codec.digest.DigestUtils;
     
    3647    //Error codes
    3748    protected static final int NO_ERROR = 0;
    38     protected static final int ERROR_REQUEST_HAS_NO_PARAM_LIST = -1;
    3949    protected static final int ERROR_NOT_LOGGED_IN = -2;
    4050    protected static final int ERROR_ADMIN_NOT_LOGGED_IN = -3;
    4151    protected static final int ERROR_COULD_NOT_GET_USER_INFO = -4;
    4252    protected static final int ERROR_USERNAME_NOT_SPECIFIED = -5;
    43     protected static final int ERROR_REQUESTED_USER_NOT_FOUND = -6;
     53    protected static final int ERROR_USER_NOT_FOUND = -6;
    4454    protected static final int ERROR_SQL_EXCEPTION = -7;
    4555    protected static final int ERROR_INVALID_USERNAME = -8;
     
    5262    protected static final int ERROR_ADDING_USER = -15;
    5363    protected static final int ERROR_REMOVING_USER = -16;
    54     protected static final int ERROR_CAPTCHA_DOES_NOT_MATCH = -17;
     64    protected static final int ERROR_CAPTCHA_FAILED = -17;
    5565    protected static final int ERROR_CAPTCHA_MISSING = -18;
    5666    protected static final int ERROR_NOT_AUTHORISED = -19;
    57 
    58     protected static final HashMap<Integer, String> _errorMessageMap;
     67    protected static final int ERROR_MISSING_PARAMS = -20;
     68 
     69    protected static final HashMap<Integer, String> _errorKeyMap;
    5970    static
    6071    {
    61         //Corresponding error messages
    62         HashMap<Integer, String> errorMessageMap = new HashMap<Integer, String>();
    63         errorMessageMap.put(ERROR_REQUEST_HAS_NO_PARAM_LIST, "The list of parameters for this request was empty.");
    64         errorMessageMap.put(ERROR_NOT_LOGGED_IN, "You must be logged in to access this page.");
    65         errorMessageMap.put(ERROR_ADMIN_NOT_LOGGED_IN, "You must be logged in as an administrator to access this page.");
    66         errorMessageMap.put(ERROR_COULD_NOT_GET_USER_INFO, "There was a error getting the user information.");
    67         errorMessageMap.put(ERROR_USERNAME_NOT_SPECIFIED, "No username was specified.");
    68         errorMessageMap.put(ERROR_REQUESTED_USER_NOT_FOUND, "The requested user was not found in the database.");
    69         errorMessageMap.put(ERROR_SQL_EXCEPTION, "There was an SQL exception while accessing the database.");
    70         errorMessageMap.put(ERROR_INVALID_USERNAME, "The username specified was invalid.");
    71         errorMessageMap.put(ERROR_PASSWORD_NOT_ENTERED, "No password was entered.");
    72         errorMessageMap.put(ERROR_PASSWORD_TOO_SHORT, "The password you entered was too short (minimum of 3 characters).");
    73         errorMessageMap.put(ERROR_PASSWORD_TOO_LONG, "The password you entered was too long (maximum of 64 characters).");
    74         errorMessageMap.put(ERROR_PASSWORD_USES_ILLEGAL_CHARACTERS, "The password you entered contains illegal characters.");
    75         errorMessageMap.put(ERROR_INCORRECT_PASSWORD, "The password specified was incorrect.");
    76         errorMessageMap.put(ERROR_USER_ALREADY_EXISTS, "This user already exists and therefore cannot be added.");
    77         errorMessageMap.put(ERROR_ADDING_USER, "There was an error adding this user to the database.");
    78         errorMessageMap.put(ERROR_REMOVING_USER, "There was an error removing this user from the database.");
    79         errorMessageMap.put(ERROR_CAPTCHA_DOES_NOT_MATCH, "The words you entered did not match the image, please try again.");
    80         errorMessageMap.put(ERROR_CAPTCHA_MISSING, "The information from the captcha is missing.");
    81         errorMessageMap.put(ERROR_NOT_AUTHORISED, "You are not authorised to access this page.");
    82 
    83         _errorMessageMap = errorMessageMap;
     72        //Corresponding error message keys for looking up in ServiceRack dictionary
     73        HashMap<Integer, String> errorKeyMap = new HashMap<Integer, String>();
     74        errorKeyMap.put(ERROR_NOT_LOGGED_IN, "auth.error.not_logged_in");
     75        errorKeyMap.put(ERROR_ADMIN_NOT_LOGGED_IN, "auth.error.admin_not_logged_in");
     76        errorKeyMap.put(ERROR_COULD_NOT_GET_USER_INFO, "auth.error.could_not_get_user_info");
     77        errorKeyMap.put(ERROR_USERNAME_NOT_SPECIFIED, "auth.error.username_not_specified");
     78        errorKeyMap.put(ERROR_USER_NOT_FOUND, "auth.error.user_not_found");
     79        errorKeyMap.put(ERROR_SQL_EXCEPTION, "auth.error.sql_exception");
     80        errorKeyMap.put(ERROR_INVALID_USERNAME, "auth.error.invalid_username");
     81        errorKeyMap.put(ERROR_PASSWORD_NOT_ENTERED, "auth.error.no_password");
     82        errorKeyMap.put(ERROR_PASSWORD_TOO_SHORT, "auth.error.password_too_short");
     83        errorKeyMap.put(ERROR_PASSWORD_TOO_LONG, "auth.error.password_too_long");
     84        errorKeyMap.put(ERROR_PASSWORD_USES_ILLEGAL_CHARACTERS, "auth.error.password_illegal_chars");
     85        errorKeyMap.put(ERROR_INCORRECT_PASSWORD, "auth.error.incorrect_password");
     86        errorKeyMap.put(ERROR_USER_ALREADY_EXISTS, "auth.error.user_already_exists");
     87        errorKeyMap.put(ERROR_ADDING_USER, "auth.error.add_user_error");
     88        errorKeyMap.put(ERROR_REMOVING_USER, "auth.error.remove_user_error");
     89        errorKeyMap.put(ERROR_CAPTCHA_FAILED, "auth.error.captcha_failed");
     90        errorKeyMap.put(ERROR_CAPTCHA_MISSING, "auth.error.captcha_missing");
     91        errorKeyMap.put(ERROR_NOT_AUTHORISED, "auth.error.not_authorised");
     92        errorKeyMap.put(ERROR_MISSING_PARAMS, "auth.error.missing_params"); // ???
     93        _errorKeyMap = errorKeyMap;
    8494    }
    8595
     
    123133
    124134    //Other operations
    125     protected static final String REGISTER = "Register";
    126     protected static final String PERFORM_REGISTER = "PerformRegister";
     135  protected static final String REGISTER = "Register"; // displays the register page
     136  protected static final String PERFORM_REGISTER = "PerformRegister"; // performs the registration action
    127137    protected static final String LOGIN = "Login";
    128 
     138  protected static final String BLANK = "Info"; // a dummy page just for showing an error message
    129139    //the services on offer
    130140    protected static final String AUTHENTICATION_SERVICE = "Authentication";
     
    135145    protected static boolean _derbyWrapperDoneForcedShutdown = false;
    136146
    137     protected String _recaptchaPrivateKey = null;
    138     protected String _recaptchaPublicKey = null;
     147    protected String _recaptchaSiteKey = null;
     148    protected String _recaptchaSecretKey = null;
    139149
    140150    /** constructor */
     
    198208        {
    199209            Element currentElem = (Element) recaptchaElems.item(i);
    200             if (currentElem.getAttribute(GSXML.NAME_ATT) != null && currentElem.getAttribute(GSXML.NAME_ATT).equals("public_key"))
     210            if (currentElem.getAttribute(GSXML.NAME_ATT) != null && currentElem.getAttribute(GSXML.NAME_ATT).equals("site_key"))
    201211            {
    202212                if (currentElem.getAttribute(GSXML.VALUE_ATT) != null)
    203213                {
    204                     _recaptchaPublicKey = currentElem.getAttribute(GSXML.VALUE_ATT);
    205                 }
    206             }
    207             else if (currentElem.getAttribute(GSXML.NAME_ATT) != null && currentElem.getAttribute(GSXML.NAME_ATT).equals("private_key"))
     214                    _recaptchaSiteKey = currentElem.getAttribute(GSXML.VALUE_ATT);
     215                }
     216            }
     217            else if (currentElem.getAttribute(GSXML.NAME_ATT) != null && currentElem.getAttribute(GSXML.NAME_ATT).equals("secret_key"))
    208218            {
    209219                if (currentElem.getAttribute(GSXML.VALUE_ATT) != null)
    210220                {
    211                     _recaptchaPrivateKey = currentElem.getAttribute(GSXML.VALUE_ATT);
     221                    _recaptchaSecretKey = currentElem.getAttribute(GSXML.VALUE_ATT);
    212222                }
    213223            }
     
    269279        return getTextString(service_id + ".description", lang);
    270280    }
    271 
     281    protected String getErrorTextString(int error_code, String lang) {
     282      return getTextString(_errorKeyMap.get(error_code), lang);
     283     
     284    }
    272285    protected Element processChangeUserEditMode(Element request)
    273286    {
     
    282295        if (paramList == null)
    283296        {
    284             GSXML.addError(result, _errorMessageMap.get(ERROR_REQUEST_HAS_NO_PARAM_LIST));
    285             return result;
     297          logger.error("ChangeUserEditMode request has no param list!!");
     298          return result;
    286299        }
    287300
     
    384397        result.setAttribute(GSXML.TYPE_ATT, GSXML.REQUEST_TYPE_PROCESS);
    385398
     399        String lang = request.getAttribute(GSXML.LANG_ATT);
    386400        Element paramList = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
    387401        if (paramList == null)
    388402        {
    389             GSXML.addError(result, _errorMessageMap.get(ERROR_REQUEST_HAS_NO_PARAM_LIST));
    390             return result;
     403          logger.error("GetUserInformation request has no param list");
     404          return result;
    391405        }
    392406
     
    397411        if (username == null)
    398412        {
    399             GSXML.addError(result, _errorMessageMap.get(ERROR_USERNAME_NOT_SPECIFIED));
     413            GSXML.addError(result, getErrorTextString(ERROR_USERNAME_NOT_SPECIFIED, lang));
    400414            return result;
    401415        }
     
    410424        if (terms.size() == 0)
    411425        {
    412             GSXML.addError(result, _errorMessageMap.get(ERROR_REQUESTED_USER_NOT_FOUND));
     426            GSXML.addError(result, getErrorTextString(ERROR_USER_NOT_FOUND, lang));
    413427            return result;
    414428        }
     
    461475
    462476        // Get the parameters of the request
     477        String lang = request.getAttribute(GSXML.LANG_ATT);
    463478        Element param_list = (Element) GSXML.getChildByTagName(request, GSXML.PARAM_ELEM + GSXML.LIST_MODIFIER);
    464479        if (param_list == null)
    465480        {
    466481            serviceNode.setAttribute("operation", LOGIN);
    467             GSXML.addError(result, _errorMessageMap.get(ERROR_REQUEST_HAS_NO_PARAM_LIST));
     482            logger.error("Authentication request has no param list");
    468483            return result; // Return the empty result
    469484        }
     
    476491
    477492        Element userInformation = (Element) GSXML.getChildByTagName(request, GSXML.USER_INFORMATION_ELEM);
    478         if (userInformation == null && _userOpList.contains(op))
    479         {
    480             serviceNode.setAttribute("operation", LOGIN);
    481             GSXML.addError(result, _errorMessageMap.get(ERROR_NOT_LOGGED_IN));
    482             return result;
    483         }
    484 
    485493        if (userInformation != null)
    486494        {
     
    489497        }
    490498
    491         if (username == null && _userOpList.contains(op))
    492         {
     499        if ((userInformation == null || username == null) && _userOpList.contains(op))
     500        {
     501          // its an operation that requires the user to be logged on - direct them to login page
    493502            serviceNode.setAttribute("operation", LOGIN);
    494             GSXML.addError(result, _errorMessageMap.get(ERROR_NOT_LOGGED_IN));
     503            GSXML.addError(result, getErrorTextString(ERROR_NOT_LOGGED_IN, lang));
    495504            return result;
    496505        }
     
    498507        if (_adminOpList.contains(op) && (groups == null || !groups.matches(".*\\badministrator\\b.*")))
    499508        {
     509          // actually, the user needs to be an admin user and they are not
    500510            serviceNode.setAttribute("operation", LOGIN);
    501             GSXML.addError(result, _errorMessageMap.get(ERROR_ADMIN_NOT_LOGGED_IN));
     511            GSXML.addError(result, getErrorTextString(ERROR_ADMIN_NOT_LOGGED_IN, lang));
    502512            return result;
    503513        }
     
    505515        if (op.equals(LIST_USERS))
    506516        {
    507             int error = addUserInformationToNode(null, serviceNode);
    508             if (error != NO_ERROR)
    509             {
    510                 GSXML.addError(result, _errorMessageMap.get(error));
    511             }
    512         }
    513         else if (op.equals(PERFORM_ADD))
     517          int error = addUserInformationToNode(null, serviceNode);
     518          if (error != NO_ERROR)
     519            {
     520              serviceNode.setAttribute("operation", BLANK);
     521              GSXML.addError(result, getErrorTextString(error, lang));
     522            }
     523          return result;
     524         
     525        }
     526       
     527        if (op.equals(PERFORM_ADD))
    514528        {
    515529            String newUsername = (String) paramMap.get("username");
     
    524538            if ((error = checkUsername(newUsername)) != NO_ERROR)
    525539            {
    526                 GSXML.addError(result, _errorMessageMap.get(error));
     540                GSXML.addError(result, getErrorTextString(error, lang));
    527541                return result;
    528542            }
     
    531545            if ((error = checkPassword(newPassword)) != NO_ERROR)
    532546            {
    533                 GSXML.addError(result, _errorMessageMap.get(error));
     547                GSXML.addError(result, getErrorTextString(error, lang));
    534548                return result;
    535549            }
     
    541555            {
    542556                serviceNode.setAttribute("operation", ADD_USER);
    543                 GSXML.addError(result, _errorMessageMap.get(error));
     557                GSXML.addError(result, getErrorTextString(error, lang));
    544558            }
    545559            else
     
    548562                serviceNode.setAttribute("operation", LIST_USERS);
    549563            }
    550         }
    551         else if (op.equals(PERFORM_REGISTER))
     564            return result;
     565        }
     566       
     567        if (op.equals(PERFORM_REGISTER))
    552568        {
    553569            String newUsername = (String) paramMap.get("username");
     
    559575            if ((error = checkUsername(newUsername)) != NO_ERROR)
    560576            {
    561                 GSXML.addError(result, _errorMessageMap.get(error));
     577                GSXML.addError(result, getErrorTextString(error, lang));
    562578                return result;
    563579            }
     
    566582            if ((error = checkPassword(newPassword)) != NO_ERROR)
    567583            {
    568                 GSXML.addError(result, _errorMessageMap.get(error));
     584                GSXML.addError(result, getErrorTextString(error, lang));
    569585                return result;
    570586            }
     
    572588            newPassword = hashPassword(newPassword);
    573589
    574             if (_recaptchaPrivateKey != null && _recaptchaPrivateKey.length() > 0)
    575             {
    576                 ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
    577                 reCaptcha.setPrivateKey(_recaptchaPrivateKey);
    578 
    579                 try
    580                 {
    581                     //If this line throws an exception then we'll assume the user has a firewall that is too restrictive
    582                     //(or that they're not connected to the Internet) to allow access to google services.
    583                     //In this situation we won't use the recaptcha test.
    584                     reCaptcha.checkAnswer(request.getAttribute("remoteAddress"), "", "");
    585 
    586                     String challenge = (String) paramMap.get("recaptcha_challenge_field");
    587                     String uResponse = (String) paramMap.get("recaptcha_response_field");
    588 
    589                     if (challenge == null || uResponse == null)
    590                     {
    591                         serviceNode.setAttribute("operation", REGISTER);
    592                         GSXML.addError(result, _errorMessageMap.get(ERROR_CAPTCHA_MISSING));
    593                         return result;
    594                     }
    595 
    596                     ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(request.getAttribute("remoteAddress"), challenge, uResponse);
    597 
    598                     if (!reCaptchaResponse.isValid())
    599                     {
    600                         serviceNode.setAttribute("operation", REGISTER);
    601                         GSXML.addError(result, _errorMessageMap.get(ERROR_CAPTCHA_DOES_NOT_MATCH));
    602                         return result;
    603                     }
    604                 }
    605                 catch (Exception ex)
    606                 {
    607                 }
     590            // check the recaptcha
     591            if (_recaptchaSiteKey != null && _recaptchaSecretKey.length() > 0) {
     592
     593              String user_response = (String) paramMap.get("g-recaptcha-response");
     594              int recaptcha_error = verifyRecaptcha(_recaptchaSecretKey, user_response);
     595              if (recaptcha_error != NO_ERROR) {
     596                serviceNode.setAttribute("operation", REGISTER);
     597                GSXML.addError(result, getErrorTextString(recaptcha_error, lang));
     598                return result;
     599              }
    608600            }
    609601
     
    612604            {
    613605                serviceNode.setAttribute("operation", REGISTER);
    614                 GSXML.addError(result, _errorMessageMap.get(error));
    615             }
    616         }
     606                GSXML.addError(result, getErrorTextString(error, lang));
     607            }
     608            return result;
     609        }
     610       
    617611        else if (op.equals(PERFORM_EDIT))
    618612        {
     
    629623            if ((error = checkUsername(newUsername)) != NO_ERROR)
    630624            {
    631                 GSXML.addError(result, _errorMessageMap.get(error));
     625                GSXML.addError(result, getErrorTextString(error, lang));
    632626                return result;
    633627            }
     
    642636                if ((error = checkPassword(newPassword)) != NO_ERROR)
    643637                {
    644                     GSXML.addError(result, _errorMessageMap.get(error));
     638                    GSXML.addError(result, getErrorTextString(error, lang));
    645639                    return result;
    646640                }
     
    660654                {
    661655                    serviceNode.setAttribute("operation", EDIT_USER);
    662                     GSXML.addError(result, _errorMessageMap.get(error));
     656                    GSXML.addError(result, getErrorTextString(error, lang));
    663657                }
    664658                return result;
     
    669663            {
    670664                serviceNode.setAttribute("operation", EDIT_USER);
    671                 GSXML.addError(result, _errorMessageMap.get(error));
     665                GSXML.addError(result, getErrorTextString(error, lang));
    672666            }
    673667            else
     
    677671            }
    678672        }
     673        // this operation is done by a user when editing their own details. Should not return userNode info.
    679674        else if (op.equals(PERFORM_ACCOUNT_EDIT))
    680675        {
     
    690685                addUserInformationToNode(previousUsername, serviceNode);
    691686                serviceNode.setAttribute("operation", ACCOUNT_SETTINGS);
    692                 GSXML.addError(result, _errorMessageMap.get(ERROR_USER_ALREADY_EXISTS));
     687                GSXML.addError(result, getErrorTextString(ERROR_USER_ALREADY_EXISTS, lang));
    693688                return result;
    694689            }
     
    704699                    addUserInformationToNode(previousUsername, serviceNode);
    705700                    serviceNode.setAttribute("operation", ACCOUNT_SETTINGS);
    706                     GSXML.addError(result, _errorMessageMap.get(ERROR_INCORRECT_PASSWORD), "Incorrect Password");
     701                    GSXML.addError(result, getErrorTextString(ERROR_INCORRECT_PASSWORD, lang), "INCORRECT_PASSWORD");
    707702                    return result;
    708703                }
     
    712707                if ((error = checkPassword(newPassword)) != NO_ERROR)
    713708                {
    714                     GSXML.addError(result, _errorMessageMap.get(error));
     709                  addUserInformationToNode(previousUsername, serviceNode);
     710                  serviceNode.setAttribute("operation", ACCOUNT_SETTINGS);
     711                    GSXML.addError(result, getErrorTextString(error, lang));
    715712                    return result;
    716713                }
     
    727724            if ((error = checkUsername(newUsername)) != NO_ERROR)
    728725            {
    729                 GSXML.addError(result, _errorMessageMap.get(error));
     726              addUserInformationToNode(previousUsername, serviceNode);
     727                  serviceNode.setAttribute("operation", ACCOUNT_SETTINGS);
     728                GSXML.addError(result, getErrorTextString(error, lang));
    730729                return result;
    731730            }
     
    738737            if (error != NO_ERROR)
    739738            {
    740                 if (error == ERROR_USERNAME_NOT_SPECIFIED)
    741                 {
    742                     addUserInformationToNode(null, serviceNode);
    743                     serviceNode.setAttribute("operation", LIST_USERS);
    744                 }
    745                 else
    746                 {
    747                     addUserInformationToNode(previousUsername, serviceNode);
    748                     serviceNode.setAttribute("operation", ACCOUNT_SETTINGS);
    749                     GSXML.addError(result, _errorMessageMap.get(error));
    750                 }
    751                 return result;
     739              addUserInformationToNode(previousUsername, serviceNode);
     740              serviceNode.setAttribute("operation", ACCOUNT_SETTINGS);
     741              GSXML.addError(result, getErrorTextString(error, lang));
     742              return result;
    752743            }
    753744
     
    755746            if (error != NO_ERROR)
    756747            {
    757                 GSXML.addError(result, _errorMessageMap.get(error));
    758             }
    759 
    760             addUserInformationToNode(null, serviceNode);
    761             serviceNode.setAttribute("operation", LIST_USERS);
     748              serviceNode.setAttribute("operation", ACCOUNT_SETTINGS);
     749                GSXML.addError(result, getErrorTextString(error, lang));
     750            }
     751
     752            addUserInformationToNode(newUsername, serviceNode);
     753            serviceNode.setAttribute("operation", ACCOUNT_SETTINGS);
     754            GSXML.addError(result, getTextString("auth.success.account_settings", lang));
     755           
    762756        }
    763757        else if (op.equals(PERFORM_RETRIEVE_PASSWORD))
     
    773767            if (user_name == null || oldPassword == null || newPassword == null)
    774768            {
    775                 GSXML.addError(result, _errorMessageMap.get("missing compulsory parameters: username, oldPassword, or newPassword"));
     769                GSXML.addError(result, getErrorTextString(ERROR_MISSING_PARAMS, lang));
    776770                return result;
    777771            }
     
    781775            {
    782776                addUserInformationToNode(user_name, serviceNode);
    783                 GSXML.addError(result, _errorMessageMap.get(ERROR_INCORRECT_PASSWORD), "Incorrect Password");
     777                GSXML.addError(result, getErrorTextString(ERROR_INCORRECT_PASSWORD, lang), "INCORRECT_PASSWORD");
    784778                return result;
    785779            }
     
    789783            if ((error = checkPassword(newPassword)) != NO_ERROR)
    790784            {
    791                 GSXML.addError(result, _errorMessageMap.get(error));
     785                GSXML.addError(result, getErrorTextString(error, lang));
    792786                return result;
    793787            }
     
    800794            if (info != "succeed")
    801795            {//see DerbyWrapper.modifyUserInfo
    802                 GSXML.addError(result, _errorMessageMap.get(info));
     796              GSXML.addError(result, info);
    803797                return result;
    804798            }
     
    810804            if (error != NO_ERROR)
    811805            {
    812                 GSXML.addError(result, _errorMessageMap.get(error));
     806              GSXML.addError(result, getErrorTextString(error, lang));
    813807            }
    814808        }
     
    820814            {
    821815                serviceNode.setAttribute("operation", "");
    822                 GSXML.addError(result, _errorMessageMap.get(ERROR_USERNAME_NOT_SPECIFIED));
     816                GSXML.addError(result, getErrorTextString(ERROR_USERNAME_NOT_SPECIFIED, lang));
    823817                return result;
    824818            }
     
    827821            {
    828822                serviceNode.setAttribute("operation", LOGIN);
    829                 GSXML.addError(result, _errorMessageMap.get(ERROR_NOT_AUTHORISED));
     823                GSXML.addError(result, getErrorTextString(ERROR_NOT_AUTHORISED, lang));
    830824                return result;
    831825            }
     
    833827            if (error != NO_ERROR)
    834828            {
    835                 GSXML.addError(result, _errorMessageMap.get(error));
     829              GSXML.addError(result, getErrorTextString(error, lang));
    836830            }
    837831        }
     
    851845        else if (op.equals(REGISTER))
    852846        {
    853             if (_recaptchaPrivateKey != null && _recaptchaPrivateKey.length() > 0)
    854             {
    855                 try
    856                 {
    857                     ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
    858                     reCaptcha.setPrivateKey(_recaptchaPrivateKey);
    859                     reCaptcha.checkAnswer(request.getAttribute("remoteAddress"), "", "");
    860                 }
    861                 catch (Exception ex)
    862                 {
    863                     return result;
    864                 }
    865             }
    866 
    867             if (_recaptchaPublicKey != null && _recaptchaPrivateKey != null)
    868             {
    869                 Element recaptchaElem = result_doc.createElement("recaptcha");
    870                 recaptchaElem.setAttribute("publicKey", _recaptchaPublicKey);
    871                 recaptchaElem.setAttribute("privateKey", _recaptchaPrivateKey);
    872                 result.appendChild(recaptchaElem);
    873             }
    874847        }
    875848        else if (op.equals(PERFORM_DELETE_USER))
     
    879852            if (error != NO_ERROR)
    880853            {
    881                 GSXML.addError(result, _errorMessageMap.get(error));
     854              GSXML.addError(result, getErrorTextString(error, lang));
    882855            }
    883856            addUserInformationToNode(null, serviceNode);
     
    933906    }
    934907
     908  public int verifyRecaptcha(String secret_key, String user_response) {
     909
     910    if (user_response == null || user_response.length() == 0) {
     911      return ERROR_CAPTCHA_MISSING;
     912    }
     913
     914    try{
     915     
     916      URL obj = new URL("https://www.google.com/recaptcha/api/siteverify");
     917      HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
     918
     919      // add reuqest header
     920      con.setRequestMethod("POST");
     921      con.setRequestProperty("User-Agent", "Mozilla/5.0");
     922      con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
     923
     924      String postParams = "secret=" + secret_key + "&response="
     925    + user_response;
     926
     927      // Send post request
     928      con.setDoOutput(true);
     929      DataOutputStream wr = new DataOutputStream(con.getOutputStream());
     930      wr.writeBytes(postParams);
     931      wr.flush();
     932      wr.close();
     933
     934      int responseCode = con.getResponseCode();
     935      //System.out.println("\nSending 'POST' request to URL : https://www.google.com/recaptcha/api/siteverify");// + url);
     936      //System.out.println("Post parameters : " + postParams);
     937      //System.out.println("Response Code : " + responseCode);
     938     
     939      BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
     940      String inputLine;
     941      StringBuffer response = new StringBuffer();
     942     
     943      while ((inputLine = in.readLine()) != null) {
     944    response.append(inputLine);
     945      }
     946      in.close();
     947     
     948      // print result
     949      //System.out.println(response.toString());
     950
     951      JSONObject json_obj = new JSONObject(response.toString());
     952      boolean res = json_obj.getBoolean("success");
     953      if (res) {
     954    return NO_ERROR;
     955      } else {
     956    return ERROR_CAPTCHA_FAILED;
     957      }
     958    }catch(Exception e){
     959      e.printStackTrace();
     960      return ERROR_CAPTCHA_FAILED;
     961    }
     962
     963  }
    935964    // This method can also be used for printing out the password in hex (in case
    936965    // the password used the UTF-8 Charset), or the hex values in any unicode string.
     
    11641193    }
    11651194
     1195
    11661196    // main() method - calls hashPassword() on any String argument, printing this to stdout
    11671197    // This main() is invoked by gliserver.pl perl code to encrypt passwords identically to Java code.
Note: See TracChangeset for help on using the changeset viewer.