Search in sources :

Example 41 with ApiMethod

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

the class EthNodeAPI method getVerificationDataFromSmartContractByFingerprint.

@ApiMethod(name = "getVerificationFromSCbyFingerprint", path = "getVerificationFromSCbyFingerprint", httpMethod = ApiMethod.HttpMethod.GET)
@SuppressWarnings("unused")
public StringWrapperObject getVerificationDataFromSmartContractByFingerprint(// final HttpServletRequest httpServletRequest,
final User googleUser, @Named("fingerprint") final String fingerprint) throws IllegalArgumentException, UnauthorizedException {
    // ensure registered user ( - may be later only for verified):
    CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
    // check form:
    LOG.warning("fingerprint" + fingerprint);
    if (fingerprint == null || fingerprint.equals("") || fingerprint.length() != 40) {
        throw new IllegalArgumentException("Provided fingerprint is not valid");
    }
    String tomcatWeb3jAPIkey = ofy().load().key(Key.create(AppSettings.class, "tomcatweb3jAPIkey")).now().getValue();
    String urlHost = "https://tomcatweb3j.cryptonomica.net";
    String urlPath = "/getVerification";
    String urlAddress = urlHost + urlPath;
    // HashMap<String, String> queryMap = new HashMap<>();
    // queryMap.put("address", ethereumAcc);
    String postRequestBody = "fingerprint=" + fingerprint;
    HTTPResponse httpResponse = HttpService.postRequestWithAPIkey(urlAddress, postRequestBody, tomcatWeb3jAPIkey);
    byte[] httpResponseContentBytes = httpResponse.getContent();
    String httpResponseContentString = new String(httpResponseContentBytes, StandardCharsets.UTF_8);
    LOG.warning("httpResponseContentString: " + httpResponseContentString);
    return new StringWrapperObject(httpResponseContentString);
}
Also used : AppSettings(net.cryptonomica.entities.AppSettings) StringWrapperObject(net.cryptonomica.returns.StringWrapperObject) HTTPResponse(com.google.appengine.api.urlfetch.HTTPResponse) CryptonomicaUser(net.cryptonomica.entities.CryptonomicaUser) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 42 with ApiMethod

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

the class NotaryAPI method verifyPGPPublicKey.

// end @ApiMethod addOrRewriteNotary
@ApiMethod(name = "verifyPGPPublicKey", path = "verifyPGPPublicKey", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public VerifyPGPPublicKeyReturn verifyPGPPublicKey(// google user
final User verifyingPerson, // 4 properties
final VerifyPGPPublicKeyForm verifyPGPPublicKeyForm) throws Exception {
    /* Check authorization: */
    CryptonomicaUser cryptonomicaUser = UserTools.ensureNotaryOrCryptonomicaOfficer(verifyingPerson);
    /*
        if (verifyPGPPublicKeyForm.getWebSafeString() == null
                || verifyPGPPublicKeyForm.getWebSafeString().length() < 1) {
            LOG.warning("VerifyPGPPublicKeyForm.webSafeString: "
                    + verifyPGPPublicKeyForm.getWebSafeString());
            throw new Exception("PGP public key not not identified");
        }
        */
    if (verifyPGPPublicKeyForm.getFingerprint() == null || verifyPGPPublicKeyForm.getFingerprint().length() < 1) {
        LOG.warning("VerifyPGPPublicKeyForm.getFingerprint(): " + verifyPGPPublicKeyForm.getFingerprint());
        throw new Exception("Key fingerprint missing or empty");
    }
    // 2)
    if (verifyPGPPublicKeyForm.getVerificationInfo() == null || verifyPGPPublicKeyForm.getVerificationInfo().length() < 1) {
        LOG.warning("verifyPGPPublicKeyForm.verificationInfo: " + verifyPGPPublicKeyForm.getVerificationInfo());
        throw new Exception("Public key verification info is missing");
    }
    // 3)
    if (verifyPGPPublicKeyForm.getBasedOnDocument() == null || verifyPGPPublicKeyForm.getBasedOnDocument().length() < 1) {
        throw new Exception("Document not specified");
    }
    // 4)
    if (verifyPGPPublicKeyForm.getNationality() == null || verifyPGPPublicKeyForm.getNationality().isEmpty() || verifyPGPPublicKeyForm.getNationality().length() != 2) {
        throw new IllegalArgumentException("User nationality shold 2-letter country code defined in ISO 3166");
    }
    ArrayList<String> isoCountries = new ArrayList<String>(Arrays.asList(Locale.getISOCountries()));
    if (!isoCountries.contains(verifyPGPPublicKeyForm.getNationality().toUpperCase())) {
        throw new IllegalArgumentException("2-letter country code provided is not from ISO 3166");
    }
    /* Load PGPPublicKeyData */
    /*
        Key<PGPPublicKeyData> pgpPublicKeyDataKEY = Key.create(verifyPGPPublicKeyForm.getFingerprint());
        PGPPublicKeyData pgpPublicKeyData = ofy()
                .load()
                .key(pgpPublicKeyDataKEY)
                .now();
        */
    // GET Key from DataBase by fingerprint:
    String fingerprint = verifyPGPPublicKeyForm.getFingerprint();
    PGPPublicKeyData pgpPublicKeyData = PGPTools.getPGPPublicKeyDataFromDataBaseByFingerprint(fingerprint);
    /* Check if key paid */
    if (pgpPublicKeyData.getPaid() == null || pgpPublicKeyData.getPaid() == Boolean.FALSE) {
        throw new Exception("This key is unpaid, and can not be verified. Please make a payment first.");
    }
    // create verification
    Key<PGPPublicKeyData> pgpPublicKeyDataKEY = Key.create(pgpPublicKeyData);
    // from:
    // http://static.javadoc.io/com.googlecode.objectify/objectify/5.1.12/com/googlecode/objectify/Key.html#create-T-
    // create
    // public static <T> Key<T> create(T pojo)
    // Create a key from a registered POJO entity.
    // TODO: check if works correctly
    Verification verification = new Verification(// verification data
    verifyPGPPublicKeyForm, // verified by
    cryptonomicaUser, // key of the entity to be verified
    pgpPublicKeyDataKEY);
    // save in DB and load verification to assign an ID (@Id private Long Id)
    Key<Verification> verificationKey = ofy().save().entity(verification).now();
    // load saved entity from database:
    verification = // 
    ofy().load().key(verificationKey).now();
    LOG.warning("saved verification: " + verification.toJson());
    // > nationality first >>
    if (pgpPublicKeyData.getNationality() == null || pgpPublicKeyData.getNationality().isEmpty()) {
        pgpPublicKeyData.setNationality(verifyPGPPublicKeyForm.getNationality().toUpperCase());
    } else if (!pgpPublicKeyData.getNationality().equals(verifyPGPPublicKeyForm.getNationality().toUpperCase())) {
        throw new IllegalArgumentException("Nationality info stored with this key certificate (" + pgpPublicKeyData.getNationality() + ") is different than nationality provided now (" + verifyPGPPublicKeyForm.getNationality() + ")");
    }
    pgpPublicKeyData.setActive(Boolean.TRUE);
    pgpPublicKeyData.setVerified(Boolean.TRUE);
    pgpPublicKeyData.addVerification(verificationKey.toWebSafeString());
    ofy().save().entity(pgpPublicKeyData).now();
    // Redundant. To ensure everything works.
    pgpPublicKeyData = ofy().load().key(pgpPublicKeyDataKEY).now();
    // 
    Key<CryptonomicaUser> keyOwnerKEY = Key.create(CryptonomicaUser.class, pgpPublicKeyData.getCryptonomicaUserId());
    CryptonomicaUser keyOwner = ofy().load().key(keyOwnerKEY).now();
    // set active, save to DB:
    // <<
    keyOwner.setActive(Boolean.TRUE);
    // async
    ofy().save().entity(keyOwner);
    VerifyPGPPublicKeyReturn verifyPGPPublicKeyReturn = new VerifyPGPPublicKeyReturn("verification saved", new VerificationGeneralView(verification), new PGPPublicKeyGeneralView(pgpPublicKeyData), new UserProfileGeneralView(keyOwner));
    LOG.warning("verifyPGPPublicKeyReturn" + new Gson().toJson(verifyPGPPublicKeyReturn));
    /* Send an email to key owner: */
    final Queue queue = QueueFactory.getDefaultQueue();
    queue.add(TaskOptions.Builder.withUrl("/_ah/SendGridServlet").param(// 1
    "email", keyOwner.getEmail().getEmail()).param(// 2
    "emailCC", cryptonomicaUser.getEmail().getEmail()).param(// 3
    "messageSubject", "Your public key " + pgpPublicKeyData.getKeyID() + " verified").param(// 4
    "messageText", "CONGRATULATION! \n\n" + keyOwner.getFirstName() + " " + keyOwner.getLastName() + ", \n\n" + "Your key: " + pgpPublicKeyData.getFingerprint() + " was verified on Cryptonomica server!\n\n" + "Information about key verification: \n\n" + "Verified by: " + cryptonomicaUser.getFirstName() + " " + cryptonomicaUser.getLastName() + "\n\n" + "Verified on: " + verification.getVerifiedOn() + "\n\n" + "Verification based on following document(s): " + verification.getBasedOnDocument() + "\n\n" + "Verification info: " + verification.getVerificationInfo() + "\n\n" + "\n\n" + "Best regards, \n\n" + "Cryptonomica team\n\n" + "if you think it's wrong or it is an error, please write to admin@cryptonomica.net \n"));
    return verifyPGPPublicKeyReturn;
}
Also used : ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) Queue(com.google.appengine.api.taskqueue.Queue) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 43 with ApiMethod

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

the class UserdataEndpoint method addReservedSession.

/**
 * Add a reserved session for the specified user. If the session is already in the user's feed,
 * it will be annotated with status=RESERVED.
 *
 * @param user         Service account making the request (injected by Endpoints)
 * @param userId       User ID of user that reserved a session.
 * @param sessionId    Session ID to mark as reserved.
 * @param timestampUTC The time (in millis, UTC) when the user performed this action. May be
 *                     different than the time this method is called if offline sync is
 *                     implemented. MUST BE ACCURATE - COMPENSATE FOR CLOCK DRIFT!
 * @return The list of reserved sessions for the user
 */
@ApiMethod(name = "addReservedSession", path = "reservations", httpMethod = ApiMethod.HttpMethod.PUT, clientIds = { Ids.SERVICE_ACCOUNT_CLIENT_ID })
public Map<String, ReservedSession> addReservedSession(User user, @Named("userId") String userId, @Named("sessionId") String sessionId, @Named("timestampUTC") long timestampUTC) throws UnauthorizedException {
    UserData data = getUser(user, userId);
    ReservedSession s = new ReservedSession(sessionId, Status.RESERVED, timestampUTC);
    data.reservedSessions.put(sessionId, s);
    save(data);
    // notify user's clients of reservation change change
    new GCMPing().notifyUserSync(data.userId);
    return data.reservedSessions;
}
Also used : GCMPing(com.google.samples.apps.iosched.server.schedule.server.GCMPing) ReservedSession(com.google.samples.apps.iosched.server.userdata.db.ReservedSession) UserData(com.google.samples.apps.iosched.server.userdata.db.UserData) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 44 with ApiMethod

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

the class UserdataEndpoint method removeReservedSession.

/**
 * Remove a reserved session for the specified user. The session will still be
 * attached to the user's feed, but will be annotated with status=DELETED.
 *
 * @param user         Service account making the request (injected by Endpoints)
 * @param userId       User ID of user that reserved a session.
 * @param sessionId    Session ID to mark as not reserved.
 * @param timestampUTC The time (in millis, UTC) when the user performed this action. May be
 *                     different than the time this method is called if offline sync is
 *                     implemented. MUST BE ACCURATE - COMPENSATE FOR CLOCK DRIFT!
 */
@ApiMethod(name = "removeReservedSession", path = "reservations", httpMethod = ApiMethod.HttpMethod.DELETE, clientIds = { Ids.SERVICE_ACCOUNT_CLIENT_ID })
public void removeReservedSession(User user, @Named("userId") String userId, @Named("sessionId") String sessionId, @Named("timestampUTC") long timestampUTC) throws UnauthorizedException {
    UserData data = getUser(user, userId);
    ReservedSession s = new ReservedSession(sessionId, Status.DELETED, timestampUTC);
    data.reservedSessions.put(sessionId, s);
    save(data);
    // notify user's clients of reservation change change
    new GCMPing().notifyUserSync(data.userId);
}
Also used : GCMPing(com.google.samples.apps.iosched.server.schedule.server.GCMPing) ReservedSession(com.google.samples.apps.iosched.server.userdata.db.ReservedSession) UserData(com.google.samples.apps.iosched.server.userdata.db.UserData) ApiMethod(com.google.api.server.spi.config.ApiMethod)

Example 45 with ApiMethod

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

the class UserdataEndpoint method addReviewedSession.

/**
 * Mark a session as reviewed for the current user. This can not be unset.
 *
 * @param user      Current user (injected by Endpoints)
 * @param sessionId Session ID to mark as reviewed.
 * @return The list of reviewed sessions for the user (as an array of Strings)
 */
@ApiMethod(name = "addReviewedSession", path = "reviewed", httpMethod = ApiMethod.HttpMethod.PUT)
public Object[] addReviewedSession(User user, @Named("sessionId") String sessionId) throws UnauthorizedException {
    UserData data = getUser(user);
    data.reviewedSessions.add(sessionId);
    save(data);
    return data.reviewedSessions.toArray();
}
Also used : UserData(com.google.samples.apps.iosched.server.userdata.db.UserData) 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