-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Motivation ---------- This fixes #1503. This is necessary before I can properly implement role based authorization (for the admins). How to test ----------- 1. CI should be green
- Loading branch information
1 parent
e05a575
commit a2a71f7
Showing
21 changed files
with
456 additions
and
387 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,7 @@ | ||
import { createRemoteJWKSet } from 'jose' | ||
import { AuthChecker } from 'type-graphql' | ||
|
||
import { CONFIG } from '#config/config' | ||
import { EVENT_CREATE_USER } from '#src/event/Events' | ||
import { Context } from '#src/server/context' | ||
import { Context } from '#src/context' | ||
|
||
import { jwtVerify } from './jwtVerify' | ||
|
||
import type { prisma as Prisma, UserWithProfile } from '#src/prisma' | ||
|
||
export interface CustomJwtPayload { | ||
nickname: string | ||
name: string | ||
} | ||
|
||
const JWKS = createRemoteJWKSet(new URL(CONFIG.JWKS_URI)) | ||
|
||
export const authChecker: AuthChecker<Context> = async ({ context }) => { | ||
const { token, dataSources } = context | ||
const { prisma } = dataSources | ||
|
||
if (!token) return false | ||
|
||
let payload: CustomJwtPayload | ||
try { | ||
const decoded = await jwtVerify<CustomJwtPayload>(token, JWKS) | ||
payload = decoded.payload | ||
} catch (err) { | ||
return false | ||
} | ||
|
||
if (payload) { | ||
const { nickname, name } = payload | ||
const user = await contextUser(prisma)(nickname, name) | ||
context.user = user | ||
return true | ||
} | ||
|
||
return false | ||
export const authChecker: AuthChecker<Context> = ({ context }) => { | ||
return !!context.user | ||
} | ||
|
||
const contextUser = | ||
(prisma: typeof Prisma) => | ||
async (username: string, name: string): Promise<UserWithProfile> => { | ||
let user: UserWithProfile | null = await prisma.user.findUnique({ | ||
where: { | ||
username, | ||
}, | ||
include: { | ||
meeting: true, | ||
userDetail: true, | ||
socialMedia: true, | ||
}, | ||
}) | ||
if (user) return user | ||
user = await prisma.user.create({ | ||
data: { | ||
username, | ||
name, | ||
}, | ||
include: { | ||
meeting: true, | ||
userDetail: true, | ||
socialMedia: true, | ||
}, | ||
}) | ||
await EVENT_CREATE_USER(user.id) | ||
return user | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { jwtVerify } from 'jose' | ||
|
||
import { context } from './context' | ||
import { findOrCreateUser } from './findOrCreateUser' | ||
|
||
import type { CustomJwtPayload } from './context' | ||
|
||
jest.mock('./findOrCreateUser') | ||
jest.mock('jose') | ||
|
||
const mockedFindOrCreateUser = jest.mocked(findOrCreateUser) | ||
const mockedJwtVerify = jest.mocked(jwtVerify<CustomJwtPayload>) | ||
|
||
describe('context', () => { | ||
describe('if prisma client throws an error, e.g. because of pending migrations', () => { | ||
beforeEach(() => { | ||
mockedFindOrCreateUser.mockRejectedValue('Ouch!') | ||
const jwtVerifyPayload = { payload: { nickname: 'nickname', name: 'name' } } | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument,@typescript-eslint/no-explicit-any | ||
mockedJwtVerify.mockResolvedValue(jwtVerifyPayload as any) | ||
}) | ||
|
||
it('resolves to "INTERNAL_SERVER_ERROR" instead of "UNAUTHENTICATED"', async () => { | ||
const contextArgs = [{ req: { headers: { authorization: 'Bearer foobar' } } }] as Parameters< | ||
typeof context | ||
> | ||
await expect(context(...contextArgs)).rejects.toBe('Ouch!') | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.