Skip to content
This repository has been archived by the owner on Dec 14, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1386 from CoderDojo/error-notify
Browse files Browse the repository at this point in the history
Oauth flow error handling
  • Loading branch information
Jeddf authored Jan 20, 2020
2 parents 0eadc7b + b08c942 commit 77d783e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
4 changes: 4 additions & 0 deletions web/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ module.exports = {
},

hapi: {
debug:
process.env.NODE_ENV === 'development'
? { log: ['error'], request: ['error'] }
: false,
connections: {
routes: {
security: {
Expand Down
64 changes: 51 additions & 13 deletions web/controllers/rpi/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const _ = require('lodash');
// eslint-disable-next-line import/no-extraneous-dependencies
const Boom = require('boom');
const { URLSearchParams } = require('url');
const {
getRedirectUri,
getRegisterRedirectUri,
Expand All @@ -11,6 +12,13 @@ const {
rpiZenAccountPassword,
} = require('../../lib/rpi-auth');

const oauthErrorMessage = 'Raspberry Pi Authentication Failed';

function getErrorRedirectUrl(message = oauthErrorMessage) {
const errorUrlQueryParams = new URLSearchParams({ error: message });
return `/?${errorUrlQueryParams}`;
}

function handleRPILogin(request, reply) {
const session = request.state['seneca-login'];
if (session && session.token) {
Expand Down Expand Up @@ -70,8 +78,11 @@ function getZenRegisterPayload(decodedIdToken) {

function handleCb(request, reply) {
if (request.query.error) {
request.log(['error', 'rpi', 'callback'], request.query);
return reply(Boom.badImplementation('callback error'));
request.log(['error', '40x'], request.query);
return reply.redirect(
// TODO: use generic user friendly error
getErrorRedirectUrl(`rpi callback error: ${request.query.error}`)
);
}

const login = (email, idToken) => {
Expand All @@ -84,8 +95,18 @@ function handleCb(request, reply) {
},
(err, res) => {
if (err) {
// TODO: Graceful error display
return reply(Boom.badImplementation(err));
request.log(['error', '50x'], err);
// TODO: use generic user friendly error
return reply.redirect(
getErrorRedirectUrl('Zen Login Failed - Seneca error.')
);
}
if (!res.login || !res.login.token) {
request.log(['error', '50x'], 'Zen Login Failed - No token.');
// TODO: use generic user friendly error
return reply.redirect(
getErrorRedirectUrl('Zen Login Failed - No token.')
);
}
request.cookieAuth.set({
token: res.login.token,
Expand All @@ -107,8 +128,14 @@ function handleCb(request, reply) {
profileId: rpiProfile.uuid,
},
(err, resp) => {
if (err) {
request.log(['error', '50x'], err);
// TODO: use generic user friendly error
return reply.redirect(
getErrorRedirectUrl('Get Profile User Failed - Seneca error.')
);
}
if (resp.email) {
// TODO: update email if not matching
return login(resp.email, idToken);
} else {
const zenRegisterPayload = getZenRegisterPayload(rpiProfile);
Expand All @@ -119,14 +146,20 @@ function handleCb(request, reply) {
);
return request.seneca.act(msg, (err, resp) => {
if (err) {
// TODO: Graceful error display
return reply(Boom.badImplementation(err));
request.log(['error', '50x'], err);
// TODO: use generic user friendly error
return reply.redirect(
getErrorRedirectUrl('Zen Registration Failed - Seneca error.')
);
}
if (!resp.user) {
// TODO: Graceful error display
// Observed error reason: nick is already used
return reply(
Boom.badImplementation('No user on registerResponse')
request.log(
['error', '50x'],
'Zen Registration Failed - No user.'
);
return reply.redirect(
// TODO: use generic user friendly error
getErrorRedirectUrl('Zen Registration Failed - No user.')
);
}
return login(resp.user.email, idToken);
Expand All @@ -136,8 +169,13 @@ function handleCb(request, reply) {
);
})
.catch(error => {
// TODO: Graceful error display
return reply(Boom.badImplementation(error));
request.log(['error', '40x'], error.data.payload);
return reply.redirect(
// TODO: use generic user friendly error
getErrorRedirectUrl(
`rpi id token error: ${error.data.payload.error_description}`
)
);
});
}

Expand Down

0 comments on commit 77d783e

Please sign in to comment.