-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support auth via dash cookies (#539)
- Loading branch information
1 parent
2ae4ea7
commit 2f33c29
Showing
9 changed files
with
264 additions
and
143 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,30 +1,54 @@ | ||
import { jwtVerify } from 'jose'; | ||
|
||
import { auth } from '../auth.js'; | ||
import type { ExtendedMiddleware } from '../../../types.js'; | ||
|
||
export const authenticate: ExtendedMiddleware = async (ctx, next) => { | ||
const { headers } = ctx.request; | ||
type SessionCookiePayload = { | ||
id?: string; | ||
role?: string; | ||
app_access?: number; | ||
admin_access?: number; | ||
}; | ||
|
||
if (headers && headers.authorization) { | ||
const parts = headers.authorization.split(' '); | ||
export const authenticate = (options: AuthenticateOptions): ExtendedMiddleware => { | ||
const sessionKey = Buffer.from(options.session.cookieSecret); | ||
|
||
if (parts.length !== 2 || parts[0] !== 'Bearer') { | ||
ctx.status = 401; | ||
return; | ||
} | ||
return async (ctx, next) => { | ||
const authorization = ctx.headers.authorization; | ||
const sessionCookie = ctx.cookies.get(options.session.cookieName); | ||
|
||
const token = parts[1]!; | ||
const origin = ctx.get('Origin'); | ||
const userId = await auth.validate(token, origin); | ||
if (authorization) { | ||
const parts = authorization.split(' '); | ||
|
||
if (!userId) { | ||
ctx.status = 401; | ||
return; | ||
} | ||
if (parts.length !== 2 || parts[0] !== 'Bearer') { | ||
ctx.status = 401; | ||
return; | ||
} | ||
|
||
const token = parts[1]!; | ||
const origin = ctx.get('Origin'); | ||
const userId = await auth.validate(token, origin); | ||
|
||
ctx.state.userId = userId; | ||
} | ||
if (!userId) { | ||
ctx.status = 401; | ||
return; | ||
} | ||
|
||
return next(); | ||
ctx.state.user = { id: userId, authMode: 'token' }; | ||
} else if (sessionCookie) { | ||
try { | ||
const result = await jwtVerify<SessionCookiePayload>(sessionCookie, sessionKey); | ||
|
||
if (result.payload.id && result.payload.app_access) { | ||
ctx.state.user = { id: result.payload.id, authMode: 'cookie' }; | ||
} | ||
} catch {} | ||
} | ||
|
||
return next(); | ||
}; | ||
}; | ||
|
||
export type AuthenticateState = { userId?: string }; | ||
export type AuthenticateOptions = { session: { cookieName: string, cookieSecret: string } }; | ||
export type AuthenticateState = { user?: { id: string, authMode: 'cookie' | 'token' } }; | ||
|
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
Oops, something went wrong.