Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const app: Koa = new Koa()
// Loading certificates
const options: http2.SecureServerOptions = {
cert: fs.readFileSync(`${process.cwd()}/src/resources/cert/localhost.crt`),
key: fs.readFileSync(`${process.cwd()}/src/resources/cert/localhost.key`)
}
const _use: Function = app.use
app.use = (x: Middleware) => _use.call(app, convert(x))
app.use(helmet())
app.use(logger())
app.use(bodyParser())
app.use(errorMiddleware.errorMiddleware())
app.use(passport.initialize())
app.use(passport.session())
routes(app)
// show swagger only if the NODE_ENV is development and stagging
if (['development', 'staging'].includes(config.environment)) {
app.use(mount('/swagger', serve(`${process.cwd()}/src/resources/swagger`)))
}
http2
.createSecureServer(options, app.callback())
.listen(config.port, () => {
console.log(`Server started on ${config.port}`)
})
return __awaiter(this, void 0, void 0, function* () {
const authRouter = new KoaRouter();
const app = this.server.getApp();
authRouter.get('/test', (ctx) => __awaiter(this, void 0, void 0, function* () {
ctx.body = 'Hallo';
}));
authRouter.use(koaBody());
app.keys = [this.authConfig.secrets.cookie];
authRouter.use(koaSession(this.authConfig.oAuth.cookie, app));
authRouter.use(passport.initialize());
// authRouter.use(passport.session());
authRouter.post('/auth/invalidateAccessToken', (ctx) => __awaiter(this, void 0, void 0, function* () {
let success;
try {
success = yield this.invalidateUserToken(ctx.state.accessToken);
}
catch (err) {
success = false;
ctx.response.status = 400;
}
ctx.body = { success };
}));
authRouter.post('/auth/invalidateAllAccessTokens', (ctx) => __awaiter(this, void 0, void 0, function* () {
let success;
try {
success = yield this.invalidateAllUserTokens(ctx.state.accessToken);
const {
modelClass,
...options
} = isObject(app.session) ? app.session : {}
if (modelClass) {
// Create a ContextStore that resolved the specified model class,
// uses it to persist and retrieve the session, and automatically
// binds all db operations to `ctx.transaction`, if it is set.
// eslint-disable-next-line new-cap
options.ContextStore = SessionStore(modelClass)
}
this.use(session(options, this))
}
// 5. passport
if (app.passport) {
this.use(passport.initialize())
if (app.session) {
this.use(passport.session())
}
this.use(emitUserEvents())
}
// 6. finally handle the found route, or set status / allow accordingly.
this.use(handleRoute())
this.hasControllerMiddleware = true
}
}
"use strict";
const koa = require("koa");
const passport = require("koa-passport");
const errorHandler = require("koa-error");
const bodyParser = require("koa-bodyparser");
const logger = require("koa-logger");
const Router = require("koa-router");
const app = module.exports = koa();
app.use(logger());
app.use(errorHandler());
app.use(bodyParser());
app.use(passport.initialize());
var router = new Router();
router.use(function *(next) {
// everything json
this.type = "json";
this.headers['accept'] = "application/json";
yield next;
});
var secured = require('./secured')
var auth = require("./auth");
var posts = require("./posts");
var follows = require("./follows");
app.use(ratelimit({
db,
duration: 60000,
max: 100,
}));
const corsOptions = {
credentials: true,
};
app.use(cors(corsOptions));
app.use(bodyParser());
const passport = require('koa-passport');
app.use(passport.initialize());
app.use(passport.session());
const auth = require('./controllers/auth');
app.use(serve(path.join(process.env.PWD, '/dist')));
const router = new Router();
router
.post('/login', ctx => passport.authenticate('local', (err, user) => {
if (!user) {
ctx.throw(401, err);
} else {
ctx.body = user;
return ctx.login(user);
}
})(ctx))
const app = new Koa()
app.keys = [config.session]
mongoose.Promise = global.Promise
mongoose.connect(config.database)
app.use(convert(logger()))
app.use(bodyParser())
app.use(session())
app.use(errorMiddleware())
app.use(convert(mount('/docs', serve(`${process.cwd()}/docs`))))
require('../config/passport')
app.use(passport.initialize())
app.use(passport.session())
const modules = require('../src/modules')
modules(app)
app.listen(config.port, () => {
console.log(`Server started on ${config.port}`)
})
export default app
/**
* From this point, you are working with KoaJS.
* Add overrides to the koa instance jollofJS will run on.
* You could implement any plugin supported by Koa here.
*/
app.proxy = true;
/**
* This scaffold app wires together a quick koa-passport implementation for you.
* However, JollofJS is not dependent on any of these and you can rip it out and use whatever
* authentication/authorization wiring you need for your app.
*/
app.use(passport.initialize());
app.use(passport.session());
app.use(convert(function* (next) {
this.passport = passport;
return yield next;
}));
//Setup passport auth strategies
require('./app/services/passport/strategies').setupStrategies(app, passport);
});
export default function auth() {
return compose([
passport.initialize(),
passport.session(),
]);
}
initStrategies() {
if (this.strategies.length > 0) {
this.parent.parent.logger.info(
"[VelopServer][Passport] Initialize " +
this.strategies.length +
" passport strategies"
);
this.parent.app.use(passport.initialize());
this.parent.app.use(passport.session());
passport.serializeUser((user, done) => done(null, user));
passport.deserializeUser((user, done) => done(null, user));
this.strategies.map(strategy => passport.use(strategy));
}
}