Skip to content

Commit 0d67bfe

Browse files
authored
Merge pull request #1425 from guardian/user-benefits-api
Use user-benefits API to work out newspaper archive entitlement
2 parents 9f086a3 + 77a6d18 commit 0d67bfe

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

server/apiProxy.ts

+1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ export const authorizationOrCookieHeader = async ({
185185
}
186186
switch (host) {
187187
case 'members-data-api.' + conf.DOMAIN:
188+
case 'user-benefits.' + conf.API_DOMAIN:
188189
return {
189190
Authorization: `Bearer ${req.signedCookies[OAuthAccessTokenCookieName]}`,
190191
};

server/routes/newspaperArchive.ts

+11-14
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ const NewspapersResponseSchema = z.object({
1818
url: z.string(),
1919
});
2020

21-
const UserAttributesSchema = z.object({
22-
contentAccess: z.record(z.string(), z.boolean()),
21+
const userBenefitsSchema = z.object({
22+
benefits: z.array(z.string()),
2323
});
2424

2525
type NewspaperArchiveConfig = {
@@ -47,13 +47,16 @@ router.get('/auth', async (req: Request, res: Response) => {
4747
return res.sendStatus(500);
4848
}
4949

50+
console.log('Checking supporter entitlement');
5051
const hasCorrectEntitlement = await checkSupporterEntitlement(req);
5152

5253
if (!hasCorrectEntitlement) {
5354
// ToDo: show the user an error/info page
55+
console.log('User does not have the newspaper archive entitlement');
5456
return res.redirect('/');
5557
}
5658

59+
console.log('User has the newspaper archive entitlement');
5760
const authHeader = base64(`${authString}`);
5861
const requestBody: NewspapersRequestBody = {};
5962

@@ -107,22 +110,16 @@ router.get('/auth', async (req: Request, res: Response) => {
107110
export { router };
108111

109112
async function checkSupporterEntitlement(req: Request): Promise<boolean> {
110-
const supporterAttributesResponse = await getSupporterStatus(req);
111-
const supporterAttributes = UserAttributesSchema.parse(
112-
await supporterAttributesResponse.json(),
113-
);
114-
115-
// ToDo: this should return a flag that represents either Tier 3 or a newspaperArchive specific entitlement
116-
return (
117-
supporterAttributes.contentAccess['guardianWeeklySubscriber'] &&
118-
supporterAttributes.contentAccess['supporterPlus']
119-
);
113+
const supporterAttributes = await getSupporterStatus(req)
114+
.then((res) => res.json())
115+
.then((json) => userBenefitsSchema.parse(json));
116+
return supporterAttributes.benefits.includes('newspaperArchive');
120117
}
121118

122119
async function getSupporterStatus(req: Request) {
123-
const host = 'members-data-api.' + conf.DOMAIN;
120+
const host = 'user-benefits.' + conf.API_DOMAIN;
124121

125-
return fetch(`https://${host}/user-attributes/me`, {
122+
return fetch(`https://${host}/benefits/me`, {
126123
method: 'GET',
127124
headers: {
128125
...(await authorizationOrCookieHeader({ req, host })),

0 commit comments

Comments
 (0)