|
|
@ -196,11 +196,13 @@ WalletService.getInstance = function(opts) { |
|
|
|
* Gets an instance of the server after authenticating the copayer. |
|
|
|
* @param {Object} opts |
|
|
|
* @param {string} opts.copayerId - The copayer id making the request. |
|
|
|
* @param {string} opts.message - The contents of the request to be signed. |
|
|
|
* @param {string} opts.signature - Signature of message to be verified using one of the copayer's requestPubKeys |
|
|
|
* @param {string} opts.message - (Optional) The contents of the request to be signed. Only needed if no session token is provided. |
|
|
|
* @param {string} opts.signature - (Optional) Signature of message to be verified using one of the copayer's requestPubKeys. Only needed if no session token is provided. |
|
|
|
* @param {string} opts.session - (Optional) A valid session token previously obtained using the #login method |
|
|
|
* @param {string} opts.clientVersion - A string that identifies the client issuing the request |
|
|
|
*/ |
|
|
|
WalletService.getInstanceWithAuth = function(opts, cb) { |
|
|
|
function withSignature(cb) { |
|
|
|
if (!checkRequired(opts, ['copayerId', 'message', 'signature'], cb)) return; |
|
|
|
|
|
|
|
var server; |
|
|
@ -224,11 +226,82 @@ WalletService.getInstanceWithAuth = function(opts, cb) { |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
function withSession(cb) { |
|
|
|
if (!checkRequired(opts, ['copayerId', 'session'], cb)) return; |
|
|
|
|
|
|
|
var server; |
|
|
|
try { |
|
|
|
server = WalletService.getInstance(opts); |
|
|
|
} catch (ex) { |
|
|
|
return cb(ex); |
|
|
|
} |
|
|
|
|
|
|
|
server.storage.getSession(opts.copayerId, function(err, s) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var isValid = s && s.id == opts.session && s.isValid(); |
|
|
|
if (!isValid) return cb(new ClientError(Errors.codes.NOT_AUTHORIZED, 'Session expired')); |
|
|
|
|
|
|
|
server.storage.fetchCopayerLookup(opts.copayerId, function(err, copayer) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!copayer) return cb(new ClientError(Errors.codes.NOT_AUTHORIZED, 'Copayer not found')); |
|
|
|
|
|
|
|
server.copayerId = opts.copayerId; |
|
|
|
server.walletId = copayer.walletId; |
|
|
|
return cb(null, server); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
var authFn = opts.session ? withSession : withSignature; |
|
|
|
return authFn(cb); |
|
|
|
}; |
|
|
|
|
|
|
|
WalletService.prototype._runLocked = function(cb, task) { |
|
|
|
$.checkState(this.walletId); |
|
|
|
this.lock.runLocked(this.walletId, cb, task); |
|
|
|
}; |
|
|
|
|
|
|
|
WalletService.prototype.login = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
var session; |
|
|
|
async.series([ |
|
|
|
|
|
|
|
function(next) { |
|
|
|
self.storage.getSession(self.copayerId, function(err, s) { |
|
|
|
if (err) return next(err); |
|
|
|
session = s; |
|
|
|
next(); |
|
|
|
}); |
|
|
|
}, |
|
|
|
function(next) { |
|
|
|
if (!session || !session.isValid()) { |
|
|
|
session = Model.Session.create({ |
|
|
|
copayerId: self.copayerId, |
|
|
|
walletId: self.walletId, |
|
|
|
}); |
|
|
|
} else { |
|
|
|
session.touch(); |
|
|
|
} |
|
|
|
next(); |
|
|
|
}, |
|
|
|
function(next) { |
|
|
|
self.storage.storeSession(session, next); |
|
|
|
}, |
|
|
|
], function(err) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!session) return cb(new Error('Could not get current session for this copayer')); |
|
|
|
|
|
|
|
return cb(null, session.id); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
WalletService.prototype.logout = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
self.storage.removeSession(self.copayerId, cb); |
|
|
|
}; |
|
|
|
|
|
|
|
/** |
|
|
|
* Creates a new wallet. |
|
|
|