Skip to content

Instantly share code, notes, and snippets.

@sapher
Last active May 25, 2017 04:24
Show Gist options
  • Select an option

  • Save sapher/522a9fea5b7c55f51d5a8766e566e3c6 to your computer and use it in GitHub Desktop.

Select an option

Save sapher/522a9fea5b7c55f51d5a8766e566e3c6 to your computer and use it in GitHub Desktop.

Revisions

  1. sapher revised this gist May 25, 2017. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions lambda.js
    Original 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) {
    console.log("err");
    console.log(err);
    return callback(err);
    }
    else {
    console.log("data");
    console.log(data.EndpointArn);

    const endpointArn = data.EndpointArn;

    const res = {
  2. sapher created this gist May 25, 2017.
    113 changes: 113 additions & 0 deletions lambda.js
    Original 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);
    }
    }
    });
    }
    };