Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function init(options = {}) {
const passport = initPassport(options.PassportStrategy)
return app.use(generatePrometheusMiddleware(router, {
ignore: [/^\/repository/]
}))
.use(generateProblemMiddleware({
exposableErrorTypes: [
CHECK_ERROR_TYPE,
GITHUB_ERROR_TYPE,
REPO_ERROR_TYPE
]
}))
.use(morgan(morganFormat, {skip: morganSkip}))
.use(convert(session({store: store})))
.use(bodyParser())
.use(passport.initialize())
.use(passport.session())
.use(compress())
.use(router.routes())
.use(router.allowedMethods())
.use(conditional())
.use(etag())
.use(serve(
nconf.get('STATIC_DIR'), {
index: 'none',
maxage: 1.7 * 10 ** 8 // ~ 2 days
}))
.use(ensureModeMiddleware)
.use(renderStatic)
}
export default function middleware() {
return compose([
logger(),
helmet(), // reset HTTP headers (e.g. remove x-powered-by)
convert(cors()),
convert(bodyParser()),
convert(session()),
]);
}
const redisStore = koaRedis({
url: config.redisUrl
});
const app = new Koa();
app.keys = [config.secretKeyBase];
// not serve static when deploy
if(config.serveStatic){
app.use(convert(require('koa-static')(__dirname + '/../public')));
}
app.use(convert(session({
store: redisStore,
prefix: 'kails:sess:',
key: 'kails.sid'
})));
app.use(cacheMiddle());
app.use(bodyParser());
app.use(methodOverride((req, _res) => {
if (req.body && (typeof req.body === 'object') && ('_method' in req.body)) {
// look in urlencoded POST bodies and delete it
const method = req.body._method;
delete req.body._method;
return method;
}
}));
import etag from 'koa-etag';
import mount from 'koa-mount';
import serve from 'koa-static';
import convert from 'koa-convert';
import session from 'koa-generic-session';
import MongoStore from 'koa-generic-session-mongo';
app.use(compress());
app.use(bodyParser());
app.use(conditional());
app.use(etag());
app.use(mount('/public', serve(path.join(__dirname, '../public'), {
maxage: config.is.prod ? 1000 * 60 * 60 * 24 * 7 : 0,
})));
app.keys = [config.SESSIONID];
app.use(convert(session({
store: new MongoStore({
url: config.MONGO_URL,
}),
})));
// Passport
import passport from './lib/passport';
import loggedInMiddleware from './lib/logged-in-middleware';
app.use(passport.initialize());
app.use(passport.session());
app.use(loggedInMiddleware());
// Views
import views from 'koa-views';
app.use(views(path.join(__dirname, 'views/'), {
extension: 'pug',
// }
// --------------------- start -------------------------
// Instead of calling convert for all legacy middlewares
// just use the following to convert them all at once
const _use = app.use
app.use = x => _use.call(app, convert(x))
// The code above avoids writting the following
// app.use(convert(logger()))
// ---------------------- end --------------------------
app.use(helmet())
app.use(logger())
app.use(bodyParser())
app.use(session())
app.use(errorMiddleware())
// Mount static API documents generated by api-generator
app.use(mount('/docs', serve(`${process.cwd()}/docs`)))
// Using Passport for authentication
require('../config/passport')
app.use(passport.initialize())
app.use(passport.session())
// Using module wise routing
const modules1 = require('../src/modules/v1')
const modules2 = require('../src/modules/v2')
const common = require('../src/modules/common')
modules1(app)
modules2(app)
// just use the following to convert them all at once
const _use = app.use
app.use = x => _use.call(app, convert(x))
// The code above avoids writting the following
// app.use(convert(logger()))
// ---------------------- end --------------------------
mongoose.Promise = global.Promise
mongoose.connect(config.database)
app.use(helmet())
app.use(logger())
app.use(bodyParser())
app.use(session())
app.use(errorMiddleware())
// Mount static API documents generated by api-generator
app.use(mount('/docs', serve(`${process.cwd()}/docs`)))
// Using Passport for authentication
require('../config/passport')
app.use(passport.initialize())
app.use(passport.session())
// Using module wise routing
const modules1 = require('../src/modules/v1')
const modules2 = require('../src/modules/v2')
const common = require('../src/modules/common')
modules1(app)
modules2(app)
it('handles generic session for GraphQL', async () => {
const app = new koa();
app.keys = ['id','token'];
app.use(convert(genericSession({
key: 'session'
})));
app.use(async (ctx,next) => {
ctx.session.id = 'first';
await next();
});
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'SessionType',
fields: {
sessionId: {
type: GraphQLString,
resolve(parentValue, args, contextCtx) {
//here has session id and cookies
//console.log("contextCtx.session is======>",contextCtx.session)
import * as Koa from "koa";
import {MemoryStore, Session} from "koa-generic-session";
import session = require("koa-generic-session");
const app = new Koa();
app.use(session({
key: 'sessionKey',
store: MemoryStore(),
ttl: 60 * 60,
prefix: 'a-prefix',
cookie: {
path: '/test',
rewrite: false,
signed: false,
maxAge: 60 * 60,
secure: true,
httpOnly: true,
},
allowEmpty: false,
defer: false,
reconnectTimeout: 100,
rolling: false,
sessionIdStore: {
get: () => 'something',
import send from 'koa-send';
import koaBody from 'koa-body';
import sqlite3 from 'co-sqlite3';
import routes from './routes';
try {
const app = koa();
const body = koaBody({ multipart: true,formidable:{uploadDir:path.join(__dirname, './upload')} });
const sessionStore = require('koa-sqlite3-session');
app.keys = ["test"];
app.use(session({
store: new sessionStore(path.join(__dirname, '../../db/db'), {
})
}));
app.use(staticCache(path.join(__dirname, '../../public'), {
gzip: true
}));
app.use(function *(next){
var start = new Date().getTime();
try {
yield next;
} catch(e) {
console.error(e);
this.status = 500;
} finally {
export default () => koaSession(options)