Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions lambda/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ let putCertinfo = ( iotcert, values,callback ) => {
Key:{
"serialNumber": values
},
UpdateExpression: "set certinfo = :r",
UpdateExpression: "set certinfo = :r, isActivated = :t",
ExpressionAttributeValues:{
":r": iotcert
":r": iotcert,
":t": 1
},
ReturnValues:"UPDATED_NEW"
}, (err, data) => {
Expand Down
39 changes: 22 additions & 17 deletions lambda/index.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
/**
/**
Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
This node.js Lambda function code creates certificate, attaches an IoT policy, IoT thing .
It also activates the certificate.
This node.js Lambda function code creates certificate, attaches an IoT policy, IoT thing .
It also activates the certificate.
**/
const config = require('./config');
const applyModel = require("app");

/*
/*
You should submit your device credentials to Lambda function through API Gateway for authenticating in DynamoDB.
eg: {"serialNumber":"YOUR_DEVICE_SERIAL_NUMBER","deviceToken":"TOKEN"}
*/
exports.handler = (event, context, callback) => {

console.log("EVENT: " + JSON.stringify(event));

const DYNAMODB_ERROR = 'Service error: 500!';
const Device_ERROR = 'Access Deny!';
const INTERLNAL_ERROR = 'Identical serial number error!';
const GET_ROOT_CA_ERROR = 'Can not get Get VeriSign Class 3 Public Primary G5 root CA certificate! ';

const ALREADY_ACTIVATED = 'Device is already activated.';
const Device_DISABLED = 'Device is disabled. Enable it to activate.';

// Get device credentials
var serialNumber = event.serialNumber;
var deviceToken = event.deviceToken;

// Verify device legality
var verifyDevice = applyModel.findDataBySerialNumber( serialNumber, ( err,data ) => {

if( err ) {
console.log( err );
callback( null , DYNAMODB_ERROR );
Expand All @@ -36,21 +38,24 @@ exports.handler = (event, context, callback) => {
}
// You should replace equipment certificate according to demand in production.
else if ( data.Count == 1) {


if(data.Items[0].isActivated==1) { callback(null, ALREADY_ACTIVATED ); }
if(data.Items[0].isEnabled==0) { callback(null, Device_DISABLED ); }

// then verify Token
if(data.Items[0].deviceToken!=deviceToken) callback(null, Device_ERROR );
else{
// After the verification is complete, you can apply for a certificate for the device.
applyModel.applycert( serialNumber, ( err, certData ) => {

// In order to be safe, you should write the certificate ID/Arn, indicating that the device has applied for a certificate.
applyModel.putCertinfo( certData.certificateArn, serialNumber, ( err,putSuccess ) => {

if(err) callback( null, INTERLNAL_ERROR );

// Don't forget to return CA certificate
applyModel.getIoTRootCA( ( err,rootca ) => {

if ( err ) {
console.log( err );
callback( null, GET_ROOT_CA_ERROR );
Expand All @@ -61,15 +66,15 @@ exports.handler = (event, context, callback) => {
// Don't forget to return CA certificate
callback(null, returnValues );
})

});
});
}

}
else{
console.log(data);
callback( null, INTERLNAL_ERROR );
}
});
};
};