{"size": 817, "ext": "js", "max_stars_repo_path": "wrap/koa2-session.js", "max_stars_repo_name": "Handfish/cas-client", "max_stars_repo_stars_event_min_datetime": "2019-12-04T16:20:48.000Z", "max_stars_repo_stars_event_max_datetime": "2021-12-01T15:57:55.000Z", "max_issues_repo_path": "wrap/koa2-session.js", "max_issues_repo_name": "Handfish/cas-client", "max_issues_repo_issues_event_min_datetime": "2019-12-20T21:04:11.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-12T05:25:20.000Z", "max_forks_repo_path": "wrap/koa2-session.js", "max_forks_repo_name": "Handfish/cas-client", "max_forks_repo_forks_event_min_datetime": "2021-01-05T00:56:19.000Z", "max_forks_repo_forks_event_max_datetime": "2021-09-17T15:50:47.000Z"}
{"max_stars_count": 8, "max_issues_count": 5, "max_forks_count": 2, "avg_line_length": 21.5, "max_line_length": 100, "alphanum_fraction": 0.641370869}
const httpCasClient = require('../');

module.exports = function createCasClientKoaSessionMiddleware(...options) {
	const handler = httpCasClient(...options);

	return async function casClientMiddleware(ctx, next) {
		const { session, request } = ctx;
		const { body } = request;

		if (!session) {
			throw new Error('Cas client for koa2 with koa-session need to be put behind session middleware');
		}

		const hooks = {
			getter() {
				return session.st;
			},
			created(ticketId) {
				session.st = ticketId;
			},
			destroy() {
				delete ctx.session;
			}
		};

		if (body) {
			hooks.bodyParser = () => body;
		}

		if(await handler(ctx.req, ctx.res, hooks)) {
			ctx.principal = ctx.request.principal = ctx.req.principal;
			ctx.ticket = ctx.request.ticket = ctx.req.ticket;

			return next();
		}
	};
};
