use of net.cryptonomica.returns.PGPPublicKeyUploadReturn in project cryptonomica by Cryptonomica.
the class PGPPublicKeyAPI method uploadNewPGPPublicKey.
// end of getPGPPublicKeyByFingerprint()
@ApiMethod(name = "uploadNewPGPPublicKey", path = "uploadNewPGPPublicKey", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public PGPPublicKeyUploadReturn uploadNewPGPPublicKey(// final HttpServletRequest httpServletRequest,
final User googleUser, final PGPPublicKeyUploadForm pgpPublicKeyUploadForm) throws Exception {
// authorization
CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
//
if (pgpPublicKeyUploadForm == null || pgpPublicKeyUploadForm.getAsciiArmored() == null || pgpPublicKeyUploadForm.getAsciiArmored().length() == 0) {
throw new Exception("ASCII-armored key is empty");
}
// --- LOG request form: (after check if not null)
LOG.warning(GSON.toJson(pgpPublicKeyUploadForm));
String asciiArmored = pgpPublicKeyUploadForm.getAsciiArmored();
PGPPublicKey pgpPublicKey = PGPTools.readPublicKeyFromString(asciiArmored);
// -> throws IOException, PGPException
LOG.warning(GSON.toJson(pgpPublicKey));
PGPPublicKeyData pgpPublicKeyData = PGPTools.checkPublicKey(pgpPublicKey, asciiArmored, cryptonomicaUser);
pgpPublicKeyData.setUserBirthday(cryptonomicaUser.getBirthday());
// -- add @Parent value: ---
pgpPublicKeyData.setCryptonomicaUserKey(Key.create(CryptonomicaUser.class, googleUser.getUserId()));
// save key
Key<PGPPublicKeyData> pgpPublicKeyDataKey = ofy().save().entity(pgpPublicKeyData).now();
// load key from DB and and create return object
String messageToUser = "Key " + pgpPublicKeyData.getFingerprint() + " saved in data base";
PGPPublicKeyGeneralView pgpPublicKeyGeneralView = new PGPPublicKeyGeneralView(ofy().load().key(pgpPublicKeyDataKey).now());
PGPPublicKeyUploadReturn pgpPublicKeyUploadReturn = new PGPPublicKeyUploadReturn(messageToUser, pgpPublicKeyGeneralView);
return pgpPublicKeyUploadReturn;
}
Aggregations