Search in sources :

Example 1 with PGPPublicKeyUploadReturn

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;
}
Also used : PGPPublicKeyData(net.cryptonomica.entities.PGPPublicKeyData) PGPPublicKeyGeneralView(net.cryptonomica.returns.PGPPublicKeyGeneralView) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) PGPPublicKeyUploadReturn(net.cryptonomica.returns.PGPPublicKeyUploadReturn) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Aggregations

ApiMethod (com.google.api.server.spi.config.ApiMethod)1 CryptonomicaUser (net.cryptonomica.entities.CryptonomicaUser)1 PGPPublicKeyData (net.cryptonomica.entities.PGPPublicKeyData)1 PGPPublicKeyGeneralView (net.cryptonomica.returns.PGPPublicKeyGeneralView)1 PGPPublicKeyUploadReturn (net.cryptonomica.returns.PGPPublicKeyUploadReturn)1 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)1