This repository has been archived by the owner on May 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2709 from ozten/issue-2678-complete_transition
First stab at updating cert_key and adding complete_transition wsapis
- Loading branch information
Showing
9 changed files
with
255 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
const | ||
db = require('../db.js'), | ||
httputils = require('../httputils'), | ||
logger = require('../logging.js').logger, | ||
primary = require('../primary.js'), | ||
wsapi = require('../wsapi.js'); | ||
|
||
// Updates DB state regarding how the user has last used an email | ||
// Needed for when an email address goes from Secondary to Primary. | ||
// If an email address goes from Primary to Secondary, the update | ||
// to the db is made in the cert_key wsapi call. | ||
// complete_transition wsapi exists, only because there is no good | ||
// API call to piggyback on. If we ever add another POST, we can | ||
// push this behavior into that new wsapi. | ||
|
||
exports.method = 'post'; | ||
exports.writes_db = true; | ||
exports.authed = 'assertion'; | ||
exports.args = { | ||
'email': 'email' | ||
}; | ||
exports.i18n = false; | ||
|
||
exports.process = function (req, res) { | ||
var email = req.params.email; | ||
db.userOwnsEmail(req.session.userid, email, function(err, owned) { | ||
if (err) return wsapi.databaseDown(res, err); | ||
// not same account? big fat error | ||
if (!owned) return httputils.badRequest(res, "that email does not belong to you"); | ||
var domain = primary.emailRegex.exec(email)[1]; | ||
|
||
primary.checkSupport(domain, function (err, r) { | ||
var notPrimary = false; | ||
if (err) { | ||
logger.error('"' + domain + '" primary support is misconfigured, falling back to secondary: ' + err); | ||
notPrimary = true; | ||
} else if (r && r.urls) { | ||
db.updateEmailLastUsedAs(email, 'primary', function (err) { | ||
if (err) return httputils.serverError(res, "Unable to update database"); | ||
return res.json({success: true}); | ||
}); | ||
} else { | ||
notPrimary = true; | ||
} | ||
if (notPrimary) return httputils.badRequest(res, "email is not a primary"); | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
#!/usr/bin/env node | ||
|
||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
require('./lib/test_env.js'); | ||
|
||
const assert = require('assert'), | ||
db = require('../lib/db.js'), | ||
email = require('../lib/email.js'), | ||
jwcrypto = require('jwcrypto'), | ||
primary = require('./lib/primary.js'), | ||
start_stop = require('./lib/start-stop.js'), | ||
vows = require('vows'), | ||
wsapi = require('./lib/wsapi.js'); | ||
|
||
var suite = vows.describe('forgotten-email'); | ||
|
||
// algs | ||
require("jwcrypto/lib/algs/ds"); | ||
require("jwcrypto/lib/algs/rs"); | ||
|
||
start_stop.addStartupBatches(suite); | ||
|
||
// every time a new token is sent out, let's update the global | ||
// var 'token' | ||
var token; | ||
|
||
const TEST_DOMAIN = 'example.domain', | ||
TEST_EMAIL = 'testuser@' + TEST_DOMAIN, | ||
TEST_ORIGIN = 'http://127.0.0.1:10002'; | ||
|
||
var primaryUser = new primary({ | ||
email: TEST_EMAIL, | ||
domain: TEST_DOMAIN | ||
}); | ||
|
||
suite.addBatch({ | ||
"set things up": { | ||
topic: function() { | ||
primaryUser.setup(this.callback); | ||
}, | ||
"works": function() { | ||
// nothing to do here | ||
} | ||
} | ||
}); | ||
|
||
// now let's generate an assertion using this user | ||
suite.addBatch({ | ||
"generating an assertion": { | ||
topic: function() { | ||
primaryUser.getAssertion(TEST_ORIGIN, this.callback); | ||
}, | ||
"succeeds": function(err, r) { | ||
assert.isString(r); | ||
}, | ||
"and logging in with the assertion succeeds": { | ||
topic: function(err, assertion) { | ||
wsapi.post('/wsapi/auth_with_assertion', { | ||
assertion: assertion, | ||
ephemeral: true | ||
}).call(this); | ||
}, | ||
"works": function(err, r) { | ||
var resp = JSON.parse(r.body); | ||
assert.isObject(resp); | ||
assert.isTrue(resp.success); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
|
||
suite.addBatch({ | ||
"Mark a transition as having been seen": { | ||
topic: wsapi.post('/wsapi/complete_transition', { | ||
email: TEST_EMAIL | ||
}), | ||
"request successful": function(err, r) { | ||
assert.equal(r.code, 200); | ||
assert.strictEqual(true, JSON.parse(r.body).success); | ||
token = undefined; | ||
}, | ||
"db updated": { | ||
topic: function () { | ||
db.emailLastUsedAs(TEST_EMAIL, this.callback); | ||
}, | ||
"lastUseAs updated": function (err, lastUsedAs) { | ||
assert.isNull(err); | ||
assert.equal(lastUsedAs, 'primary'); | ||
} | ||
} | ||
} | ||
}); | ||
|
||
start_stop.addShutdownBatches(suite); | ||
|
||
// run or export the suite. | ||
if (process.argv[1] === __filename) suite.run(); | ||
else suite.export(module); |