Search in sources :

Example 1 with ApiMethod

use of com.google.api.server.spi.config.ApiMethod in project cryptonomica by Cryptonomica.

the class OnlineVerificationAPI method sendTestSms.

// end: getDocumentsUploadKey
/* --- Test SMS service: */
@ApiMethod(name = "sendTestSms", path = "sendTestSms", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public StringWrapperObject sendTestSms(// final HttpServletRequest httpServletRequest,
final User googleUser, @Named("phoneNumber") final String phoneNumber, @Named("smsMessage") final String smsMessage) throws // see: https://cloud.google.com/appengine/docs/java/endpoints/exceptions
UnauthorizedException, BadRequestException, NotFoundException, NumberParseException, IllegalArgumentException, TwilioRestException {
    /* --- Check authorization: */
    CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaOfficer(googleUser);
    /* --- Send SMS */
    Message message = TwilioUtils.sendSms(phoneNumber, smsMessage);
    return new StringWrapperObject(message.toJSON());
}
Also used : Message(com.twilio.sdk.resource.instance.Message) StringWrapperObject(net.cryptonomica.returns.StringWrapperObject) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 2 with ApiMethod

use of com.google.api.server.spi.config.ApiMethod in project cryptonomica by Cryptonomica.

the class OnlineVerificationAPI method getOnlineVerificationByFingerprint.

/* --- Get online verification info by OpenPGP Public Key fingerprint  : */
@ApiMethod(name = "getOnlineVerificationByFingerprint", path = "getOnlineVerificationByFingerprint", httpMethod = ApiMethod.HttpMethod.GET)
@SuppressWarnings("unused")
public OnlineVerificationView getOnlineVerificationByFingerprint(final HttpServletRequest httpServletRequest, final User googleUser, @Named("fingerprint") final String fingerprint) throws // see: https://cloud.google.com/appengine/docs/java/endpoints/exceptions
UnauthorizedException, BadRequestException, NotFoundException {
    /* --- Check input: */
    if (fingerprint == null || fingerprint.equals("") || fingerprint.length() != 40) {
        throw new BadRequestException("fingerprint is missing or invalid");
    }
    PGPPublicKeyData pgpPublicKeyData = ofy().load().type(PGPPublicKeyData.class).filter("fingerprintStr", fingerprint).first().now();
    if (pgpPublicKeyData == null) {
        throw new NotFoundException("Key with fingerprint " + fingerprint + " not found");
    }
    /* --- Check authorization: */
    // only allowed users can get verification data:
    // << registered user
    CryptonomicaUser requester = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
    LOG.warning(GSON.toJson(requester));
    // >>>>>>>>>>>>>>>>>> New OnlineVerifications are created here !!!
    // (user first have to request verification to make changes to it)
    OnlineVerification onlineVerification = ofy().load().key(Key.create(OnlineVerification.class, fingerprint)).now();
    if (onlineVerification == null) {
        if (requester.getUserId().equalsIgnoreCase(pgpPublicKeyData.getCryptonomicaUserId())) {
            onlineVerification = new OnlineVerification(pgpPublicKeyData);
            ofy().save().entity(onlineVerification).now();
        } else {
            throw new NotFoundException("Online verification data for fingerprint " + fingerprint + " not found");
        }
    }
    if (requester.getUserId().equalsIgnoreCase(pgpPublicKeyData.getCryptonomicaUserId()) || (requester.getCryptonomicaOfficer() != null && requester.getCryptonomicaOfficer()) || // || (requester.getNotary() != null && requester.getNotary()) // TODO: should all notaries have access?
    (onlineVerification.getAllowedUsers().contains(requester.getUserId()))) {
        LOG.warning("user " + requester.getUserId() + "is allowed to get online verification data for key " + fingerprint);
    } else {
        throw new UnauthorizedException("you are not allowed to get online verification data for key " + fingerprint);
    }
    LOG.warning(GSON.toJson(onlineVerification));
    ArrayList<VerificationDocument> verificationDocumentArrayList = new ArrayList<>();
    int verificationDocumentsListSize = ofy().load().type(VerificationDocument.class).filter("fingerprint", fingerprint).filter("hidden", false).list().size();
    LOG.warning("verificationDocumentsListSize: " + verificationDocumentsListSize);
    if (verificationDocumentsListSize > 0) {
        List<VerificationDocument> verificationDocumentList = ofy().load().type(VerificationDocument.class).filter("fingerprint", fingerprint).filter("hidden", false).list();
        verificationDocumentArrayList.addAll(verificationDocumentList);
        LOG.warning(GSON.toJson(verificationDocumentArrayList));
    }
    OnlineVerificationView onlineVerificationView = new OnlineVerificationView(onlineVerification, verificationDocumentArrayList);
    LOG.warning("onlineVerificationView:");
    LOG.warning(onlineVerificationView.toString());
    return onlineVerificationView;
}
Also used : UnauthorizedException(com.google.api.server.spi.response.UnauthorizedException) ArrayList(java.util.ArrayList) BadRequestException(com.google.api.server.spi.response.BadRequestException) NotFoundException(com.google.api.server.spi.response.NotFoundException) OnlineVerificationView(net.cryptonomica.returns.OnlineVerificationView) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 3 with ApiMethod

use of com.google.api.server.spi.config.ApiMethod in project cryptonomica by Cryptonomica.

the class OnlineVerificationAPI method checkSms.

// end of sendTestSms();
/* --- Check SMS  */
@ApiMethod(name = "checkSms", path = "checkSms", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public StringWrapperObject checkSms(// final HttpServletRequest httpServletRequest,
final User googleUser, @Named("smsMessage") final String smsMessage, @Named("fingerprint") final String fingerprint) throws // see: https://cloud.google.com/appengine/docs/java/endpoints/exceptions
UnauthorizedException, BadRequestException, NotFoundException, NumberParseException, IllegalArgumentException, TwilioRestException {
    /* --- Check authorization: */
    CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
    /* --- Check if OnlineVerificaiton entity exists */
    OnlineVerification onlineVerification = ofy().load().key(Key.create(OnlineVerification.class, fingerprint)).now();
    if (onlineVerification == null) {
        throw new NotFoundException("OnlineVerification entity does not exist in data base");
    }
    // --- store SMS:
    PhoneVerification phoneVerification = null;
    phoneVerification = ofy().load().key(Key.create(PhoneVerification.class, fingerprint)).now();
    if (phoneVerification == null) {
        throw new NotFoundException("Send sms message not found for key " + fingerprint);
    }
    LOG.warning("phoneVerification.getSmsMessage(): " + phoneVerification.getSmsMessage());
    LOG.warning("smsMessage: " + smsMessage);
    Boolean verificationResult = phoneVerification.getSmsMessage().toString().equalsIgnoreCase(smsMessage);
    phoneVerification.setVerified(verificationResult);
    StringWrapperObject result = new StringWrapperObject();
    if (verificationResult) {
        result.setMessage("Phone verified!");
    } else {
        phoneVerification.setFailedVerificationAttemps(phoneVerification.getFailedVerificationAttemps() + 1);
        ofy().save().entity(phoneVerification).now();
        if (phoneVerification.getFailedVerificationAttemps() >= 3) {
            throw new BadRequestException("The number of attempts is exhausted. Please resend new sms");
        } else {
            throw new BadRequestException("Code does not much. It was attempt # " + phoneVerification.getFailedVerificationAttemps());
        }
    }
    // save phone verification
    ofy().save().entity(phoneVerification).now();
    // record to verification
    onlineVerification.setPhoneNumber(phoneVerification.getPhoneNumber());
    ofy().save().entity(onlineVerification).now();
    return result;
}
Also used : StringWrapperObject(net.cryptonomica.returns.StringWrapperObject) NotFoundException(com.google.api.server.spi.response.NotFoundException) BadRequestException(com.google.api.server.spi.response.BadRequestException) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 4 with ApiMethod

use of com.google.api.server.spi.config.ApiMethod in project cryptonomica by Cryptonomica.

the class OnlineVerificationAPI method sendSms.

// end of sendTestSms();
/* --- Send SMS : */
@ApiMethod(name = "sendSms", path = "sendSms", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public StringWrapperObject sendSms(// final HttpServletRequest httpServletRequest,
final User googleUser, @Named("phoneNumber") final String phoneNumber, // in international format, f.e. +972523333333
@Named("fingerprint") final String fingerprint) throws // see: https://cloud.google.com/appengine/docs/java/endpoints/exceptions
UnauthorizedException, BadRequestException, NotFoundException, NumberParseException, IllegalArgumentException, TwilioRestException {
    /* --- Check authorization: */
    CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
    // --- create SMS:
    String smsMessage = RandomStringUtils.randomNumeric(7);
    LOG.warning("smsMessage: " + smsMessage);
    // --- store SMS:
    PhoneVerification phoneVerification = null;
    phoneVerification = ofy().load().key(Key.create(PhoneVerification.class, fingerprint)).now();
    if (phoneVerification == null) {
        phoneVerification = new PhoneVerification(fingerprint);
    }
    if (phoneVerification.getVerified()) {
        throw new BadRequestException("Phone already verified for this OpenPGP public key " + fingerprint);
    }
    phoneVerification.setPhoneNumber(phoneNumber);
    phoneVerification.setUserEmail(cryptonomicaUser.getEmail());
    phoneVerification.setSmsMessage(smsMessage);
    phoneVerification.setFailedVerificationAttemps(0);
    phoneVerification.setSmsMessageSend(new Date());
    LOG.warning(GSON.toJson(phoneVerification));
    /* --- Send SMS */
    Message message = TwilioUtils.sendSms(phoneNumber, smsMessage);
    LOG.warning(message.toJSON());
    /* --- Save phoneVerification */
    ofy().save().entity(phoneVerification).now();
    return new StringWrapperObject("SMS message send successfully");
}
Also used : Message(com.twilio.sdk.resource.instance.Message) StringWrapperObject(net.cryptonomica.returns.StringWrapperObject) BadRequestException(com.google.api.server.spi.response.BadRequestException) Date(java.util.Date) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 5 with ApiMethod

use of com.google.api.server.spi.config.ApiMethod in project cryptonomica by Cryptonomica.

the class PGPPublicKeyAPI method addFingerprintStrProperties.

@ApiMethod(name = "addFingerprintStrProperties", path = "addFingerprintStrProperties", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public // (fingerprint -> fingerprintStr)
StringWrapperObject addFingerprintStrProperties(final User googleUser) throws Exception {
    /* Check authorization: */
    UserTools.ensureCryptonomicaOfficer(googleUser);
    /* Load PGPPublicKeyData from DB*/
    List<PGPPublicKeyData> pgpPublicKeyDataList = ofy().load().type(PGPPublicKeyData.class).limit(20).list();
    if (pgpPublicKeyDataList.size() > 10) {
        throw new Exception("there are to many keys in the database");
    }
    for (PGPPublicKeyData pgpPublicKeyData : pgpPublicKeyDataList) {
        pgpPublicKeyData.setFingerprintStr(pgpPublicKeyData.getFingerprint());
    }
    Map<Key<PGPPublicKeyData>, PGPPublicKeyData> result = ofy().save().entities(pgpPublicKeyDataList).now();
    String resultJSON = new Gson().toJson(result);
    return new StringWrapperObject(resultJSON);
}
Also used : PGPPublicKeyData(net.cryptonomica.entities.PGPPublicKeyData) StringWrapperObject(net.cryptonomica.returns.StringWrapperObject) Gson(com.google.gson.Gson) PGPPublicKey(org.bouncycastle.openpgp.PGPPublicKey) Key(com.googlecode.objectify.Key) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Aggregations

ApiMethod (com.google.api.server.spi.config.ApiMethod)54 CryptonomicaUser (net.cryptonomica.entities.CryptonomicaUser)19 Gson (com.google.gson.Gson)16 UserData (com.google.samples.apps.iosched.server.userdata.db.UserData)10 PGPPublicKeyData (net.cryptonomica.entities.PGPPublicKeyData)10 ArrayList (java.util.ArrayList)9 StringWrapperObject (net.cryptonomica.returns.StringWrapperObject)9 NotFoundException (com.google.api.server.spi.response.NotFoundException)8 BadRequestException (com.google.api.server.spi.response.BadRequestException)7 UnauthorizedException (com.google.api.server.spi.response.UnauthorizedException)7 Queue (com.google.appengine.api.taskqueue.Queue)7 HTTPResponse (com.google.appengine.api.urlfetch.HTTPResponse)6 Device (com.google.samples.apps.iosched.server.gcm.db.models.Device)6 MessageSender (com.google.samples.apps.iosched.server.gcm.device.MessageSender)5 AppSettings (net.cryptonomica.entities.AppSettings)5 PGPPublicKeyGeneralView (net.cryptonomica.returns.PGPPublicKeyGeneralView)5 UserProfileGeneralView (net.cryptonomica.returns.UserProfileGeneralView)5 BookmarkedSession (com.google.samples.apps.iosched.server.userdata.db.BookmarkedSession)4 BooleanWrapperObject (net.cryptonomica.returns.BooleanWrapperObject)4 PGPPublicKey (org.bouncycastle.openpgp.PGPPublicKey)4