- /session/destroy
- /recovery_email/resend_code
- /get_random_bytes
This commit is contained in:
Danny Coates 2013-08-19 13:16:13 -07:00
Родитель 99de5c1325
Коммит 796a7b124b
6 изменённых файлов: 125 добавлений и 6 удалений

Просмотреть файл

@ -241,6 +241,25 @@ Client.prototype.login = function (callback) {
}
}
Client.prototype.destroySession = function (callback) {
var p = P(null)
if (this.sessionToken) {
p = this.api.sessionDestroy(this.sessionToken)
.then(
function () {
this.sessionToken = null
return {}
}.bind(this)
)
}
if (callback) {
p.done(callback.bind(null, null), callback)
}
else {
return p
}
}
Client.prototype.verifyEmail = function (code, callback) {
var p = this.api.recoveryEmailVerifyCode(this.uid, code)
if (callback) {
@ -273,6 +292,21 @@ Client.prototype.emailStatus = function (callback) {
}
}
Client.prototype.requestVerifyEmail = function (callback) {
var o = this.sessionToken ? P(null) : this.login()
var p = o.then(
function () {
return this.api.recoveryEmailResendCode(this.sessionToken, this.email)
}.bind(this)
)
if (callback) {
p.done(callback.bind(null, null), callback)
}
else {
return p
}
}
Client.prototype.sign = function (publicKey, duration, callback) {
var o = this.sessionToken ? P(null) : this.login()
var p = o.then(

Просмотреть файл

@ -237,6 +237,23 @@ module.exports = function (P, tokens, RecoveryEmail, db, config, error) {
return P.all(methods)
}
Account.prototype.primaryRecoveryEmail = function () {
// TODO: this is not ideal. consider refactoring how
// recovery emails are indexed
return this.recoveryEmails()
.then(
function (emails) {
for (var i = 0; emails.length; i++) {
var email = emails[i]
if (email.primary) {
return email
}
}
return null
}
)
}
Account.prototype.deleteAllRecoveryEmails = function () {
var codes = Object.keys(this.recoveryEmailCodes)
var methods = []

Просмотреть файл

@ -5,6 +5,7 @@
module.exports = function (crypto, P, db, mailer) {
function RecoveryEmail() {
this.email = null
this.code = null
this.uid = null
this.verified = false
@ -31,6 +32,7 @@ module.exports = function (crypto, P, db, mailer) {
if (!object) return null
if (object.value) object = object.value
var rm = new RecoveryEmail()
rm.email = object.email
rm.code = object.code
rm.uid = object.uid
rm.verified = object.verified

Просмотреть файл

@ -185,17 +185,16 @@ module.exports = function (crypto, uuid, isA, error, Account, RecoveryEmail) {
handler: function (request) {
var sessionToken = request.auth.credentials
Account
.get(sessionToken.id)
.then(
.get(sessionToken.uid)
.done(
function (account) {
RecoveryEmail
.get(account.email)
return account.primaryRecoveryEmail()
.then(
function (rm) {
return rm.sendVerifyCode()
}
)
.done(
.then(
function () {
request.reply({})
},

Просмотреть файл

@ -188,6 +188,60 @@ function main() {
}
)
test(
'session destroy',
function (t) {
var email = 'test4@example.com'
var password = 'foobar'
var client = null
var sessionToken = null
Client.create(config.public_url, email, password)
.then(
function (x) {
client = x
return client.devices()
}
)
.then(
function () {
sessionToken = client.sessionToken
return client.destroySession()
}
)
.then(
function () {
t.equal(client.sessionToken, null, 'session token deleted')
client.sessionToken = sessionToken
return client.devices()
}
)
.done(
function (devices) {
t.fail('got devices with destroyed session')
t.end()
},
function (err) {
t.equal(err.errno, 401, 'session is invalid')
t.end()
}
)
}
)
test(
'random bytes',
function (t) {
var client = new Client(config.public_url)
client.api.getRandomBytes()
.done(
function (x) {
t.equal(x.data.length, 64)
t.end()
}
)
}
)
test(
'teardown',
function (t) {

Просмотреть файл

@ -17,6 +17,7 @@ function main() {
var email = 'verification@example.com'
var password = 'allyourbasearebelongtous'
var client = null
var verifyCode = null
Client.create(config.public_url, email, password)
.then(
function (x) {
@ -49,7 +50,19 @@ function main() {
.then(waitForCode)
.then(
function (code) {
return client.verifyEmail(code)
verifyCode = code
return client.requestVerifyEmail()
}
)
.then(waitForCode)
.then(
function (code) {
t.equal(code, verifyCode, 'verify codes are the same')
}
)
.then(
function () {
return client.verifyEmail(verifyCode)
}
)
.then(