From 48afa73ae8762d43835f992af046ac1c4faa6b0f Mon Sep 17 00:00:00 2001 From: Timo Hocker Date: Tue, 5 Jan 2021 15:59:06 +0100 Subject: [PATCH] fix --- lib/AuthHandler.ts | 2 +- lib/Authority.ts | 4 ++-- lib/Gateway.ts | 6 ++---- test/spec/Gateway.ts | 23 ++++++++++++++++++----- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/lib/AuthHandler.ts b/lib/AuthHandler.ts index 3f7fb50..0f4d287 100644 --- a/lib/AuthHandler.ts +++ b/lib/AuthHandler.ts @@ -37,7 +37,7 @@ class AuthRequest { public is_basic: boolean; public user: string; public password: string; - public token_data?: Record; + public token_data?: unknown; public token_id?: string; public body: string; diff --git a/lib/Authority.ts b/lib/Authority.ts index 9c81b83..58f5532 100644 --- a/lib/Authority.ts +++ b/lib/Authority.ts @@ -22,7 +22,7 @@ interface VerificationResult { type: TokenType; id: string; next_module?: string; - data?: Record; + data?: unknown; error?: string; } @@ -33,7 +33,7 @@ interface SignatureResult { interface SignatureOptions { - data?: Record + data?: unknown next_module?: string } diff --git a/lib/Gateway.ts b/lib/Gateway.ts index fd4d54c..badf237 100644 --- a/lib/Gateway.ts +++ b/lib/Gateway.ts @@ -67,10 +67,8 @@ class GatewayClass { const ver = authority.verify (auth); - const con = req.connection as Record; - con.auth = {}; - con.auth.token_id = ver.id; - con.auth.token_data = ver.data; + const con = req.connection as unknown as Record; + con.auth = { token_id: ver.id, token_data: ver.data }; return ver.authorized; } diff --git a/test/spec/Gateway.ts b/test/spec/Gateway.ts index fac5ed2..a93a701 100644 --- a/test/spec/Gateway.ts +++ b/test/spec/Gateway.ts @@ -29,7 +29,8 @@ describe ('gateway', () => { server = http.createServer ((req, res) => { const passed_handler = () => { res.writeHead (200); - res.end ('passed'); + const con = req.connection as unknown as Record; + res.end (JSON.stringify (con.auth)); }; g (req, res, passed_handler); }); @@ -60,8 +61,8 @@ describe ('gateway', () => { const resp = await get ({ authorization: `Bearer ${token.signature}` }); expect (resp.statusCode) .toEqual (200); - expect (resp.body) - .toEqual ('passed'); + expect (JSON.parse (resp.body as string).token_id) + .toEqual (token.id); }); it ('should allow a valid access token using cookies', async () => { @@ -69,8 +70,20 @@ describe ('gateway', () => { const resp = await get ({ cookie: `cookie_jar=${token.signature}` }); expect (resp.statusCode) .toEqual (200); - expect (resp.body) - .toEqual ('passed'); + expect (JSON.parse (resp.body as string).token_id) + .toEqual (token.id); + }); + + it ('should correctly deliver token data', async () => { + const token = authority.sign ('access_token', 60, { data: 'foobar' }); + const resp = await get ({ authorization: `Bearer ${token.signature}` }); + expect (resp.statusCode) + .toEqual (200); + const body = JSON.parse (resp.body as string); + expect (body.token_id) + .toEqual (token.id); + expect (body.token_data) + .toEqual ('foobar'); }); it ('should reject an outdated access token', async () => {