-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauthenticateRequest.ts
More file actions
338 lines (307 loc) · 10.1 KB
/
authenticateRequest.ts
File metadata and controls
338 lines (307 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import {Logger} from "@aws-lambda-powertools/logger"
import {DynamoDBDocumentClient} from "@aws-sdk/lib-dynamodb"
import {getSecret} from "@aws-lambda-powertools/parameters/secrets"
import {AxiosInstance} from "axios"
import {
refreshApigeeAccessToken,
verifyIdToken,
constructSignedJWTBody,
exchangeTokenForApigeeAccessToken
} from "./index"
import {deleteTokenMapping, updateTokenMapping, TokenMappingItem} from "@cpt-ui-common/dynamoFunctions"
// Define the ApigeeTokenResponse type
interface ApigeeTokenResponse {
accessToken: string
idToken?: string
refreshToken?: string
expiresIn: number
}
/**
* Represents the result of a successful authentication
*/
export interface AuthResult {
username: string
apigeeAccessToken: string
roleId?: string
orgCode?: string
sessionId?: string
isConcurrentSession?: boolean
}
/**
* Represents a timeout authentication result
*/
export interface AuthTimeoutResult {
isTimeout?: boolean
}
/**
* Options for the authenticateRequest function
*/
export interface AuthenticateRequestOptions {
tokenMappingTableName: string
sessionManagementTableName: string
jwtPrivateKeyArn: string
apigeeApiKeyArn: string
apigeeApiSecretArn: string
jwtKid: string
apigeeCis2TokenEndpoint: string
apigeeMockTokenEndpoint: string
cloudfrontDomain: string
}
export type AuthDependencies = {
axiosInstance: AxiosInstance
ddbClient: DynamoDBDocumentClient
authOptions: AuthenticateRequestOptions
logger: Logger
}
export const authParametersFromEnv = (): AuthenticateRequestOptions => {
return {
tokenMappingTableName: process.env["TokenMappingTableName"] as string,
sessionManagementTableName: process.env["SessionManagementTableName"] as string,
jwtPrivateKeyArn: process.env["jwtPrivateKeyArn"] as string,
apigeeApiKeyArn: process.env["APIGEE_API_KEY_ARN"] as string,
apigeeApiSecretArn: process.env["APIGEE_API_SECRET_ARN"] as string,
jwtKid: process.env["jwtKid"] as string,
apigeeMockTokenEndpoint: process.env["apigeeMockTokenEndpoint"] as string,
apigeeCis2TokenEndpoint: process.env["apigeeCIS2TokenEndpoint"] as string,
cloudfrontDomain: process.env["FULL_CLOUDFRONT_DOMAIN"] as string
}
}
const refreshTokenFlow = async (
{
axiosInstance,
ddbClient,
authOptions,
logger
}: AuthDependencies,
apigeeTokenEndpoint: string,
specifiedTokenTable: string,
username: string,
existingToken: ApigeeTokenResponse,
lastActivityTime: number
): Promise<ApigeeTokenResponse> => {
if (existingToken.refreshToken === undefined) {
throw new Error("Missing refresh token")
}
const apigeeApiKey = await getSecret(authOptions.apigeeApiKeyArn)
const apigeeApiSecret = await getSecret(authOptions.apigeeApiSecretArn)
if (!apigeeApiKey || !apigeeApiSecret) {
throw new Error("Missing Apigee API credentials")
}
const refreshResult = await refreshApigeeAccessToken(
axiosInstance,
apigeeTokenEndpoint,
existingToken.refreshToken,
apigeeApiKey.toString(),
apigeeApiSecret.toString(),
logger
)
// Update DynamoDB with the new tokens
await updateTokenMapping(
ddbClient,
specifiedTokenTable,
{
username,
apigeeAccessToken: refreshResult.accessToken,
apigeeExpiresIn: refreshResult.expiresIn,
apigeeRefreshToken: refreshResult.refreshToken,
lastActivityTime: lastActivityTime
},
logger
)
logger.info("Successfully refreshed tokens")
return refreshResult
}
const fifteenMinutes = 15 * 60 * 1000
/**
* Authenticates a request and handles the entire authentication flow including token refresh.
*
* @param event - API Gateway proxy event
* @param documentClient - DynamoDB document client
* @param logger - Logger instance
* @param options - Authentication options
* @returns Authentication result containing tokens and metadata
*/
export async function authenticateRequest(
username: string,
{
axiosInstance,
ddbClient,
authOptions,
logger
}: AuthDependencies,
userRecord: TokenMappingItem,
specifiedTokenTable: string,
disableLastActivityUpdate: boolean
): Promise<AuthResult | AuthTimeoutResult | null> {
const {
jwtPrivateKeyArn,
apigeeApiKeyArn,
apigeeApiSecretArn,
jwtKid,
apigeeMockTokenEndpoint,
apigeeCis2TokenEndpoint,
cloudfrontDomain
} = authOptions
const apigeeApiKey = await getSecret(apigeeApiKeyArn)
const apigeeApiSecret = await getSecret(apigeeApiSecretArn)
if (!apigeeApiKey || !apigeeApiSecret) {
throw new Error("Missing Apigee API credentials")
}
logger.info("Starting authentication flow")
// Extract username and determine if this is a mock request
const isMockRequest = username.startsWith("Mock_")
// If disableTokenRefresh is true, we won't update lastActivityTime
const lastActivityTime = disableLastActivityUpdate ? userRecord.lastActivityTime : Date.now()
if (Date.now() - userRecord.lastActivityTime > fifteenMinutes) {
logger.info("Last activity was more than 15 minutes ago, clearing user record")
await deleteTokenMapping(
ddbClient,
specifiedTokenTable,
username,
logger
)
return {isTimeout: true}
}
// If token exists, check if we need to refresh it
if (userRecord.apigeeAccessToken) {
const currentTime = Math.floor(Date.now() / 1000)
if (userRecord.apigeeExpiresIn === undefined) {
throw new Error("Missing apigee expires in time")
}
const timeUntilExpiry = userRecord.apigeeExpiresIn - currentTime
// If token is expired or will expire soon (within 60 seconds), refresh it
if (timeUntilExpiry <= 60) {
logger.info("Token needs refresh, initiating token refresh flow")
try {
const refreshedToken = await refreshTokenFlow(
{
axiosInstance,
ddbClient,
authOptions,
logger
},
isMockRequest ? apigeeMockTokenEndpoint : apigeeCis2TokenEndpoint,
specifiedTokenTable,
username,
{
accessToken: userRecord.apigeeAccessToken,
refreshToken: userRecord.apigeeRefreshToken,
expiresIn: userRecord.apigeeExpiresIn
},
lastActivityTime
)
return {
username,
apigeeAccessToken: refreshedToken.accessToken,
roleId: userRecord.currentlySelectedRole?.role_id,
orgCode: userRecord.currentlySelectedRole?.org_code
}
} catch (error) {
logger.warn("Token refresh failed, will proceed with new token acquisition", {error})
// Continue to the token acquisition flow if refresh fails
}
} else {
// Token is still valid, use it
logger.info("Using existing valid token", {
timeUntilExpiry,
isMockRequest
})
// Don't update last activity time if token refresh is disabled
await updateTokenMapping(
ddbClient,
specifiedTokenTable,
{username, lastActivityTime: lastActivityTime},
logger
)
return {
username,
apigeeAccessToken: userRecord.apigeeAccessToken,
roleId: userRecord.currentlySelectedRole?.role_id,
orgCode: userRecord.currentlySelectedRole?.org_code
}
}
}
// No valid existing token found or refresh failed, need to acquire a new one
logger.info(`Obtaining tokens through ${isMockRequest ? "mock" : "standard"} flow`)
let exchangeResult
if (isMockRequest) {
// for mock request we need to call apigee userinfo endpoint
// we also need to include the callback url registered for our apigee app
// for pull requests, this is the domain name for the environment (without -pr-XXX)
const baseEnvironmentDomain = cloudfrontDomain.replace(/-pr-(\d*)/, "")
const callbackUri = `https://${baseEnvironmentDomain}/oauth2/mock-callback`
const tokenExchangeBody = {
grant_type: "authorization_code",
client_id: apigeeApiKey.toString(),
client_secret: apigeeApiSecret.toString(),
redirect_uri: callbackUri,
code: userRecord.apigeeCode
}
exchangeResult = await exchangeTokenForApigeeAccessToken(
axiosInstance,
apigeeMockTokenEndpoint,
tokenExchangeBody,
logger
)
} else {
// When we aren't mocking, get CIS2 tokens and exchange for Apigee token
if (userRecord.cis2IdToken === undefined) {
throw new Error("Missing cis2IdToken")
}
await verifyIdToken(userRecord.cis2IdToken, logger)
// Fetch the private key for signing the client assertion
logger.info("Fetching JWT private key from Secrets Manager", {jwtPrivateKeyArn})
const jwtPrivateKey = await getSecret(jwtPrivateKeyArn)
if (!jwtPrivateKey || typeof jwtPrivateKey !== "string") {
throw new Error("Invalid or missing JWT private key")
}
// Construct a new body with the signed JWT client assertion
const requestBody = constructSignedJWTBody(
logger,
apigeeCis2TokenEndpoint,
jwtPrivateKey,
apigeeApiKey.toString(),
jwtKid,
userRecord.cis2IdToken
)
// Exchange token with Apigee
exchangeResult = await exchangeTokenForApigeeAccessToken(
axiosInstance,
apigeeCis2TokenEndpoint,
requestBody,
logger
)
}
// Update DynamoDB with the new Apigee access token
await updateTokenMapping(
ddbClient,
specifiedTokenTable,
{
username,
apigeeAccessToken: exchangeResult.accessToken,
apigeeRefreshToken: exchangeResult.refreshToken,
apigeeExpiresIn: exchangeResult.expiresIn,
lastActivityTime: lastActivityTime
},
logger
)
// Validate that we have the tokens we need
if (!exchangeResult.accessToken) {
throw new Error("Failed to obtain required tokens after authentication flow")
}
logger.info("Authentication flow completed successfully")
logger.debug("Authentication result", {
authResult: {
accessToken: exchangeResult.accessToken,
refreshToken: exchangeResult.refreshToken,
expiresIn: exchangeResult.expiresIn
}
})
// at this point we may or may not have a selected role
return {
username,
apigeeAccessToken: exchangeResult.accessToken,
roleId: userRecord.currentlySelectedRole?.role_id,
orgCode: userRecord.currentlySelectedRole?.org_code
}
}