Skip to content

Commit e3a09d7

Browse files
The JWT secret is now in a separate file in the newly created folder keys in the root of the project - we are using a 1024bit private key (precaution against brute forcing hs256 - see https://auth0.com/blog/brute-forcing-hs256-is-possible-the-importance-of-using-strong-keys-to-sign-jwts/)
1 parent 3ab86bc commit e3a09d7

File tree

7 files changed

+50
-16
lines changed

7 files changed

+50
-16
lines changed

keys/jwt.private.key

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-----BEGIN RSA PRIVATE KEY-----
2+
MIICWwIBAAKBgHc9aU8P/1PUILe/lUIZye+0nu+o/pzQ5Hvsy+i0cjt/ISE+nl+2
3+
pTetLVoNaKaGYv2V0jFCkWAp+hsmvPutSsbh62gVtoqcQUJn0PyywTJukXVXzsVa
4+
EeSrEeEmWF3jmvi97Cg0BoVWrQqHhDdml3gYEPCWYUGlwDwS0ULNq/iJAgMBAAEC
5+
gYAOtGISP/TKz7QhNN0hQ7DlgK7A+2Q6zn/+0FrtHaOgtrLNOL2rLAj/7rlTC6hG
6+
MPhwMVO0g3MOGt8fDg3sM5iu9vip18Ekz3YIoZQQqUdYF0SwgZKPKz5wzzw+R7lU
7+
llmmqMkIY3YpLucpb/dpJBT9Y2zq0/J0o50ykGyTVs6+oQJBALX0dweLaD+yyePj
8+
VTyUiPlc7q0mUyOlo20z5eMfItzr2yOGadrxayYzDm+XEzBVEG3AMpyeEwFg1bK7
9+
bNfWf1UCQQCnw3mgSYZPShzLvvUGCyte4Ys4NoSnY7No6YbTO1/oA8iSnXR3KBZK
10+
kOgqZdpMPOkDoVDr+YS7wRS6c1lORMxlAkB90GBNszOOeA3pqdPSY9KiuoO+7nUm
11+
fO4YIH6hIXJ12BBa7CJd5fj1HPCqcIgwL2GAwhk8+oChv1eEktycEhFRAkEAmGZE
12+
A+8m2rqZxCEZlz7oTE4Z2Zv1D9bLcX/LIfKrIirltwLgfSpmbaCLt5BFKcKfbtPJ
13+
nkRSZvl0qhgFRcvL3QJAEqvabbcjmSL9IHMF02mJYs/cey2LFqjhge++zY+sr91d
14+
nMqwJ1mue4XQVaSZfSF8GNk+YpR7DFNgKJEaQB4q9w==
15+
-----END RSA PRIVATE KEY-----

keys/jwt.public.key

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIGeMA0GCSqGSIb3DQEBAQUAA4GMADCBiAKBgHc9aU8P/1PUILe/lUIZye+0nu+o
3+
/pzQ5Hvsy+i0cjt/ISE+nl+2pTetLVoNaKaGYv2V0jFCkWAp+hsmvPutSsbh62gV
4+
toqcQUJn0PyywTJukXVXzsVaEeSrEeEmWF3jmvi97Cg0BoVWrQqHhDdml3gYEPCW
5+
YUGlwDwS0ULNq/iJAgMBAAE=
6+
-----END PUBLIC KEY-----

src/server/config/config.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { normalize, join } from 'path';
1+
import { extractKey } from '../utilities/keys';
22

33
interface IEnvironmentConfig {
44
rootPath: string;
@@ -18,14 +18,15 @@ interface IConfig {
1818
}
1919

2020
const rootPath = process.cwd();
21+
const jwtSecret = extractKey(`${rootPath}/keys/jwt.private.key`);
2122

2223
const Config: IConfig = {
2324
development: {
2425
rootPath,
2526
db: 'mongodb://localhost:27017/store',
2627
httpPort: 1337,
2728
wsPort: 1338,
28-
jwtSecret: 'secret',
29+
jwtSecret,
2930
domain: 'localhost',
3031
httpProtocol: 'http',
3132
wsProtocol: 'ws'

src/server/modules/auth/auth.service.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,8 @@ export class AuthService {
4040
};
4141
}
4242

43-
async validateUser(payload: any): Promise<boolean> {
44-
const user: IUser = await this.userModel.findById(payload.sub);
45-
46-
if (user) {
47-
return true;
48-
}
49-
50-
return false;
43+
async findUserById(id: string): Promise<IUser> {
44+
return await this.userModel.findById(id);
5145
}
5246

5347
async requestFacebookRedirectUri(): Promise<{redirect_uri: string}> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export interface IJwtPayload {
2+
sub: string;
3+
iat: number;
4+
exp: number;
5+
};

src/server/modules/auth/passport/jwt.strategy.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { ExtractJwt, Strategy } from 'passport-jwt';
55

66
import { AuthService } from '../auth.service';
77
import { SERVER_CONFIG, MESSAGES } from '../../../server.constants';
8+
import { IUser } from '../../user/interfaces/user.interface';
9+
import { IJwtPayload } from '../interfaces/jwt-payload.interface';
810

911
@Injectable()
1012
export class JwtStrategy extends Strategy {
@@ -15,16 +17,19 @@ export class JwtStrategy extends Strategy {
1517
passReqToCallback: true,
1618
secretOrKey: SERVER_CONFIG.jwtSecret,
1719
},
18-
async (req: Request, payload: any, next: Function) => await this.verify(req, payload, next),
20+
async (req: Request, payload: IJwtPayload, next: Function) =>
21+
await this.verify(payload, next)
1922
);
20-
use(this);
23+
use('jwt', this);
2124
}
2225

23-
public async verify(req: Request, payload: any, done: Function) {
24-
const isValid = await this.authService.validateUser(payload);
25-
if (!isValid) {
26+
public async verify(payload: IJwtPayload, done: Function) {
27+
const user: IUser = await this.authService.findUserById(payload.sub);
28+
29+
if (!user) {
2630
return done(new UnauthorizedException(MESSAGES.UNAUTHORIZED_UNRECOGNIZED_BEARER), false);
2731
}
28-
done(null, payload);
32+
33+
done(null, user);
2934
}
3035
}

src/server/utilities/keys.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { readFileSync } from 'fs';
2+
3+
export function extractKey(path: string) {
4+
return readFileSync(path)
5+
.toString()
6+
.replace(/\n|\r/g, '')
7+
.replace(/[-]+[\w\s]+[-]+/g, '');
8+
}

0 commit comments

Comments
 (0)