-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Serverless] Success to get user info, finally! #7
- Loading branch information
1 parent
c4aeec9
commit eb55b8f
Showing
2 changed files
with
27 additions
and
38 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,28 @@ | ||
'use strict'; | ||
|
||
var jwt = require('jsonwebtoken'); | ||
var request = require('request'); | ||
|
||
exports.handler = function(event, context, callback){ | ||
if (!event.authToken) { | ||
callback('Could not find authToken'); | ||
return; | ||
if (!event.authToken) { | ||
callback('Could not find authToken'); | ||
return; | ||
} | ||
|
||
var options = { | ||
url: 'https://'+ process.env.DOMAIN + '/userinfo', | ||
method: 'GET', | ||
json: true, | ||
headers: { | ||
'Authorization': event.authToken | ||
} | ||
}; | ||
|
||
var token = event.authToken.split(' ')[1]; | ||
|
||
var secretBuffer = new Buffer(process.env.AUTH0_SECRET); | ||
jwt.verify(token, secretBuffer, function(err, decoded){ | ||
if(err){ | ||
console.log('Failed jwt verification: ', err, 'auth: ', event.authToken); | ||
callback('Authorization Failed'); | ||
} else { | ||
|
||
var body = { | ||
'id_token': token | ||
}; | ||
|
||
var options = { | ||
url: 'https://'+ process.env.DOMAIN + '/tokeninfo', | ||
method: 'POST', | ||
json: true, | ||
body: body | ||
}; | ||
|
||
request(options, function(error, response, body){ | ||
if (!error && response.statusCode === 200) { | ||
callback(null, body); | ||
} else { | ||
callback(error); | ||
} | ||
}); | ||
} | ||
}) | ||
request(options, function(error, response, body){ | ||
if (!error && response.statusCode === 200) { | ||
console.log('Success to get user info.'); | ||
callback(null, body); | ||
} else { | ||
callback(error); | ||
} | ||
}); | ||
}; |