Last active
May 25, 2017 04:24
-
-
Save sapher/522a9fea5b7c55f51d5a8766e566e3c6 to your computer and use it in GitHub Desktop.
Revisions
-
sapher revised this gist
May 25, 2017 . 1 changed file with 0 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,14 +11,9 @@ const createEndpoint = (platformAppArn, token, callback) => { SnsClient.createPlatformEndpoint(createParams, (err, data) => { if(err) { return callback(err); } else { const endpointArn = data.EndpointArn; const res = { -
sapher created this gist
May 25, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,113 @@ const AWS = require('aws-sdk'); const SnsClient = new AWS.SNS(); const createEndpoint = (platformAppArn, token, callback) => { const createParams = { PlatformApplicationArn: platformAppArn, Token: token }; SnsClient.createPlatformEndpoint(createParams, (err, data) => { if(err) { console.log("err"); console.log(err); return callback(err); } else { console.log("data"); console.log(data.EndpointArn); const endpointArn = data.EndpointArn; const res = { statusCode: 200, headers: {}, body: JSON.stringify({ app_token : endpointArn }) }; return callback(null, res); } }); }; exports.handler = (event, context, callback) => { // Application ARN is passed as ENVironnement variable const appArn = process.env.SNS_APP_ARN; // Get body from request let body = event["body"]; // parse local data if(typeof body === "string") { body = JSON.parse(body); } // Retrieve useful data const refreshToken = body['refresh_token']; let appToken = body['app_token']; // easier const endpointArn = appToken; // First time registration, create endpoint if(!appToken) { createEndpoint(appArn, refreshToken, callback); } // Already created else { // Get attributes SnsClient.getEndpointAttributes({ EndpointArn: endpointArn }, (err, data) => { // not found or issue, assume we need to recreate if(err) { createEndpoint(appArn, refreshToken, callback); } // endpoint found else { const update = data.Attributes.Token !== refreshToken || data.Attributes.Enabled !== true; // need update if(update) { const updateParams = { EndpointArn: endpointArn, Attributes: { Token: refreshToken, Enabled: "true" } }; SnsClient.setEndpointAttributes(updateParams, (err, data) => { if(err) return callback(err); else { const res = { statusCode: 200, headers: {}, body: JSON.stringify({}) }; return callback(null, res); } }); } // no update needed, we can go else { const res = { statusCode: 200, headers: {}, body: JSON.stringify({}) }; return callback(null, res); } } }); } };