This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
import Koa from 'koa';
|
||||
import bodyparser from 'koa-bodyparser';
|
||||
import cookie from 'koa-cookie';
|
||||
import passport from 'koa-passport';
|
||||
import session from 'koa-session';
|
||||
|
||||
import passport from './passport';
|
||||
import { performanceLogger, perfromanceTimer } from './middleware/performance';
|
||||
import { errorHandler } from './middleware/errorHandler';
|
||||
|
||||
|
||||
16
lib/auth.ts
16
lib/auth.ts
@@ -1,16 +0,0 @@
|
||||
// import koaPassport from 'koa-passport';
|
||||
|
||||
// import Users from 'grow-db/lib/models/users';
|
||||
// import { User } from 'grow-db/lib/schemas/user';
|
||||
|
||||
// passport.serializeUser((user: User, done) => { done(null, user._id); });
|
||||
|
||||
// passport.deserializeUser(async (id, done) => {
|
||||
// const user = await Users.findById(id);
|
||||
|
||||
// if (user) {
|
||||
// done(null, user);
|
||||
// }
|
||||
|
||||
// done('user not found', null);
|
||||
// });
|
||||
@@ -2,3 +2,5 @@ export const PORT = 9000;
|
||||
export const API_PATH = '/api';
|
||||
export const AUTH_ROUTE = '/auth';
|
||||
export const RESET_ROUTE = `${AUTH_ROUTE}/reset`;
|
||||
|
||||
export const JWT_SECRET = 'secret';
|
||||
|
||||
23
lib/passport/index.ts
Normal file
23
lib/passport/index.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import passport from 'koa-passport';
|
||||
|
||||
import Auth from '../model/auth';
|
||||
import { Auth as AuthRecord } from '../schema/auth';
|
||||
import LocalStrategy from './strategies/local';
|
||||
import JwtStrategy from './strategies/jwt';
|
||||
|
||||
passport.serializeUser((user, done) => done(null, (user as AuthRecord).record));
|
||||
|
||||
passport.deserializeUser(async (id, done) => {
|
||||
const user = await Auth.findOne({ record: id });
|
||||
|
||||
if (user) {
|
||||
done(null, user);
|
||||
}
|
||||
|
||||
done('user not found', null);
|
||||
});
|
||||
|
||||
passport.use(LocalStrategy);
|
||||
passport.use(JwtStrategy);
|
||||
|
||||
export default passport;
|
||||
22
lib/passport/strategies/jwt.ts
Normal file
22
lib/passport/strategies/jwt.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
// eslint-disable-next-line import/named
|
||||
import { ExtractJwt, Strategy as JwtStrategy } from 'passport-jwt';
|
||||
|
||||
import Auth from '../../model/auth';
|
||||
import { getJwtSecret } from '../../utils/jwt';
|
||||
|
||||
const opts = {
|
||||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||
secretOrKey: getJwtSecret(),
|
||||
issuer: process.env.JWT_ISSUER,
|
||||
audience: process.env.JWT_AUDIENCE,
|
||||
};
|
||||
|
||||
export default new JwtStrategy(opts, async (jwt_payload, done) => {
|
||||
const auth = await Auth.findOne({ record: jwt_payload.sub }).catch();
|
||||
|
||||
if (auth) {
|
||||
return done(null, auth);
|
||||
}
|
||||
|
||||
return done(null, false);
|
||||
});
|
||||
18
lib/passport/strategies/local.ts
Normal file
18
lib/passport/strategies/local.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import passport from 'koa-passport';
|
||||
// eslint-disable-next-line import/named
|
||||
import { Strategy as LocalStrategy } from 'passport-local';
|
||||
|
||||
import Auth from '../../model/auth';
|
||||
|
||||
export default new LocalStrategy(async (username: string, password: string, done: any) => {
|
||||
const user = await Auth.findOne({
|
||||
where: {
|
||||
username,
|
||||
},
|
||||
}).catch();
|
||||
if (user && user.authenticate(password)) {
|
||||
done(null, user);
|
||||
} else {
|
||||
done(null, false);
|
||||
}
|
||||
});
|
||||
@@ -1,20 +0,0 @@
|
||||
import passport from 'koa-passport';
|
||||
// eslint-disable-next-line import/named
|
||||
import { Strategy as LocalStrategy } from 'passport-local';
|
||||
|
||||
import Auth from '../model/auth';
|
||||
|
||||
export const localStrategy = passport.use(
|
||||
new LocalStrategy(async (username: string, password: string, done: any) => {
|
||||
const user = await Auth.findOne({
|
||||
where: {
|
||||
username,
|
||||
},
|
||||
}).catch();
|
||||
if (user && user.authenticate(password)) {
|
||||
done(null, user);
|
||||
} else {
|
||||
done(null, false);
|
||||
}
|
||||
}),
|
||||
);
|
||||
@@ -1,5 +1,7 @@
|
||||
import jwt from 'jsonwebtoken';
|
||||
import { JWT_SECRET } from '../constants/defaults';
|
||||
|
||||
export const getJwtSecret = () => process.env.JWT_SECRET || JWT_SECRET;
|
||||
export interface TokenProps {
|
||||
aud?: string;
|
||||
exp?: number | Date;
|
||||
@@ -28,8 +30,8 @@ export const sign = (props: SignProps) => {
|
||||
iat: today.getTime(),
|
||||
iss: rest.iss || process.env.JWT_ISSUER,
|
||||
},
|
||||
process.env.JWT_SECRET || 'secret',
|
||||
getJwtSecret(),
|
||||
);
|
||||
};
|
||||
|
||||
export const verify = (token: string) => jwt.verify(token, process.env.JWT_SECRET || 'secret');
|
||||
export const verify = (token: string) => jwt.verify(token, getJwtSecret());
|
||||
|
||||
Reference in New Issue
Block a user