diff --git a/lib/model/host.js b/lib/model/host.js index bd9474030..fc0a1ec90 100644 --- a/lib/model/host.js +++ b/lib/model/host.js @@ -124,6 +124,25 @@ Host.ensureHost = function(hostname, callback) { ); }; +Host.prototype.destroyHost = function(callback) { + + var host = this; + + Step( + function() { + Credentials.getForHost(URLMaker.hostname, host, this); + }, + function(err, cred) { + if (err) throw err; + cred.del(this); + }, + function(err) { + host.del(this); + }, + callback + ); +}; + Host.discover = function(hostname, callback) { var props = { diff --git a/routes/web.js b/routes/web.js index 045fd3320..1a67b7cce 100644 --- a/routes/web.js +++ b/routes/web.js @@ -250,10 +250,16 @@ var handleRemote = function(req, res, next) { host.getRequestToken(this); }, function(err, rt) { - if (err) { - next(err); - } else { + if (!err) { res.redirect(host.authorizeURL(rt)); + } else if (err.statusCode) { + if (host && err.statusCode === 400) { + req.log.warn("Invalid or old host credentials", err); + host.destroyHost(req.log.error); + } + next(new HTTPError(err.data, err.statusCode)); + } else { + next(err); } } );