Search in sources :

Example 1 with NotFoundException

use of com.google.api.server.spi.response.NotFoundException 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 2 with NotFoundException

use of com.google.api.server.spi.response.NotFoundException 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 3 with NotFoundException

use of com.google.api.server.spi.response.NotFoundException in project cryptonomica by Cryptonomica.

the class VerificationAPI method getVerificationByID.

/* --- Get verification info by verification ID: */
@ApiMethod(name = "getVerificationByID", path = "getVerificationByID", httpMethod = ApiMethod.HttpMethod.GET)
@SuppressWarnings("unused")
public VerificationGeneralView getVerificationByID(final HttpServletRequest httpServletRequest, final User googleUser, @Named("verificationID") final String verificationID) throws // see: https://cloud.google.com/appengine/docs/java/endpoints/exceptions
UnauthorizedException, BadRequestException, NotFoundException {
    /* --- Check authorization: */
    CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
    /* --- Check input: */
    if (verificationID == null || verificationID.equals("")) {
        throw new BadRequestException("Verification ID missing");
    }
    /* --- Load verification entity from DS: */
    Verification verification = ofy().load().key(Key.create(Verification.class, verificationID)).now();
    if (verification == null) {
        throw new NotFoundException("Verification info not found");
    }
    /* --- Create new verification info representation: */
    VerificationGeneralView verificationGeneralView = new VerificationGeneralView(verification);
    LOG.warning(new Gson().toJson(verificationGeneralView));
    return verificationGeneralView;
}
Also used : BadRequestException(com.google.api.server.spi.response.BadRequestException) NotFoundException(com.google.api.server.spi.response.NotFoundException) Gson(com.google.gson.Gson) Verification(net.cryptonomica.entities.Verification) VerificationGeneralView(net.cryptonomica.returns.VerificationGeneralView) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 4 with NotFoundException

use of com.google.api.server.spi.response.NotFoundException in project endpoints-java by cloudendpoints.

the class CachingDiscoveryProviderTest method getRpcDocument_notFound.

@Test
public void getRpcDocument_notFound() throws Exception {
    CachingDiscoveryProvider provider = createNonExpiringProvider();
    when(delegate.getRpcDocument(ROOT, NAME, VERSION)).thenThrow(new NotFoundException(""));
    try {
        provider.getRpcDocument(ROOT, NAME, VERSION);
        fail("expected NotFoundException");
    } catch (NotFoundException e) {
    // expected
    }
}
Also used : NotFoundException(com.google.api.server.spi.response.NotFoundException) Test(org.junit.Test)

Example 5 with NotFoundException

use of com.google.api.server.spi.response.NotFoundException in project cryptonomica by Cryptonomica.

the class OnlineVerificationAPI method approve.

// end of acceptTerms();
// /* --- Approve online verification (for Cryptonomica Complience Officer)  */
@ApiMethod(name = "approve", path = "approve", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public BooleanWrapperObject approve(// final HttpServletRequest httpServletRequest,
final User googleUser, @Named("onlineVerificationApproved") final Boolean onlineVerificationApproved, @Named("verificationNotes") final String verificationNotes, @Named("fingerprint") final String fingerprint) throws // see: https://cloud.google.com/appengine/docs/java/endpoints/exceptions
UnauthorizedException, BadRequestException, NotFoundException, NumberParseException, IllegalArgumentException {
    /* --- Check authorization : CRYPTONOMICA OFFICER ONLY !!! */
    CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaOfficer(googleUser);
    /* --- Check input: */
    if (fingerprint == null || fingerprint.equals("") || fingerprint.length() != 40) {
        throw new BadRequestException("fingerprint is missing or invalid");
    }
    if (onlineVerificationApproved == null) {
        throw new IllegalArgumentException("onlineVerificationApproved is null");
    } else if (onlineVerificationApproved == false) {
        throw new BadRequestException("onlineVerificationApproved (checkbox): false");
    }
    // Check if OnlineVerification entity exists:
    OnlineVerification onlineVerification = ofy().load().key(Key.create(OnlineVerification.class, fingerprint)).now();
    if (onlineVerification == null) {
        throw new NotFoundException("OnlineVeriication entity for fingerprint " + fingerprint + " does not exist in data base");
    } else if (onlineVerification.getOnlineVerificationDataVerified() != null && onlineVerification.getOnlineVerificationDataVerified()) {
        throw new BadRequestException("OnlineVerification already approved");
    }
    // mark Online Verification as approved:
    // <<<<<< !!!
    onlineVerification.setOnlineVerificationDataVerified(onlineVerificationApproved);
    onlineVerification.setVerifiedById(googleUser.getUserId());
    onlineVerification.setVerifiedByFirstNameLastName(cryptonomicaUser.getFirstName() + " " + cryptonomicaUser.getLastName());
    onlineVerification.setVerifiedOn(new Date());
    onlineVerification.setVerificationNotes(verificationNotes);
    // mark key as verified:
    PGPPublicKeyData pgpPublicKeyData = ofy().load().type(PGPPublicKeyData.class).filter("fingerprintStr", fingerprint).first().now();
    if (pgpPublicKeyData == null) {
        throw new NotFoundException("Key with fingerprint " + fingerprint + " not found");
    }
    // 
    pgpPublicKeyData.setOnlineVerificationFinished(Boolean.TRUE);
    pgpPublicKeyData.setNationality(onlineVerification.getNationality().toUpperCase());
    // save data to data store:
    ofy().save().entity(onlineVerification).now();
    ofy().save().entity(pgpPublicKeyData).now();
    // Send email to user:
    final Queue queue = QueueFactory.getDefaultQueue();
    queue.add(TaskOptions.Builder.withUrl("/_ah/SendGridServlet").param("email", onlineVerification.getUserEmail().getEmail()).param("emailCC", "verification@cryptonomica.net").param("messageSubject", "[cryptonomica] Online verification for key: " + onlineVerification.getKeyID() + " approved").param("messageText", "Congratulation! \n\n" + onlineVerification.getFirstName().toUpperCase() + " " + onlineVerification.getLastName().toUpperCase() + ",\n\n" + "your request for online verification for key with fingerprint : " + fingerprint + " approved! \n\n" + "See verification information on:\n" + "https://cryptonomica.net/#/onlineVerificationView/" + fingerprint + "\n" + "(information is not public, you have to login with your google account " + onlineVerification.getUserEmail().getEmail() + ")\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 support@cryptonomica.net\n\n"));
    // create result object:
    BooleanWrapperObject result = new BooleanWrapperObject(onlineVerificationApproved, "Online Verification for key " + fingerprint + " approved");
    return result;
}
Also used : BadRequestException(com.google.api.server.spi.response.BadRequestException) NotFoundException(com.google.api.server.spi.response.NotFoundException) BooleanWrapperObject(net.cryptonomica.returns.BooleanWrapperObject) Queue(com.google.appengine.api.taskqueue.Queue) Date(java.util.Date) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Aggregations

NotFoundException (com.google.api.server.spi.response.NotFoundException)13 ApiMethod (com.google.api.server.spi.config.ApiMethod)7 BadRequestException (com.google.api.server.spi.response.BadRequestException)5 Test (org.junit.Test)4 Gson (com.google.gson.Gson)3 ApiKey (com.google.api.server.spi.config.model.ApiKey)2 Queue (com.google.appengine.api.taskqueue.Queue)2 CryptonomicaUser (net.cryptonomica.entities.CryptonomicaUser)2 Verification (net.cryptonomica.entities.Verification)2 VerificationGeneralView (net.cryptonomica.returns.VerificationGeneralView)2 ApiConfig (com.google.api.server.spi.config.model.ApiConfig)1 UnauthorizedException (com.google.api.server.spi.response.UnauthorizedException)1 RestDescription (com.google.api.services.discovery.model.RestDescription)1 GsonBuilder (com.google.gson.GsonBuilder)1 Device (com.google.samples.apps.iosched.server.gcm.db.models.Device)1 MessageSender (com.google.samples.apps.iosched.server.gcm.device.MessageSender)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 BooleanWrapperObject (net.cryptonomica.returns.BooleanWrapperObject)1 OnlineVerificationView (net.cryptonomica.returns.OnlineVerificationView)1