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

Check whether ticket has expired on selectAccountByTicket #493

Merged
merged 3 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions src/routes/auth/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { asyncWrapper, selectAccount } from '@shared/helpers'
import { newJwtExpiry, createHasuraJwt } from '@shared/jwt'
import { setRefreshToken } from '@shared/cookies'
import { loginAnonymouslySchema, loginSchema, magicLinkLoginSchema } from '@shared/validation'
import { insertAccount } from '@shared/queries'
import { insertAccount, setNewTicket } from '@shared/queries'
import { request } from '@shared/request'
import { AccountData, UserData, Session } from '@shared/types'
import { emailClient } from '@shared/email'
Expand Down Expand Up @@ -77,7 +77,7 @@ async function loginAccount({ body, headers }: Request, res: Response): Promise<
return res.boom.badRequest('Account does not exist.')
}

const { id, mfa_enabled, password_hash, active, ticket, email } = account
const { id, mfa_enabled, password_hash, active, email } = account

if (typeof password === 'undefined') {
const refresh_token = await setRefreshToken(res, id, useCookie)
Expand Down Expand Up @@ -131,6 +131,16 @@ async function loginAccount({ body, headers }: Request, res: Response): Promise<
}

if (mfa_enabled) {
const ticket = uuidv4()
const ticket_expires_at = new Date(+new Date() + 60 * 60 * 1000)

// set new ticket
await request(setNewTicket, {
user_id: account.user.id,
ticket,
ticket_expires_at
})

return res.send({ mfa: true, ticket })
}

Expand Down
4 changes: 1 addition & 3 deletions src/routes/auth/register.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ async function registerAccount(req: Request, res: Response): Promise<unknown> {
let password_hash: string | null = null;

const ticket = uuidv4()
const now = new Date()
const ticket_expires_at = new Date()
ticket_expires_at.setTime(now.getTime() + 60 * 60 * 1000) // active for 60 minutes
const ticket_expires_at = new Date(+new Date() + 60 * 60 * 1000).toISOString() // active for 60 minutes

if (typeof password !== 'undefined') {
try {
Expand Down
5 changes: 4 additions & 1 deletion src/shared/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export const selectAccountByEmail = async (email: string): Promise<AccountData>
}

export const selectAccountByTicket = async (ticket: string): Promise<AccountData> => {
const hasuraData = await request<QueryAccountData>(selectAccountByTicketQuery, { ticket })
const hasuraData = await request<QueryAccountData>(selectAccountByTicketQuery, {
ticket,
now: new Date()
})
if (!hasuraData.auth_accounts[0]) throw new Error('Account does not exist.')
return hasuraData.auth_accounts[0]
}
Expand Down
4 changes: 2 additions & 2 deletions src/shared/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ export const selectAccountByEmail = gql`
`

export const selectAccountByTicket = gql`
query($ticket: uuid!) {
auth_accounts(where: { ticket: { _eq: $ticket } }) {
query($ticket: uuid!, $now: timestamptz!) {
auth_accounts(where: { _and: [{ ticket: { _eq: $ticket } }, { ticket_expires_at: { _gt: $now } }] }) {
komninoschatzipapas marked this conversation as resolved.
Show resolved Hide resolved
komninoschatzipapas marked this conversation as resolved.
Show resolved Hide resolved
...accountFragment
}
}
Expand Down