Search in sources :

Example 1 with Login

use of net.cryptonomica.entities.Login in project cryptonomica by Cryptonomica.

the class NewUserRegistrationAPI method registerNewUser.

@ApiMethod(name = "registerNewUser", path = "registerNewUser", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public // creates a new cryptonomica user and saves him/her to database
NewUserRegistrationReturn registerNewUser(final HttpServletRequest httpServletRequest, final User googleUser, final NewUserRegistrationForm newUserRegistrationForm) throws Exception {
    /* --- Ensure 1) user login, 2) not already registered:: */
    UserTools.ensureNewCryptonomicaUser(googleUser);
    /* --- Check form:*/
    if (newUserRegistrationForm.getArmoredPublicPGPkeyBlock() == null || newUserRegistrationForm.getArmoredPublicPGPkeyBlock().length() == 0) {
        throw new Exception("ASCII-armored PGP public key can not be empty");
    // } else if (newUserRegistrationForm.getUserInfo() == null) {
    // throw new Exception("Info can not be empty");
    } else if (newUserRegistrationForm.getBirthday() == null) {
        throw new Exception("Birthdate can not be empty");
    }
    /* --- user BirthDate */
    Date userBirthDate = newUserRegistrationForm.getBirthday();
    // TODO: add check
    /* --- create PGPPublicKey from armored PGP key block: */
    String userId = googleUser.getUserId();
    String armoredPublicPGPkeyBlock = newUserRegistrationForm.getArmoredPublicPGPkeyBlock();
    LOG.warning("[armoredPublicPGPkeyBlock]:");
    LOG.warning(armoredPublicPGPkeyBlock);
    PGPPublicKey pgpPublicKey = PGPTools.readPublicKeyFromString(armoredPublicPGPkeyBlock);
    // create PGPPublicKeyData (Entity in DS) from PGPPublicKey:
    PGPPublicKeyData pgpPublicKeyData = new PGPPublicKeyData(pgpPublicKey, armoredPublicPGPkeyBlock, userId);
    pgpPublicKeyData.setUserBirthday(userBirthDate);
    /* --- Check PGPPublic Key: */
    // --- check key creation date/time:
    Date creationTime = pgpPublicKey.getCreationTime();
    if (creationTime.after(new Date())) {
        throw new Exception("Invalid key creation Date/Time");
    }
    // -- bits size check:
    if (pgpPublicKeyData.getBitStrength() < 2048) {
        throw new Exception("Key Strength (bits size) should be min 2048 bits");
    }
    // -- email check:
    if (!pgpPublicKeyData.getUserEmail().getEmail().toLowerCase().equals(googleUser.getEmail().toLowerCase())) {
        throw new Exception("Email in the key's user ID should be the same as in account");
    }
    // -- key validity period check
    Integer validDays = pgpPublicKey.getValidDays();
    if (validDays > 366 * 2) {
        throw new Exception("This key valid for more than 2 years");
    } else if (validDays <= 0) {
        // 
        throw new Exception("This key's validity term is incorrect");
    }
    // --- check for dublicates in DS:
    List<PGPPublicKeyData> duplicates = ofy().load().type(PGPPublicKeyData.class).filter("fingerprintStr", pgpPublicKeyData.getFingerprint()).list();
    if (!duplicates.isEmpty()) {
        throw new Exception("The key with fingerprint" + pgpPublicKeyData.getFingerprint() + "already registered");
    }
    // create CryptonomicaUser:
    CryptonomicaUser cryptonomicaUser = new CryptonomicaUser(googleUser, pgpPublicKeyData, newUserRegistrationForm);
    // save new user and his key
    Key<CryptonomicaUser> cryptonomicaUserKey = ofy().save().entity(cryptonomicaUser).now();
    cryptonomicaUser = // ?
    ofy().load().key(cryptonomicaUserKey).now();
    Key<PGPPublicKeyData> pgpPublicKeyDataKey = ofy().save().entity(pgpPublicKeyData).now();
    pgpPublicKeyData = ofy().load().key(pgpPublicKeyDataKey).now();
    // 
    Login login = UserTools.registerLogin(httpServletRequest, googleUser);
    // 
    ArrayList<PGPPublicKeyData> pgpPublicKeyDataArrayList = new ArrayList<>();
    pgpPublicKeyDataArrayList.add(pgpPublicKeyData);
    UserProfileGeneralView userProfileGeneralView = new UserProfileGeneralView(cryptonomicaUser, pgpPublicKeyDataArrayList);
    // - for $rootScope.currentUser
    userProfileGeneralView.setRegisteredCryptonomicaUser(Boolean.TRUE);
    // 
    String messageToUser;
    if (cryptonomicaUser != null) {
        messageToUser = "User created successful";
    } else {
        messageToUser = "Error creating new user";
    }
    NewUserRegistrationReturn result = new NewUserRegistrationReturn(messageToUser, new PGPPublicKeyGeneralView(pgpPublicKeyData), userProfileGeneralView, new LoginView(login));
    // send an email to user:
    final Queue queue = QueueFactory.getDefaultQueue();
    Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
    queue.add(TaskOptions.Builder.withUrl("/_ah/SendGridServlet").param("email", googleUser.getEmail()).param("messageSubject", "You are registered on Cryptonomica server").param("messageText", "Congratulation! \n\n" + userProfileGeneralView.getFirstName().toUpperCase() + " " + userProfileGeneralView.getLastName().toUpperCase() + ",\n\n" + "You are registered on Cryptonomica server" + "\n\n" + "To verify your key online:" + "\n" + "1) go to 'My profile', 2) click on key ID and go to page with key data , " + "3) click green button 'Verify online' and follow instructions provided by web application" + "\n\n" + "Best regards, \n\n" + "Cryptonomica team\n\n" + new Date().toString() + "\n\n" + "if you think it's wrong or it is an error, please write to admin@cryptonomica.net \n"));
    // 
    return result;
}
Also used : PGPPublicKeyData(net.cryptonomica.entities.PGPPublicKeyData) PGPPublicKeyGeneralView(net.cryptonomica.returns.PGPPublicKeyGeneralView) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) Gson(com.google.gson.Gson) Login(net.cryptonomica.entities.Login) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) Date(java.util.Date) UserProfileGeneralView(net.cryptonomica.returns.UserProfileGeneralView) NewUserRegistrationReturn(net.cryptonomica.returns.NewUserRegistrationReturn) LoginView(net.cryptonomica.returns.LoginView) Queue(com.google.appengine.api.taskqueue.Queue) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 2 with Login

use of net.cryptonomica.entities.Login in project cryptonomica by Cryptonomica.

the class UserTools method registerLogin.

// end of ensureGoogleAuth method
public static Login registerLogin(final HttpServletRequest httpServletRequest, final User googleUser) {
    // Register login of this Cryptonomica User:
    Login login = new Login();
    // IP
    String userIP = httpServletRequest.getHeader("X-FORWARDED-FOR");
    if (userIP == null) {
        userIP = httpServletRequest.getRemoteAddr();
    }
    // Get an UserAgentStringParser and analyze the requesting client
    // see example on: http://uadetector.sourceforge.net/usage.html
    UserAgentStringParser parser = UADetectorServiceFactory.getResourceModuleParser();
    ReadableUserAgent agent = parser.parse(httpServletRequest.getHeader("User-Agent"));
    String userBrowser = agent.getName();
    String userOS = agent.getOperatingSystem().getName();
    // 
    String userProviderOrg = null;
    String userProviderHost = null;
    String country = null;
    String region = null;
    String city = null;
    JSONObject ipInfoIoJSON = GetJSONfromURL.getIpInfoIo(userIP);
    if (ipInfoIoJSON != null) {
        try {
            userProviderOrg = ipInfoIoJSON.getString("org");
        } catch (JSONException e) {
            userProviderOrg = e.getMessage();
        }
        try {
            userProviderHost = ipInfoIoJSON.getString("hostname");
        } catch (JSONException e) {
            userProviderHost = e.getMessage();
        }
        try {
            country = ipInfoIoJSON.getString("country");
        } catch (JSONException e) {
            country = e.getMessage();
        }
        try {
            region = ipInfoIoJSON.getString("region");
        } catch (JSONException e) {
            region = e.getMessage();
        }
        try {
            city = ipInfoIoJSON.getString("city");
        } catch (JSONException e) {
            city = e.getMessage();
        }
    }
    login.setCryptonomicaUserKey(Key.create(CryptonomicaUser.class, googleUser.getUserId()));
    login.setLoginEmail(new Email(googleUser.getEmail()));
    login.setLoginDate(new Date());
    login.setIP(userIP);
    login.setUserBrowser(userBrowser);
    login.setUserOS(userOS);
    login.setHostname(userProviderHost);
    login.setCity(city);
    login.setRegion(region);
    login.setCountry(country);
    login.setProvider(userProviderOrg);
    // --- save Login:
    Key<Login> loginKey = ofy().save().entity(login).now();
    login = ofy().load().key(loginKey).now();
    return login;
}
Also used : ReadableUserAgent(net.sf.uadetector.ReadableUserAgent) Email(com.google.appengine.api.datastore.Email) JSONObject(org.json.JSONObject) UserAgentStringParser(net.sf.uadetector.UserAgentStringParser) JSONException(org.json.JSONException) Login(net.cryptonomica.entities.Login) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) Date(java.util.Date)

Aggregations

Date (java.util.Date)2 CryptonomicaUser (net.cryptonomica.entities.CryptonomicaUser)2 Login (net.cryptonomica.entities.Login)2 ApiMethod (com.google.api.server.spi.config.ApiMethod)1 Email (com.google.appengine.api.datastore.Email)1 Queue (com.google.appengine.api.taskqueue.Queue)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 ArrayList (java.util.ArrayList)1 PGPPublicKeyData (net.cryptonomica.entities.PGPPublicKeyData)1 LoginView (net.cryptonomica.returns.LoginView)1 NewUserRegistrationReturn (net.cryptonomica.returns.NewUserRegistrationReturn)1 PGPPublicKeyGeneralView (net.cryptonomica.returns.PGPPublicKeyGeneralView)1 UserProfileGeneralView (net.cryptonomica.returns.UserProfileGeneralView)1 ReadableUserAgent (net.sf.uadetector.ReadableUserAgent)1 UserAgentStringParser (net.sf.uadetector.UserAgentStringParser)1 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1