@zhaochy/egg-oauth2-server
Version:
koa-oauth-server(node-oauth2-server) plugin for egg
26 lines (22 loc) • 831 B
JavaScript
;
module.exports = (options, app) => {
app.loggers.coreLogger.info('[egg-oauth2-server] starting with whiteList -> %j', options.whiteList);
return async (ctx, next) => {
const whiteList = ctx.app.config.oAuth2Server.whiteList;
ctx.logger.debug('authenticate URL %s, whiteList: %j', ctx.path, whiteList);
if (whiteList.includes(ctx.path)) {
ctx.logger.debug('[OAuth] - request in whiteList, skip authentication');
await next();
return;
}
const token = await ctx.app.oAuth2Server.execute('authenticate', ctx, options);
if (token) {
const jwt = ctx.app.jwt.decode(token.accessToken);
ctx.state.oauth = token;
ctx.state.jwt = jwt;
ctx.state.user = jwt.user;
ctx.logger.debug('[OAuth] - authentication passed');
await next();
}
}
};