File tree 8 files changed +95
-23
lines changed 8 files changed +95
-23
lines changed Original file line number Diff line number Diff line change @@ -3,14 +3,14 @@ var jwt = require('jsonwebtoken');
3
3
4
4
5
5
// verify an existing JWT
6
- var existingToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhbGdvcml0aG0iOiJIUzI1NiIsImRhdGEiOnsidXNlcklkIjoxfSwiaWF0IjoxNTAyODgxOTIxfQ.6ayBjYiaTMJ8Z3tYx6VfueFLDN1U8SFl94B7U3ZWO6Q ' ;
6
+ var existingToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiQWxpY2UiLCJpYXQiOjE1MDI4ODkxOTF9._tPQtlZz2GhXHXATn5W09K4XCG0Z5LyEQqikJf3qXF8 ' ;
7
7
8
8
9
9
var secretKey = 'secret-key' ;
10
10
11
11
12
12
13
- const verify = jwt . verify ( existingToken , secretKey , { algorithm : 'HS256' } ) ;
13
+ const verify = jwt . verify ( existingToken , secretKey ) ;
14
14
15
15
16
16
console . log ( "Decoded JWT:" , verify ) ;
Original file line number Diff line number Diff line change @@ -4,15 +4,15 @@ var fs = require('fs');
4
4
5
5
6
6
// verify an existing JWT
7
- var existingToken = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InRlc3RAbWFpbGluYXRvci5jb20iLCJpYXQiOjE1MDI4ODAzODB9.LiZZp_SIy2TApLnJGjWfWhKUU0uc6oh5wJa5gLY4l82cmgB4MGMssxbagaIROmkmSA68tk57YihBmbz7d76lyV1dWw6HAZ6KttvkHnvk8Zyg0QethIG6TYPJ083H_xWUBTDDF-bQCXf3AgELMuKyUWqVONW294tW5n7vKqo41eMx-r372oxHdL9Du_GzZ2LJrWtxPnaIWh5hb0MiPz5KNKlWh0D4MBb-lEkmghc7QE69mIKJ2u3-ZYe_i3KGEclCZArKusmpxfhNbmfvU_JX2kF7ko4HS5qe4a7ZV04Bzgovz5TNZ-13j79jSWpWod3jA_xZZfLfMpgBhteWuxhImw ' ;
7
+ var existingToken = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoiQWxpY2UiLCJpYXQiOjE1MDI4OTA4NjYsImV4cCI6MTUwMjg5MDk4Niwic3ViIjoiMSJ9.SkF5CArLIU9A0MeCiv-HOiRctivGESnXHnxGSwc7lx1nBP0-qO7-r3LO4TVLlLydQBce0hQAbiik1lndgiR1V_8TN-dMGcpFYZlSv3gp4cJD5LNxl_fQ9CBhpznkNo6Yuys8DYIfL90uWpm0jjnSJh2hFDxbGwPaXTciJ0Xdj-QLq2hc_jHrRKaWlB6eCbs9P43tfrUw_pXb0lCdYfPMjPLHJMNyZMLxCxExBoHay7UoYDx_cYU-UTR1hQQ2xbHfrA8oMz1fOCjwCChEXuDKVDqOF9W4LhbrdvGmVgv8mvdl72VSHkqH5jWc3vh0u5xJtsMpRBwg0-OBU3hOebtlyg ' ;
8
8
9
9
10
10
var publicKey = fs . readFileSync ( './demos/public.key' ) ;
11
11
12
12
13
13
console . log ( "verifying" ) ;
14
14
15
- const verify = jwt . verify ( existingToken , publicKey , { algorithm : 'RS256' } ) ;
15
+ const verify = jwt . verify ( existingToken , publicKey ) ;
16
16
17
17
18
18
Original file line number Diff line number Diff line change @@ -6,15 +6,14 @@ var jwt = require('jsonwebtoken');
6
6
var secretKey = 'secret-key' ;
7
7
8
8
var payload = {
9
- userId : 1
9
+ name : 'Alice'
10
10
} ;
11
11
12
12
13
13
// create a JWT
14
- var newToken = jwt . sign ( {
15
- algorithm : 'HS256' ,
16
- data : payload
17
- } , secretKey ) ;
14
+ var newToken = jwt . sign ( payload , secretKey , {
15
+ algorithm : 'HS256'
16
+ } ) ;
18
17
19
18
console . log ( "JWT created:" , newToken ) ;
20
19
Original file line number Diff line number Diff line change @@ -6,14 +6,15 @@ var fs = require('fs');
6
6
var privateKey = fs . readFileSync ( './demos/private.key' ) ;
7
7
8
8
var payload = {
9
- userId : 1
9
+ name : 'Alice'
10
10
} ;
11
11
12
12
13
- var token = jwt . sign ( {
13
+ var token = jwt . sign ( payload , privateKey , {
14
14
algorithm : 'RS256' ,
15
- data : payload
16
- } , privateKey ) ;
15
+ expiresIn : 120 ,
16
+ subject : "1"
17
+ } ) ;
17
18
18
19
19
20
console . log ( 'RSA 256 JWT' , token ) ;
Original file line number Diff line number Diff line change
1
+ import { Request , Response , NextFunction } from 'express' ;
2
+ import { isSessionTokenValid } from "./security.utils" ;
3
+
4
+
5
+ export function retrieveUserIdFromRequest ( req : Request , res : Response , next : NextFunction ) {
6
+
7
+ const jwt = req . cookies [ 'SESSIONID' ] ;
8
+
9
+ if ( jwt ) {
10
+ handleSessionCookie ( jwt , req , next ) ;
11
+ }
12
+ else {
13
+ next ( ) ;
14
+ }
15
+ }
16
+
17
+ async function handleSessionCookie ( jwt , req : Request , next : NextFunction ) {
18
+ try {
19
+
20
+ const token = await isSessionTokenValid ( jwt ) ;
21
+
22
+ console . log ( "decoded token" , token ) ;
23
+
24
+ req [ 'userId' ] = token . sub ;
25
+
26
+ }
27
+ catch ( error ) {
28
+ console . error ( "Error: Could not extract user from request" ) ;
29
+ }
30
+ finally {
31
+ next ( ) ;
32
+ }
33
+ }
34
+
35
+
36
+ export function checkIfAuthenticated ( req : Request , res : Response , next : NextFunction ) {
37
+
38
+ console . log ( "Calling check if authenticated ..." ) ;
39
+
40
+ next ( ) ;
41
+
42
+ }
Original file line number Diff line number Diff line change @@ -6,10 +6,10 @@ import {Request, Response} from "express";
6
6
7
7
export function getUser ( req :Request , res :Response ) {
8
8
9
- const sessionId = req . cookies [ 'SESSIONID' ] ;
9
+ const userId = req . cookies [ 'SESSIONID' ] ;
10
10
11
11
//TODO
12
- const user = { } ;
12
+ const user = { email : 'test@gmail.com' } ;
13
13
14
14
15
15
if ( user ) {
Original file line number Diff line number Diff line change @@ -11,15 +11,42 @@ import * as fs from "fs";
11
11
12
12
const RSA_PRIVATE_KEY = fs . readFileSync ( './demos/private.key' ) ;
13
13
14
+ const RSA_PUBLIC_KEY = fs . readFileSync ( './demos/public.key' ) ;
15
+
16
+
17
+
14
18
export const randomBytes = util . promisify ( crypto . randomBytes ) ;
15
19
16
20
17
21
18
22
export async function createSessionToken ( userId :number ) {
19
23
return jwt . sign (
24
+ {
25
+ } ,
26
+ RSA_PRIVATE_KEY ,
20
27
{
21
28
algorithm : 'RS256' ,
22
- exp : ( moment ( ) . add ( 2 , "minutes" ) . toDate ( ) . getTime ( ) / 1000 ) ,
23
- subject : userId
24
- } , RSA_PRIVATE_KEY ) ;
25
- }
29
+ expiresIn : 120 ,
30
+ subject : '' + userId
31
+ } ) ;
32
+ }
33
+
34
+
35
+ export async function isSessionTokenValid ( sessionToken :string ) {
36
+
37
+ console . log ( "validating token" , sessionToken ) ;
38
+
39
+ const verify = await jwt . verify ( sessionToken , RSA_PUBLIC_KEY ) ;
40
+
41
+ console . log ( "decoded token" , verify ) ;
42
+
43
+ return verify ;
44
+ }
45
+
46
+
47
+
48
+
49
+
50
+
51
+
52
+
Original file line number Diff line number Diff line change @@ -9,14 +9,17 @@ import {createUser} from "./create-user.route";
9
9
import { getUser } from "./get-user.route" ;
10
10
import { logout } from "./logout.route" ;
11
11
import { login } from "./login.route" ;
12
+ import { checkIfAuthenticated , retrieveUserIdFromRequest } from "./auth.middleware" ;
12
13
const bodyParser = require ( 'body-parser' ) ;
13
14
const cookieParser = require ( 'cookie-parser' ) ;
14
15
15
16
16
17
const app : Application = express ( ) ;
17
18
18
- app . use ( bodyParser . json ( ) ) ;
19
19
app . use ( cookieParser ( ) ) ;
20
+ app . use ( retrieveUserIdFromRequest ) ;
21
+ app . use ( bodyParser . json ( ) ) ;
22
+
20
23
21
24
const commandLineArgs = require ( 'command-line-args' ) ;
22
25
@@ -29,16 +32,16 @@ const options = commandLineArgs(optionDefinitions);
29
32
30
33
// REST API
31
34
app . route ( '/api/lessons' )
32
- . get ( readAllLessons ) ;
35
+ . get ( checkIfAuthenticated , readAllLessons ) ;
33
36
34
37
app . route ( '/api/signup' )
35
38
. post ( createUser ) ;
36
39
37
40
app . route ( '/api/user' )
38
- . get ( getUser ) ;
41
+ . get ( checkIfAuthenticated , getUser ) ;
39
42
40
43
app . route ( '/api/logout' )
41
- . post ( logout ) ;
44
+ . post ( checkIfAuthenticated , logout ) ;
42
45
43
46
app . route ( '/api/login' )
44
47
. post ( login ) ;
You can’t perform that action at this time.
0 commit comments