This repository has been archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
auth.js
65 lines (53 loc) · 1.8 KB
/
auth.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
'use strict';
import config from './config.json';
import crypto from 'crypto';
import cookie from 'cookie';
import request from 'request-promise';
import { shopifyIsValid } from './shopify/utilities';
import AWS from 'aws-sdk';
const cognitoidentity = new AWS.CognitoIdentity();
const apiKey = config.SHOPIFY_API_KEY;
const apiSecret = config.SHOPIFY_API_SECRET;
const scopes = config.SCOPES;
const forwardingAddress = config.BASE_URL;
module.exports.cognito = (event, context, callback) => {
context.callbackWaitsForEmptyLoop = false;
let response;
const shopParams = JSON.parse(event.body);
if (shopifyIsValid(shopParams, true, apiSecret)) {
// build our params
const cognitoParams = {
IdentityPoolId: 'us-east-1:6f495654-5560-46ef-b09b-a00ea882a542', /* required us-east-1_wDf8fU7RT */
// IdentityId: 'us-east-1:6f495654-5560-46ef-b09b-a00ea882a542',
Logins: { /* required */
'shopify-developer-provider': shopParams.shop,
/* '<IdentityProviderName>': ... */
},
TokenDuration: 86400
};
// get a token from cognito
cognitoidentity.getOpenIdTokenForDeveloperIdentity(cognitoParams, (err, data) => {
if (err) console.log(err, err.stack); // an error occurred
response = {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin" : "*", // Required for CORS support to work
"Access-Control-Allow-Credentials" : true // Required for cookies, authorization headers with HTTPS
},
body: JSON.stringify({
message: 'App it up baby',
cognito: data
})
}
callback(null, response);
});
} else {
response = {
statusCode: 400,
body: JSON.stringify({
message: 'HMAC validation failed'
})
}
callback(null, response);
}
};