Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

login-account-proxy: Better handle duplicate email change requests #23

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions apps/login-account-proxy/src/pages/api/public/submit.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import axios from 'axios';
import createHttpError from 'http-errors';
import { apiRoute } from '../../../lib/api/apiRoute';
import env from '../../../lib/api/env';
import { slackAlert } from '../../../lib/api/slackAlert';

export type SubmitRequest = {
oldEmail: string,
oldEmail?: string,
newEmail: string,
password: string,
secret: string,
Expand Down Expand Up @@ -43,6 +45,8 @@ export default apiRoute(async (
return;
}

const isChangingEmail = data.oldEmail && data.oldEmail !== data.newEmail;

const clientCredentialsGrantParams = new URLSearchParams();
clientCredentialsGrantParams.append('grant_type', 'client_credentials');
clientCredentialsGrantParams.append('client_id', 'login-account-proxy');
Expand All @@ -56,24 +60,25 @@ export default apiRoute(async (
const potentialUsers = (await axios.get<{ id: string, email: string }[]>(`https://login.bluedot.org/admin/realms/customers/users?email=${encodeURIComponent(data.oldEmail || data.newEmail)}&exact=true`, { headers })).data;

if (potentialUsers.length > 1) {
throw new Error(`Found more than one user for email: ${data.newEmail}`);
throw new Error(`Found more than one user for email: ${data.oldEmail || data.newEmail}`);
}

if (potentialUsers.length === 1) {
if (data.oldEmail && data.oldEmail !== data.newEmail) {
await axios.put(`https://login.bluedot.org/admin/realms/customers/users/${potentialUsers[0]!.id}`, {
...potentialUsers[0],
email: data.newEmail,
username: data.newEmail,
}, { headers });
// No account exists, and we expect one to
if (isChangingEmail && potentialUsers.length === 0) {
const potentialNewUsers = (await axios.get<{ id: string, email: string }[]>(`https://login.bluedot.org/admin/realms/customers/users?email=${encodeURIComponent(data.newEmail)}&exact=true`, { headers })).data;

if (potentialNewUsers.length === 0) {
throw new createHttpError.BadRequest(`Tried to change email, but cannot find existing accounts in Keycloak for email ${data.oldEmail} or ${data.newEmail}`);
}

await axios.put(`https://login.bluedot.org/admin/realms/customers/users/${potentialUsers[0]!.id}/reset-password`, {
type: 'password',
value: data.password,
}, { headers });
if (potentialNewUsers.length >= 1) {
slackAlert([`User tried to change email from ${data.oldEmail} to ${data.newEmail}, but only found account for new email. Ignoring this request, because this usually indicates it previously succeeded and they clicked the button in Bubble twice in quick succession.`]);
res.status(200).json({ type: 'success' });
return;
}
}

// No account exists, and we don't expect one to
if (potentialUsers.length === 0) {
await axios.post('https://login.bluedot.org/admin/realms/customers/users', {
enabled: true,
Expand All @@ -85,5 +90,21 @@ export default apiRoute(async (
}, { headers });
}

// An account exists
if (potentialUsers.length === 1) {
if (isChangingEmail) {
await axios.put(`https://login.bluedot.org/admin/realms/customers/users/${potentialUsers[0]!.id}`, {
...potentialUsers[0],
email: data.newEmail,
username: data.newEmail,
}, { headers });
}

await axios.put(`https://login.bluedot.org/admin/realms/customers/users/${potentialUsers[0]!.id}/reset-password`, {
type: 'password',
value: data.password,
}, { headers });
}

res.status(200).json({ type: 'success' });
}, 'insecure_no_auth');
Loading