Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
koa-passport
Advanced tools
koa-passport is a Koa middleware that integrates Passport.js for authentication. It allows you to use various authentication strategies, manage user sessions, and protect routes in a Koa application.
Authentication Strategy
This code demonstrates how to set up a local authentication strategy using koa-passport. It includes initializing the Koa app, setting up the LocalStrategy, and defining a login route.
const Koa = require('koa');
const Router = require('koa-router');
const bodyParser = require('koa-bodyparser');
const passport = require('koa-passport');
const LocalStrategy = require('passport-local').Strategy;
const app = new Koa();
const router = new Router();
passport.use(new LocalStrategy((username, password, done) => {
// Replace with your own logic
if (username === 'user' && password === 'pass') {
return done(null, { id: 1, username: 'user' });
} else {
return done(null, false);
}
}));
app.use(bodyParser());
app.use(passport.initialize());
app.use(passport.session());
router.post('/login', passport.authenticate('local', {
successRedirect: '/success',
failureRedirect: '/login'
}));
app.use(router.routes());
app.listen(3000);
Session Management
This code demonstrates how to set up session management with koa-passport. It includes initializing session middleware and defining serializeUser and deserializeUser functions.
const session = require('koa-session');
app.keys = ['your-session-secret'];
app.use(session({}, app));
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser((id, done) => {
// Replace with your own logic
done(null, { id: 1, username: 'user' });
});
Protecting Routes
This code demonstrates how to protect routes using koa-passport. It includes defining a protected route that requires authentication.
router.get('/protected', passport.authenticate('local', { session: false }), (ctx) => {
ctx.body = 'This is a protected route';
});
Passport.js is a popular authentication middleware for Node.js. It is framework-agnostic and can be used with Express, Koa, and other frameworks. Compared to koa-passport, Passport.js requires additional setup to integrate with Koa.
koa-auth is a Koa middleware for authentication. It provides a simpler API compared to koa-passport but may not support as many authentication strategies out of the box.
koa-jwt is a Koa middleware for JSON Web Token (JWT) authentication. It is useful for stateless authentication and can be used in combination with other authentication strategies. Unlike koa-passport, it focuses solely on JWT.
Passport middleware for Koa
// body parser
var bodyParser = require('koa-bodyparser')
app.use(bodyParser())
// Sessions
var session = require('koa-session')
app.keys = ['secret']
app.use(session())
var passport = require('koa-passport')
app.use(passport.initialize())
app.use(passport.session())
Copyright (c) 2014 Markus Ast
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0.4.0
public.post('/login', function*(next) {
var ctx = this
yield* passport.authenticate('local', function*(err, user, info) {
if (err) throw err
if (user === false) {
ctx.status = 401
ctx.body = { success: false }
} else {
yield ctx.login(user)
ctx.body = { success: true }
}
}).call(this, next)
})
FAQs
Passport middleware for Koa
The npm package koa-passport receives a total of 152,215 weekly downloads. As such, koa-passport popularity was classified as popular.
We found that koa-passport demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.