use of net.cryptonomica.entities.CryptonomicaUser in project cryptonomica by Cryptonomica.
the class EthNodeAPI method ethGetDocBySha256.
//
/* ---- AddDocRequestObj Class: */
@ApiMethod(name = "getDocBySha256", path = "getDocBySha256", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public // get doc by sha256
Object ethGetDocBySha256(// final HttpServletRequest httpServletRequest,
final User googleUser, final GetDocBySha256Form getDocBySha256Form) throws IllegalArgumentException, UnauthorizedException {
// ensure registered user ( - may be later only for verificated):
CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
// check form:
LOG.warning("getDocBySha256Form" + getDocBySha256Form);
if (getDocBySha256Form == null || getDocBySha256Form.getSha256() == null || getDocBySha256Form.getSha256().length() < 64 || getDocBySha256Form.getSha256().equals("")) {
throw new IllegalArgumentException("Provided text is to short or empty");
}
if (getDocBySha256Form.getSha256() != null && getDocBySha256Form.getSha256().length() > 64) {
throw new IllegalArgumentException("Provided hash is to long");
}
/* ---- Send request to Ethereum node: */
String ethnodeApiKey = ofy().load().key(Key.create(AppSettings.class, "EthnodeApiKey")).now().getValue();
String urlAddress = "https://ethnode.cryptonomica.net/api/proofofexistence-get";
HTTPResponse httpResponse = HttpService.getDocBySha256(urlAddress, "0x" + getDocBySha256Form.getSha256(), ethnodeApiKey);
LOG.warning("httpResponse: " + new Gson().toJson(httpResponse));
byte[] httpResponseContentBytes = httpResponse.getContent();
String httpResponseContentString = new String(httpResponseContentBytes, StandardCharsets.UTF_8);
// Test:
Object resObj = new Gson().fromJson(httpResponseContentString, Object.class);
LOG.warning("resObj: " + new Gson().toJson(resObj));
return resObj;
}
use of net.cryptonomica.entities.CryptonomicaUser 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;
}
use of net.cryptonomica.entities.CryptonomicaUser in project cryptonomica by Cryptonomica.
the class UserSearchAndViewAPI method getMyProfile.
// end of getUserProfileById @ApiMethod
@// <<< --- not used, use getMyUserData in CryptonomicaUserAPI
ApiMethod(name = "getMyProfile", path = "getMyProfile", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public // returns list of user profiles
UserProfileGeneralView getMyProfile(final User googleUser) throws Exception {
// ensure authorization
UserTools.ensureCryptonomicaRegisteredUser(googleUser);
//
Key<CryptonomicaUser> myKey = Key.create(CryptonomicaUser.class, googleUser.getUserId());
CryptonomicaUser profile = ofy().load().key(myKey).now();
UserProfileGeneralView userProfileGeneralView = new UserProfileGeneralView(profile);
List<PGPPublicKeyData> pgpPublicKeyDataList = ofy().load().type(PGPPublicKeyData.class).ancestor(myKey).list();
ArrayList<PGPPublicKeyGeneralView> pgpPublicKeyGeneralViews = new ArrayList<>();
for (PGPPublicKeyData pgpPublicKeyData : pgpPublicKeyDataList) {
pgpPublicKeyGeneralViews.add(new PGPPublicKeyGeneralView(pgpPublicKeyData));
}
userProfileGeneralView.setPgpPublicKeyGeneralViews(pgpPublicKeyGeneralViews);
return userProfileGeneralView;
}
use of net.cryptonomica.entities.CryptonomicaUser in project cryptonomica by Cryptonomica.
the class UserSearchAndViewAPI method getUserProfileById.
// end of generalSearchUserProfiles @ApiMethod
@ApiMethod(name = "getUserProfileById", path = "getUserProfileById", httpMethod = ApiMethod.HttpMethod.GET)
@SuppressWarnings("unused")
public // returns list of user profiles
UserProfileGeneralView getUserProfileById(final User googleUser, @Named("id") final String id) throws Exception {
/* Check authorization: */
UserTools.ensureCryptonomicaRegisteredUser(googleUser);
/* Validate form: */
LOG.warning("@Named(\"id\"): " + id);
if (id == null || id.length() < 1) {
throw new Exception("User id is empty");
}
/* Load CryptonomicaUser from DB */
// create key
Key<CryptonomicaUser> cryptonomicaUserKey = Key.create(CryptonomicaUser.class, id);
// get user with given Id from DB:
CryptonomicaUser profile = ofy().load().key(cryptonomicaUserKey).now();
// check result:
if (profile == null) {
LOG.warning("User with not found");
throw new Exception("User not found");
}
/* Create UserProfileGeneralView object to return */
// find his public keys:
List<PGPPublicKeyData> pgpPublicKeyDataList = ofy().load().type(PGPPublicKeyData.class).filter("cryptonomicaUserId", id).list();
// check keys found, if no webSafeString add and store in DB :
LOG.info("pgpPublicKeyDataList: " + new Gson().toJson(pgpPublicKeyDataList));
for (PGPPublicKeyData k : pgpPublicKeyDataList) {
if (k.getWebSafeString() == null) {
if (k.getCryptonomicaUserKey() != null) {
k.setWebSafeString(Key.create(k.getCryptonomicaUserKey(), PGPPublicKeyData.class, k.getFingerprint()).toWebSafeString());
} else {
// for old keys without parent
k.setWebSafeString(Key.create(PGPPublicKeyData.class, k.getFingerprint()).toWebSafeString());
}
// async !
ofy().save().entity(k);
}
}
// create obj to return:
UserProfileGeneralView userProfileGeneralView = new UserProfileGeneralView(profile, pgpPublicKeyDataList);
LOG.warning("userProfileGeneralView: " + userProfileGeneralView.toJson());
return userProfileGeneralView;
}
use of net.cryptonomica.entities.CryptonomicaUser in project cryptonomica by Cryptonomica.
the class UserSearchAndViewAPI method generalSearchUserProfiles.
@ApiMethod(name = "generalSearchUserProfiles", path = "generalSearchUserProfiles", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public // returns list of user profiles
UserSearchAndViewReturn generalSearchUserProfiles(final HttpServletRequest httpServletRequest, final User googleUser, final GeneralSearchUserProfilesForm generalSearchUserProfilesForm) throws Exception {
LOG.info("serch request: " + new Gson().toJson(generalSearchUserProfilesForm));
// authorization
UserTools.ensureCryptonomicaRegisteredUser(googleUser);
// initialize object for search result:
List<CryptonomicaUser> cryptonomicaUsersList;
// set "" value to be null
if (generalSearchUserProfilesForm.getFirstName() != null && generalSearchUserProfilesForm.getFirstName().equals("")) {
generalSearchUserProfilesForm.setFirstName(null);
}
if (generalSearchUserProfilesForm.getLastName() != null && generalSearchUserProfilesForm.getLastName().equals("")) {
generalSearchUserProfilesForm.setLastName(null);
}
if (generalSearchUserProfilesForm.getEmail() != null && generalSearchUserProfilesForm.getEmail().equals("")) {
generalSearchUserProfilesForm.setEmail(null);
}
// search: first name + last name
if (generalSearchUserProfilesForm.getFirstName() != null && generalSearchUserProfilesForm.getLastName() != null && generalSearchUserProfilesForm.getEmail() == null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("firstName", generalSearchUserProfilesForm.getFirstName().toLowerCase()).filter("lastName", generalSearchUserProfilesForm.getLastName().toLowerCase()).list();
} else // first name + last name + email
if (generalSearchUserProfilesForm.getFirstName() != null && generalSearchUserProfilesForm.getLastName() != null && generalSearchUserProfilesForm.getEmail() != null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("firstName", generalSearchUserProfilesForm.getFirstName().toLowerCase()).filter("lastName", generalSearchUserProfilesForm.getLastName().toLowerCase()).filter("email", new Email(generalSearchUserProfilesForm.getEmail().toLowerCase())).list();
} else // first name + email
if (generalSearchUserProfilesForm.getFirstName() != null && generalSearchUserProfilesForm.getLastName() == null && generalSearchUserProfilesForm.getEmail() != null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("firstName", generalSearchUserProfilesForm.getFirstName().toLowerCase()).filter("email", new Email(generalSearchUserProfilesForm.getEmail().toLowerCase())).list();
} else // last name + email
if (generalSearchUserProfilesForm.getFirstName() == null && generalSearchUserProfilesForm.getLastName() != null && generalSearchUserProfilesForm.getEmail() != null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("lastName", generalSearchUserProfilesForm.getLastName().toLowerCase()).filter("email", new Email(generalSearchUserProfilesForm.getEmail().toLowerCase())).list();
} else // email only
if (generalSearchUserProfilesForm.getFirstName() == null && generalSearchUserProfilesForm.getLastName() == null && generalSearchUserProfilesForm.getEmail() != null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("email", new Email(generalSearchUserProfilesForm.getEmail().toLowerCase())).list();
} else // first name only
if (generalSearchUserProfilesForm.getFirstName() != null && generalSearchUserProfilesForm.getLastName() == null && generalSearchUserProfilesForm.getEmail() == null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("firstName", generalSearchUserProfilesForm.getFirstName().toLowerCase()).list();
} else // last name
if (generalSearchUserProfilesForm.getFirstName() == null && generalSearchUserProfilesForm.getLastName() != null && generalSearchUserProfilesForm.getEmail() == null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("lastName", generalSearchUserProfilesForm.getLastName().toLowerCase()).list();
} else {
throw new Exception("Search query is incorrect or empty");
}
//
UserSearchAndViewReturn userSearchAndViewReturn;
if (cryptonomicaUsersList != null && !cryptonomicaUsersList.isEmpty()) {
userSearchAndViewReturn = new UserSearchAndViewReturn("results found", new ArrayList<>(cryptonomicaUsersList));
} else {
userSearchAndViewReturn = new UserSearchAndViewReturn("no results found");
}
LOG.info("search result: " + new Gson().toJson(userSearchAndViewReturn));
return userSearchAndViewReturn;
}
Aggregations