-
Notifications
You must be signed in to change notification settings - Fork 2
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
refactor(backend): context hook #1684
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be nice to avoid the
construction in all resolvers that require a user. Can we somehow force the
type UserWithProfile
for calls that require authorization?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I will also have a look but this is out of scope of this PR, isn't it?
Maybe there is a typescript solution for this. Ideally
Authorized()
would mark thecontext.user
as not null.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Mogge I think we're out of luck here: microsoft/TypeScript#49229
I would suggest we use a simple helper method:
with:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would not raise the coverage of our branches
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we have a dummy unauthenticated user that is set if there is no authentication?