Permalink
Cannot retrieve contributors at this time
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
44 lines (37 sloc)
1.44 KB
This file contains 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 characters
| const AccessToken = Twilio.jwt.AccessToken; | |
| const SyncGrant = AccessToken.SyncGrant; | |
| function userAuth(context, username, pincode) { | |
| var pincodes = JSON.parse(context.USER_PINCODES); | |
| return pincodes[username] === pincode; | |
| } | |
| exports.handler = function(context, event, callback) { | |
| let username = event.username; | |
| let pincode = event.pincode; | |
| if (!username) return callback(null, { success: false, error: "username is not defined in event" }); | |
| if (!pincode) return callback(null, { success: false, error: "pincode is not defined in event" }); | |
| if (!userAuth(context, username, pincode)) return callback(null, { success: false, error: "username or token provided is invalid" }); | |
| // Create a "grant" which enables a client to use Sync as a given user, | |
| // on a given device | |
| let syncGrant = new SyncGrant({ | |
| serviceSid: context.SERVICE_SID | |
| }); | |
| // Create an access token which we will sign and return to the client, | |
| // containing the grant we just created | |
| let token = new AccessToken( | |
| context.ACCOUNT_SID, | |
| context.API_KEY, | |
| context.API_SECRET, { | |
| ttl : parseInt(context.TOKEN_TTL) // int and string are different for AccessToken | |
| } | |
| ); | |
| token.addGrant(syncGrant); | |
| token.identity = username; | |
| // Serialize the token to a JWT string and include it in a JSON response | |
| callback(null, { | |
| success: true, | |
| username: username, | |
| service_sid: context.SERVICE_SID, | |
| ttl: context.TOKEN_TTL, | |
| token: token.toJwt() | |
| }); | |
| }; |