Skip to content

Commit

Permalink
Add impersonation guards into auth utils
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwofford committed Nov 19, 2024
1 parent 7665665 commit cd0386b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
19 changes: 2 additions & 17 deletions src/app/api/admin/impersonate/[slackId]/route.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,14 @@
import { getSelfPerson } from '@/app/utils/airtable'
import { redirect } from 'next/navigation'
import { NextRequest } from 'next/server'

import { HsSession, signAndSet } from '@/app/utils/auth'
import { impersonate } from '@/app/utils/auth'

export async function GET(
_request: NextRequest,
{ params }: { params: { slackId: string } },
) {
if (process.env.NODE_ENV === 'development') {
// only allow impersonation in development while testing
const { slackId } = params
// look for airtable user with this record
const person = await getSelfPerson(slackId)
const id = person.id
const email = person.fields.email

const session: HsSession = {
personId: id,
authType: 'impersonation',
slackId,
email,
}

await signAndSet(session)
impersonate(params.slackId)
}

redirect('/signpost')
Expand Down
23 changes: 22 additions & 1 deletion src/app/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,28 @@ async function hashSession(session: HsSession) {
return hashHex
}

export async function signAndSet(session: HsSession) {
export async function impersonate(slackId: string) {
// only allow impersonation in development while testing
if (process.env.NODE_ENV !== 'development') {
return
}

// look for airtable user with this record
const person = await getSelfPerson(slackId)
const id = person.id
const email = person.fields.email

const session: HsSession = {
personId: id,
authType: 'impersonation',
slackId,
email,
}

await signAndSet(session)
}

async function signAndSet(session: HsSession) {
session.sig = await hashSession(session)

cookies().set(sessionCookieName, JSON.stringify(session), {
Expand Down

0 comments on commit cd0386b

Please sign in to comment.