1
1
import { Router } from 'express' ;
2
2
import type { Request , Response } from 'express' ;
3
3
import fetch from 'node-fetch' ;
4
+ import { z } from 'zod' ;
4
5
import { authorizationOrCookieHeader } from '../apiProxy' ;
5
6
import { s3ConfigPromise } from '../awsIntegration' ;
6
7
import { conf } from '../config' ;
@@ -13,9 +14,13 @@ type NewspapersRequestBody = {
13
14
} ;
14
15
15
16
// { url: "https://<subdomain>.newspapers.com/…?tpa=<token>" }
16
- type NewspapersResponseBody = {
17
- url : string ;
18
- } ;
17
+ const NewspapersResponseSchema = z . object ( {
18
+ url : z . string ( ) ,
19
+ } ) ;
20
+
21
+ const UserAttributesSchema = z . object ( {
22
+ contentAccess : z . record ( z . string ( ) , z . boolean ( ) ) ,
23
+ } ) ;
19
24
20
25
type NewspaperArchiveConfig = {
21
26
authString : string ;
@@ -64,8 +69,9 @@ router.get('/auth', async (req: Request, res: Response) => {
64
69
} ,
65
70
) ;
66
71
67
- // ToDo: we have zod on the server, we could parse the responses with that
68
- const responseJson = ( await response . json ( ) ) as NewspapersResponseBody ;
72
+ const responseJson = NewspapersResponseSchema . parse (
73
+ await response . json ( ) ,
74
+ ) ;
69
75
70
76
const archiveReturnUrlString = req . query [ 'ncom-return-url' ] ;
71
77
if (
@@ -92,7 +98,9 @@ export { router };
92
98
93
99
async function checkSupporterEntitlement ( req : Request ) : Promise < boolean > {
94
100
const supporterAttributesResponse = await getSupporterStatus ( req ) ;
95
- const supporterAttributes = await supporterAttributesResponse . json ( ) ;
101
+ const supporterAttributes = UserAttributesSchema . parse (
102
+ await supporterAttributesResponse . json ( ) ,
103
+ ) ;
96
104
97
105
// ToDo: this should return a flag that represents either Tier 3 or a newspaperArchive specific entitlement
98
106
return (
0 commit comments