Skip to content

Commit f6f3c8e

Browse files
author
Raphael Kabo
committed
mostly typescript tweaks
1 parent eee9e8c commit f6f3c8e

File tree

4 files changed

+32
-39
lines changed

4 files changed

+32
-39
lines changed

server/IdentityLocalState.ts

+6-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,17 @@
11
import type { Response } from 'express';
2-
3-
//could this be unified with IdentityDetails in globalstate.ts ?
4-
interface IdentityLocalState {
5-
userId?: string;
6-
email?: string;
7-
name?: string;
8-
signInStatus?: string;
9-
}
2+
import type { IdentityDetails } from '@/shared/globals';
103

114
export const setIdentityLocalState = (
125
res: Response,
13-
identityLocalState: IdentityLocalState,
14-
): void => {
6+
identityLocalState: IdentityDetails,
7+
) => {
158
res.locals.identity = identityLocalState;
169
};
1710
export const getIdentityLocalState = (
1811
res: Response,
19-
): IdentityLocalState | undefined => {
20-
return res.locals.identity;
12+
): IdentityDetails | undefined => {
13+
return res.locals?.identity;
2114
};
22-
export const clearIdentityLocalState = (res: Response): void => {
15+
export const clearIdentityLocalState = (res: Response) => {
2316
delete res.locals.identity;
2417
};

server/middleware/OktaServerSideAuthMiddleware.ts

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { joinUrl } from '@guardian/libs';
22
import type { NextFunction, Request, Response } from 'express';
3-
import fetch from 'node-fetch';
43
import {
54
clearIdentityLocalState,
65
getIdentityLocalState,
@@ -33,12 +32,12 @@ export const withOktaSeverSideValidation = async (
3332
}
3433

3534
console.log('Validating token server side ...');
36-
const signinRequired = () => requiresSignin(req.originalUrl);
35+
const signinRequired = requiresSignin(req.originalUrl);
3736

3837
const locallyValidatedIdentityData = getIdentityLocalState(res);
3938
const accessToken = req.signedCookies[OAuthAccessTokenCookieName];
4039
if (!accessToken || !locallyValidatedIdentityData?.userId) {
41-
if (signinRequired()) {
40+
if (signinRequired) {
4241
console.error(
4342
'error: no access token or user in request for a sign-in required endpoint! this should have failed local validation',
4443
);
@@ -65,18 +64,21 @@ export const withOktaSeverSideValidation = async (
6564
console.log(`okta response status: ${oktaResponse.status}`);
6665
if (oktaResponse.status == 200) {
6766
//valid token
68-
const oktaUserInfo = await oktaResponse.json<OktaUserInfo>();
67+
const oktaUserInfo = (await oktaResponse.json()) as OktaUserInfo;
68+
// Refresh the local state from the Okta response.
6969
setIdentityLocalState(res, {
70-
signInStatus: 'signedInRecently', // TODO can I hardcode this here or do I need to check something else ?
70+
// This is always 'signedInRecently' because we've just checked
71+
// the token is valid with Okta, and it's only valid for 30 minutes.
72+
signInStatus: 'signedInRecently',
7173
userId: oktaUserInfo.legacy_identity_id,
72-
name: oktaUserInfo.name,
74+
displayName: oktaUserInfo.name,
7375
email: oktaUserInfo.email,
7476
});
7577
return next();
76-
} else if ([403, 401].includes(oktaResponse.status)) {
78+
} else if ([401, 403].includes(oktaResponse.status)) {
7779
//invalid token
7880
clearIdentityLocalState(res);
79-
if (signinRequired()) {
81+
if (signinRequired) {
8082
return res.sendStatus(401);
8183
}
8284
return next();

server/oauth.ts

+15-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import crypto from 'crypto';
22
import { joinUrl } from '@guardian/libs';
3+
import type { JwtClaims } from '@okta/jwt-verifier';
34
import OktaJwtVerifier from '@okta/jwt-verifier';
45
import type { CookieOptions, Request, Response } from 'express';
56
import ms from 'ms';
@@ -25,6 +26,12 @@ export const oauthCookieOptions: CookieOptions = {
2526
httpOnly: true,
2627
};
2728

29+
interface IdTokenClaims extends JwtClaims {
30+
legacy_identity_id: string;
31+
name: string;
32+
email: string;
33+
}
34+
2835
/**
2936
* @function getOktaOrgUrl
3037
*
@@ -348,25 +355,16 @@ export const setLocalStateFromIdTokenOrUserCookie = (
348355
// signed in menu, but not show the user's name or email.
349356
const hasIdTokenOrUserCookie = idToken || req.cookies['GU_U'];
350357

351-
let identityIdClaim: string | undefined;
352-
let nameClaim: string | undefined;
353-
let emailClaim: string | undefined;
354-
355-
if (typeof idToken?.claims.legacy_identity_id === 'string') {
356-
identityIdClaim = idToken?.claims.legacy_identity_id;
357-
}
358-
if (typeof idToken?.claims.name === 'string') {
359-
nameClaim = idToken?.claims.name;
360-
}
361-
if (typeof idToken?.claims.email === 'string') {
362-
emailClaim = idToken?.claims.email;
363-
}
364-
358+
const {
359+
legacy_identity_id: userId,
360+
name: displayName,
361+
email,
362+
} = idToken?.claims as IdTokenClaims;
365363
setIdentityLocalState(res, {
366364
signInStatus: hasIdTokenOrUserCookie ? 'signedInRecently' : undefined,
367-
userId: identityIdClaim,
368-
name: nameClaim,
369-
email: emailClaim,
365+
userId,
366+
displayName,
367+
email,
370368
});
371369
};
372370

shared/globals.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface CommonGlobals {
66
dsn: string | null;
77
}
88

9-
interface IdentityDetails {
9+
export interface IdentityDetails {
1010
userId?: string;
1111
email?: string;
1212
displayName?: string;

0 commit comments

Comments
 (0)