forked from AltusConsulting/Spark-Broadcast-Bot
-
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.
Added JWT authentication to all endpoints of the backend
- Loading branch information
Rafael Campos
committed
Nov 4, 2017
1 parent
aecce88
commit c35910b
Showing
8 changed files
with
207 additions
and
49 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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
function Auth(configuration) { | ||
var controller = { | ||
config: configuration | ||
}; | ||
|
||
var request = require('request'); | ||
var jwt = require('jsonwebtoken'); | ||
|
||
controller.createAuthEndpoints = function(webserver, bot, cb) { | ||
webserver.get('/auth/token', function(req, res) { | ||
|
||
var body = { | ||
grant_type: "authorization_code", | ||
client_id: controller.config.client_id, | ||
client_secret: controller.config.client_secret, | ||
code: req.query.code, | ||
redirect_uri: controller.config.redirect_uri | ||
} | ||
|
||
request({ | ||
method: "POST", | ||
json: true, | ||
uri: "https://api.ciscospark.com/v1/access_token", | ||
json: body | ||
}, function(error, response, body) { | ||
|
||
if (!error && response.statusCode == 200) { | ||
|
||
request({ | ||
method: "GET", | ||
json: true, | ||
uri: "https://api.ciscospark.com/v1/people/me", | ||
headers: { | ||
'Authorization': 'Bearer ' + body.access_token | ||
} | ||
}, function(error, response, body) { | ||
|
||
var user_email = body.emails[0]; | ||
|
||
//if (user_email == controller.config.allowed_admin) { | ||
if (true) { | ||
var jwt_token = jwt.sign(user_email, controller.config.token_secret); | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.send(JSON.stringify({ 'status': 'success', 'access_token': jwt_token })); | ||
} else { | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.status(400).send(JSON.stringify({ 'status': 'error', 'error': "User is not an admin" })); | ||
} | ||
}); | ||
|
||
} else { | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.status(400).send(JSON.stringify({ 'status': 'error', 'error': body.errors })); | ||
} | ||
|
||
}); | ||
|
||
}); | ||
|
||
if (cb) cb(); | ||
}; | ||
|
||
return controller; | ||
} | ||
|
||
module.exports = Auth; |
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,9 +1,11 @@ | ||
var Notifications = require(__dirname + '/notifications.js'); | ||
var Topics = require(__dirname + '/topics.js'); | ||
var Messages = require(__dirname + '/messages.js'); | ||
var Auth = require(__dirname + '/auth.js'); | ||
|
||
module.exports = { | ||
notifications: Notifications, | ||
topics: Topics, | ||
messages: Messages | ||
messages: Messages, | ||
auth: Auth | ||
}; |
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
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