-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from ajhenry/ajhenry/auth
Add authentication middleware for routes and ALLOWED_HANDLES restrictions
- Loading branch information
Showing
14 changed files
with
217 additions
and
7 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
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,9 +1,10 @@ | ||
import * as trpcNext from '@trpc/server/adapters/next' | ||
import { createContext } from 'server/trpc' | ||
import { appRouter } from '../../../server/routers/_app' | ||
|
||
// export API handler | ||
// @see https://trpc.io/docs/server/adapters | ||
export default trpcNext.createNextApiHandler({ | ||
router: appRouter, | ||
createContext: () => ({}), | ||
createContext, | ||
}) |
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,25 @@ | ||
import { TRPCError } from '@trpc/server' | ||
import { personalOctokit } from 'bot/octokit' | ||
import { logger } from 'utils/logger' | ||
|
||
const middlewareLogger = logger.getSubLogger({ name: 'middleware' }) | ||
|
||
export const checkGitHubAuth = async (accessToken: string | undefined) => { | ||
if (!accessToken) { | ||
middlewareLogger.error('No access token provided') | ||
throw new TRPCError({ code: 'UNAUTHORIZED' }) | ||
} | ||
|
||
// Check validity of token | ||
const octokit = personalOctokit(accessToken) | ||
try { | ||
const user = await octokit.rest.users.getAuthenticated() | ||
if (!user) { | ||
middlewareLogger.error('No user found') | ||
throw new TRPCError({ code: 'UNAUTHORIZED' }) | ||
} | ||
} catch (error) { | ||
middlewareLogger.error('Error checking github auth', error) | ||
throw new TRPCError({ code: 'UNAUTHORIZED' }) | ||
} | ||
} |
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,13 @@ | ||
import { checkGitHubAuth } from 'server/lib/auth' | ||
import { Middleware } from 'server/trpc' | ||
|
||
export const verifyAuth: Middleware = async (opts) => { | ||
const { ctx } = opts | ||
|
||
// Verify valid github session | ||
checkGitHubAuth(ctx.session?.user?.accessToken) | ||
|
||
return opts.next({ | ||
ctx, | ||
}) | ||
} |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Simple health check endpoint | ||
import { procedure, router } from '../trpc' | ||
|
||
export const healthRouter = router({ | ||
// Queries | ||
ping: procedure.query(async () => { | ||
return 'pong' | ||
}), | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { DefaultSession } from 'next-auth' | ||
|
||
declare module 'next-auth' { | ||
interface Session { | ||
user: { | ||
accessToken: string | undefined | ||
} & DefaultSession['user'] | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { healthRouter } from '../../src/server/routers/health' | ||
import { Octomock } from '../octomock' | ||
import { createTestContext } from '../utils/auth' | ||
const om = new Octomock() | ||
|
||
jest.mock('../../src/bot/octokit', () => ({ | ||
personalOctokit: () => om.getOctokitImplementation(), | ||
})) | ||
|
||
describe('Git router', () => { | ||
beforeEach(() => { | ||
om.resetMocks() | ||
jest.resetAllMocks() | ||
}) | ||
|
||
it('should allow users that are authenticated', async () => { | ||
const caller = healthRouter.createCaller(createTestContext()) | ||
|
||
om.mockFunctions.rest.users.getAuthenticated.mockResolvedValue({ | ||
status: 200, | ||
data: { | ||
login: 'test-user', | ||
}, | ||
}) | ||
|
||
const res = await caller.ping() | ||
|
||
expect(res).toEqual('pong') | ||
|
||
expect(om.mockFunctions.rest.users.getAuthenticated).toHaveBeenCalledTimes( | ||
1, | ||
) | ||
}) | ||
|
||
it('should throw on invalid sessions', async () => { | ||
const caller = healthRouter.createCaller( | ||
createTestContext({ | ||
user: { | ||
name: 'fake-username', | ||
email: 'fake-email', | ||
image: 'fake-image', | ||
accessToken: 'bad-token', | ||
}, | ||
expires: new Date('2030-01-01').toISOString(), | ||
}), | ||
) | ||
|
||
om.mockFunctions.rest.users.getAuthenticated.mockResolvedValue({ | ||
status: 401, | ||
data: { | ||
message: 'Bad credentials', | ||
}, | ||
}) | ||
|
||
await caller.ping().catch((error) => { | ||
expect(error.code).toContain('UNAUTHORIZED') | ||
}) | ||
|
||
expect(om.mockFunctions.rest.users.getAuthenticated).toHaveBeenCalledTimes( | ||
1, | ||
) | ||
}) | ||
}) |
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Session } from 'next-auth' | ||
import { createContext } from '../../src/server/trpc' | ||
|
||
export const createTestContext = ( | ||
session?: Session, | ||
): Awaited<ReturnType<typeof createContext>> => { | ||
return { | ||
session: session ?? { | ||
user: { | ||
name: 'fake-username', | ||
email: 'fake-email', | ||
image: 'fake-image', | ||
accessToken: 'fake-access-token', | ||
}, | ||
expires: new Date('2030-01-01').toISOString(), | ||
}, | ||
} | ||
} |