From 8fdfa325cd6f15d52eab90b3ff3428cd1dd87738 Mon Sep 17 00:00:00 2001 From: rcpeters Date: Mon, 17 Apr 2017 16:10:34 -0700 Subject: [PATCH] move send email before authentication --- local_modules/smid-manager.js | 24 ++++++++-- server.js | 90 +++++++++++++++++------------------ 2 files changed, 64 insertions(+), 50 deletions(-) diff --git a/local_modules/smid-manager.js b/local_modules/smid-manager.js index cc9ce92..54b423d 100644 --- a/local_modules/smid-manager.js +++ b/local_modules/smid-manager.js @@ -80,8 +80,7 @@ var SmidManger = function(connectionStr) { * callback(err, [new smid object]) */ -SmidManger.prototype.createSmid = function(orcidRecord, callback) { - this.validateOrcidRecord(orcidRecord); +SmidManger.prototype.createSmid = function(callback) { var smidManger = this; var pubKey = randomstring.generate(8); var privKey = randomstring.generate() @@ -102,7 +101,7 @@ SmidManger.prototype.createSmid = function(orcidRecord, callback) { description: undefined, title: undefined, }, - owner: orcidRecord + owner: undefined }, private_key: privKey, // used for allowing edits public_key: pubKey, // used for shareing @@ -197,6 +196,25 @@ SmidManger.prototype.validateOrcidRecord = function(orcidRecord) { } }; +SmidManger.prototype.addOwnerOrcidRecord = function(orcidRecord, privateKey, callback) { + this.validateOrcidRecord(orcidRecord); + this._smidCol.findAndModify({ + query: { + private_key: privateKey + }, + update: { + $set: { + 'details.owner': orcidRecord + } + }, + new: true // this means return the updated object + }, + function(err, doc, lastErrorObject) { + if (err) callback(err, null); + else callback(null, doc); + }); +} + SmidManger.prototype.addOrcidRecord = function(orcidRecord, publicKey, callback) { this.validateOrcidRecord(orcidRecord); this._smidCol.findAndModify({ diff --git a/server.js b/server.js index d89937c..b9013d5 100644 --- a/server.js +++ b/server.js @@ -51,7 +51,7 @@ var CREATE_SMID_URI = '/create-smid-redirect'; var COLLECTION_DETAILS = '/:publicKey/details'; var COLLECTION_DETAILS_DOWNLOAD = '/:publicKey/details/download'; var COLLECTION_DETAILS_FORM = '/:publicKey/details/:privateKey/details/form'; -var COLLECTION_DETAILS_EMAIL = '/:publicKey/details/:privateKey/details/owner/email'; +var EMAIL_SMID = '/email-smid'; var ADD_ID_AUTHORIZE = '/add-id-authorize/:publicKey'; var ADD_ID_REDIRECT = '/add-id-redirect'; var ADD_ID_SUCCESS = '/:publicKey/orcid/:orcid'; @@ -89,12 +89,6 @@ app.get(CONFIG, function(req, res) { }); }); -//Create smid oauth sign into ORCID -app.get(CREATE_SMID_AUTHORIZE, function(req, res) { - var create_smid_authorization_uri = ooau.getAuthUrl(config.HOST + CREATE_SMID_URI); - res.redirect(create_smid_authorization_uri); -}); - app.get(CONFIG, function(req, res) { return res.status(200).json({ 'ORCID_URL': config.ORCID_URL @@ -122,7 +116,7 @@ app.get(CREATE_SMID_URI, function(req, res) { // Redeem code URL orcidLogger.log(date, token.name, token.orcid, req.query.state); console.log("creating smid for " + token.orcid); var orcidRecord = smidManger.createOrcidRecord(token.orcid, ooau.fullOrcid(token.orcid), token.name); - smidManger.createSmid(orcidRecord, function(err, doc) { + smidManger.addOwnerOrcidRecord(orcidRecord, req.query.state, function(err, doc) { if (err) res.send(err) else { var collection = JSON.parse(JSON.stringify(doc, null, 2)); @@ -183,49 +177,51 @@ app.put(COLLECTION_DETAILS_FORM, function(req, res) { }); }); -//Update collection details form fields -app.put(COLLECTION_DETAILS_EMAIL, function(req, res) { +// create and email smid +app.post(EMAIL_SMID, function(req, res) { var data = req.body; - console.log(data.email) mailgunPub.validate(data.email, function (error, body) { - if(body && body.is_valid){ - var mailData = { - from: 'No Reply ', - to: data.email, - subject: 'Share My iD links', - text: `Thanks for creating a ORCID iD collection.\n` - + `\n` - + `\n` - + `Administration Link\n` - + `Use this link to edit collection details: \n` - + `${config.HOST}/${req.params.publicKey}/edit/${req.params.privateKey}\n` - + `\n` - + `\n` - + `Share Link\n` - + `Share this link with anyone whose iD you want to collect, or display this page on a laptop/tablet at your event:` - + `${config.HOST}/${req.params.publicKey}` - + `\n` - + `\n` - + `Thanks,\n` - + `The Share My iD Team` - }; - mailgunPriv.messages().send(mailData, function (error, body) { - if (error != null) { - console.log("mailgun error:"); - console.log(error); - if (body != null && body.message != null && body.message.includes("Great job")) - res.status(200).json({'email': data.email}); // using test credentials - else - res.status(400).json({'error':error, 'body': body}) - } else { - console.log("mailgun body:"); - console.log(body); - res.status(200).json({'email': data.email}); - } + if(body && body.is_valid) { + console.log("email is valid"); + smidManger.createSmid(function(err, doc) { + var mailData = { + from: 'No Reply ', + to: data.email, + subject: 'Share My iD links', + text: `Thanks for creating a ORCID iD collection.\n` + + `\n` + + `\n` + + `Administration Link\n` + + `Use this link to edit collection details: \n` + + `${config.HOST}/${doc.public_key}/edit/${doc.private_key}\n` + + `\n` + + `\n` + + `Share Link\n` + + `Share this link with anyone whose iD you want to collect, or display this page on a laptop/tablet at your event:` + + `${config.HOST}/${doc.public_key}` + + `\n` + + `\n` + + `Thanks,\n` + + `The Share My iD Team` + }; + var create_smid_authorization_uri = ooau.getAuthUrl(config.HOST + CREATE_SMID_URI, doc.private_key); + mailgunPriv.messages().send(mailData, function (error, body) { + if (error != null) { + console.log("mailgun error:"); + console.log(error); + if (body != null && body.message != null && body.message.includes("Great job")) + res.status(200).json({'email': data.email, 'redirect': create_smid_authorization_uri }); // using test credentials + else + res.status(400).json({'error':error, 'body': body}); + } else { + console.log("mailgun body:"); + console.log(body); + res.status(200).json({'email': data.email}); + } + }); }); - // do something } else { - res.status(400).json({'error':error, 'body': body}); + res.status(400).json({'error':"Email failed to pass validation", 'body': body}); } }); });