mirror of
https://github.com/logto-io/logto.git
synced 2025-01-13 21:30:30 -05:00
29 lines
748 B
TypeScript
29 lines
748 B
TypeScript
|
import assert from 'assert';
|
||
|
import RequestError from '@/errors/RequestError';
|
||
|
import { RequestErrorBody } from '@logto/schemas';
|
||
|
import { Middleware } from 'koa';
|
||
|
|
||
|
const bearerToken = 'Bearer';
|
||
|
|
||
|
export default function koaAuth<StateT, ContextT>(): Middleware<
|
||
|
StateT,
|
||
|
ContextT,
|
||
|
RequestErrorBody
|
||
|
> {
|
||
|
return async (ctx, next) => {
|
||
|
const { authorization } = ctx.request.headers;
|
||
|
assert(
|
||
|
authorization,
|
||
|
new RequestError({ code: 'auth.authorization_header_missing', status: 401 })
|
||
|
);
|
||
|
assert(
|
||
|
authorization.startsWith(bearerToken),
|
||
|
new RequestError(
|
||
|
{ code: 'auth.authorization_type_not_supported', status: 401 },
|
||
|
{ supportedTypes: [bearerToken] }
|
||
|
)
|
||
|
);
|
||
|
return next();
|
||
|
};
|
||
|
}
|