Delete mail auth token after usage

This commit is contained in:
Jonas Lochmann
2021-12-27 01:00:00 +01:00
parent 9f2ea6e741
commit 3715581d10
6 changed files with 34 additions and 13 deletions
+25 -4
View File
@@ -1,6 +1,6 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 Jonas Lochmann
* Copyright (C) 2019 - 2021 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -31,7 +31,11 @@ export const createAuthTokenByMailAddress = async ({ mail, database, transaction
return token
}
export const getMailByAuthToken = async ({ mailAuthToken, database, transaction }: { mailAuthToken: string, database: Database, transaction: Transaction }) => {
export const getMailByAuthToken = async ({
mailAuthToken, database, transaction, invalidate
}: {
mailAuthToken: string, database: Database, transaction: Transaction, invalidate: boolean
}) => {
const entry = await database.authtoken.findOne({
where: {
token: mailAuthToken
@@ -40,14 +44,31 @@ export const getMailByAuthToken = async ({ mailAuthToken, database, transaction
})
if (entry) {
if (invalidate) {
const rowCounter = await database.authtoken.destroy({
where: {
token: mailAuthToken
},
transaction
})
if (rowCounter !== 1) {
return null
}
}
return entry.mail
} else {
return null
}
}
export const requireMailByAuthToken = async ({ mailAuthToken, database, transaction }: { mailAuthToken: string, database: Database, transaction: Transaction }) => {
const mail = await getMailByAuthToken({ mailAuthToken, database, transaction })
export const requireMailByAuthToken = async ({
mailAuthToken, database, transaction, invalidate
}: {
mailAuthToken: string, database: Database, transaction: Transaction, invalidate: boolean
}) => {
const mail = await getMailByAuthToken({ mailAuthToken, database, transaction, invalidate })
if (!mail) {
throw new Unauthorized()
+2 -2
View File
@@ -1,6 +1,6 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 Jonas Lochmann
* Copyright (C) 2019 - 2021 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -37,7 +37,7 @@ export const createFamily = async ({ database, mailAuthToken, firstParentDevice,
}) => {
return database.transaction(async (transaction) => {
const now = Date.now().toString(10)
const mail = await requireMailByAuthToken({ database, mailAuthToken, transaction })
const mail = await requireMailByAuthToken({ database, mailAuthToken, transaction, invalidate: true })
// ensure that no family was created for this mail yet
const exisitngUserEntry = await database.user.findOne({
@@ -1,6 +1,6 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 Jonas Lochmann
* Copyright (C) 2019 - 2021 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -43,7 +43,7 @@ const getStatusByMailAddress = async ({
export const getStatusByMailToken = async ({
mailAuthToken, database, transaction
}: { mailAuthToken: string, database: Database, transaction: Transaction }) => {
const mail = await requireMailByAuthToken({ mailAuthToken, database, transaction })
const mail = await requireMailByAuthToken({ mailAuthToken, database, transaction, invalidate: false })
const status = await getStatusByMailAddress({ mail, database, transaction })
return { mail, status }
+1 -1
View File
@@ -45,7 +45,7 @@ export const linkMailAddress = async ({ mailAuthToken, deviceAuthToken, parentUs
const familyId = deviceEntry.familyId
const mailAddress = await requireMailByAuthToken({ mailAuthToken, database, transaction })
const mailAddress = await requireMailByAuthToken({ mailAuthToken, database, transaction, invalidate: true })
const exisitingUser = await database.user.findOne({
where: {
@@ -1,6 +1,6 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 Jonas Lochmann
* Copyright (C) 2019 - 2021 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -31,7 +31,7 @@ export const recoverParentPassword = async ({ database, websocket, password, mai
// no transaction here because this is directly called from an API endpoint
}) => {
await database.transaction(async (transaction) => {
const mail = await requireMailByAuthToken({ mailAuthToken, database, transaction })
const mail = await requireMailByAuthToken({ mailAuthToken, database, transaction, invalidate: true })
// update the user entry
const userEntry = await database.user.findOne({
+2 -2
View File
@@ -1,6 +1,6 @@
/*
* server component for the TimeLimit App
* Copyright (C) 2019 - 2020 Jonas Lochmann
* Copyright (C) 2019 - 2021 Jonas Lochmann
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
@@ -33,7 +33,7 @@ export const signInIntoFamily = async ({ database, mailAuthToken, newDeviceInfo,
// no transaction here because this is directly called from an API endpoint
}): Promise<{ deviceId: string; deviceAuthToken: string }> => {
return database.transaction(async (transaction) => {
const mail = await requireMailByAuthToken({ database, mailAuthToken, transaction })
const mail = await requireMailByAuthToken({ database, mailAuthToken, transaction, invalidate: true })
const userEntryUnsafe = await database.user.findOne({
where: {