-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
145 lines (132 loc) · 5.02 KB
/
index.js
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
/* Amplify Params - DO NOT EDIT
You can access the following resource attributes as environment variables from your Lambda function
var environment = process.env.ENV
var region = process.env.REGION
var storageDynamoStoresName = process.env.STORAGE_DYNAMOSTORES_NAME
var storageDynamoStoresArn = process.env.STORAGE_DYNAMOSTORES_ARN
var functionRetrieveSecretName = process.env.FUNCTION_RETRIEVESECRET_NAME
Amplify Params - DO NOT EDIT *//*
this file will loop through all js modules which are uploaded to the lambda resource,
provided that the file names (without extension) are included in the "MODULES" env variable.
"MODULES" is a comma-delimmited string.
*/
const AWS = require('aws-sdk')
const https = require('https')
exports.handler = async (event) => {
console.log({ message: 'Received signup request' })
return new Promise((resolve, reject) => {
try {
let config = {
client_id: process.env.CLIENT_ID,
redirect_url: process.env.REDIRECT_URL
}
const lambdaClient = new AWS.Lambda()
const lambdaParams = {
FunctionName: process.env.FUNCTION_RETRIEVESECRET_NAME,
InvocationType: 'RequestResponse',
Payload: JSON.stringify({})
}
lambdaClient.invoke(lambdaParams, (err, data) => {
if (err) {
console.log(err)
reject(new Error('Error during installation'))
}
else {
let client_secret = data.Payload
// Secret is returned wrapped in double quotes
client_secret = client_secret.substring(1, client_secret.length - 1)
// send the POST request
const body = JSON.stringify({
client_id: config.client_id,
client_secret,
code: event.request.validationData.code,
scope: event.request.validationData.scope,
grant_type: 'authorization_code',
redirect_uri: config.redirect_url,
context: event.request.validationData.context,
})
const options = {
hostname: 'login.bigcommerce.com',
port: 443,
path: '/oauth2/token',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(body)
}
}
const req = https.request(options, res => {
let data = false
res.on('data', chunk => {
data = !data ? chunk : data + chunk
})
res.on('end', () => {
if (res.statusCode == 200) {
// Store the token in DynamoDB:
// Token is base64 encoded. If encryption in transit is required,
// consider creating an encryption Lamda with DynamoDB Encryption Client
data = data.toString()
const jsonData = JSON.parse(data),
encodedToken = Buffer.from(jsonData.access_token).toString('base64'),
dynamoDbClient = new AWS.DynamoDB(),
table = process.env.STORAGE_DYNAMOSTORES_NAME,
item = {
"Hash": {
S: jsonData.context.substring(7),
},
"Token": {
S: encodedToken,
},
"Scope": {
S: event.request.validationData.scope,
},
"Created": {
N: Math.round((new Date()).getTime() / 1000).toString(),
},
"Modified": {
N: Math.round((new Date()).getTime() / 1000).toString(),
},
}
const params = {
TableName: table,
Item: item
}
console.log(`Adding user: ${event.request.userAttributes.name}`)
dynamoDbClient.putItem(params, (err, data) => {
if (err) {
console.error(`Error adding store. Error: ${JSON.stringify(err, null, 2)}`)
reject(new Error('Error during installation'))
}
else {
console.log(`SUCCESS: Added user ${event.request.userAttributes.name}`)
event.response.autoConfirmUser = true
resolve(event)
}
})
}
else {
console.error(`Error adding user: BC responded with ${res.statusCode}
Response body:
${data} `)
reject(err)
}
})
})
req.on('error', err => {
console.error(err)
reject(err)
})
req.write(body)
req.end()
}
const secretsManagerClient = new AWS.SecretsManager()
secretsManagerClient.getSecretValue({ SecretId: process.env.SECRETNAME }, (err, data) => {
})
})
}
catch (err) {
console.error(err)
reject(err)
}
})
}