Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit aa6bc82

Browse files
authored
Merge pull request #1 from topcoder-platform/issue-125
For #125: AWS handshake
2 parents 7886348 + 3761437 commit aa6bc82

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

DLPTrigger/handlers/options.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as aws from 'aws-lambda'
2+
import { createHash } from 'crypto'
3+
4+
/**
5+
* Handles HTTP get event.
6+
* @param event
7+
* @param context
8+
*/
9+
export default async function handleGetRequest(event: aws.APIGatewayEvent, context: aws.Context) {
10+
const handshakeRequestData = event.headers['x-handshake-request-data']
11+
if (!handshakeRequestData) {
12+
return {}
13+
}
14+
const digest = createHash('sha256').update(handshakeRequestData).digest('hex')
15+
const responseHeaders = { 'x-handshake-response-data': digest }
16+
return responseHeaders
17+
}

DLPTrigger/lambda.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import handleGetRequest from './handlers/get'
22
import * as aws from 'aws-lambda'
33
import handlePostRequest from './handlers/post'
4+
import handleOptionsRequest from './handlers/options'
45

56
/**
67
* HTTP headers to be always returned to the client.
78
*/
89
const commonHeaders = {
910
'Access-Control-Allow-Credentials': 'true',
1011
'Access-Control-Allow-Origin': '*',
11-
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, HANDSHAKE',
12+
'Access-Control-Allow-Methods': 'GET, POST, OPTIONS',
1213
'Access-Control-Allow-Headers': '*',
1314
'Access-Control-Max-Age': '86400',
1415
// eslint-disable-next-line quote-props
@@ -25,6 +26,10 @@ export async function handle (event: aws.APIGatewayEvent, context: aws.Context)
2526
const headers = commonHeaders
2627
let statusCode = 200
2728
let body: any = null
29+
// Store lower-case version of the headers
30+
for (const key in event.headers) {
31+
event.headers[key.toLowerCase()] = event.headers[key];
32+
}
2833

2934
try {
3035
switch (event.httpMethod) {
@@ -37,7 +42,8 @@ export async function handle (event: aws.APIGatewayEvent, context: aws.Context)
3742
break
3843
}
3944
case 'OPTIONS':
40-
// Nothing to do for OPTIONS method.
45+
const additionalHeaders = await handleOptionsRequest(event, context)
46+
Object.assign(headers, additionalHeaders)
4147
break
4248
default:
4349
statusCode = 405

0 commit comments

Comments
 (0)