Created
February 25, 2016 01:38
-
-
Save khle/23d8616c0eee2a5fd44f to your computer and use it in GitHub Desktop.
server.js
This file contains hidden or 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
| var express = require('express'), | |
| app = express(), | |
| fs = require('fs'), | |
| aad = require('azure-ad-jwt'); | |
| app.get('/books', function(req, res) { | |
| var audience = 'http://samplewebservice.azure-mobile.net' | |
| var authorization = req.headers['authorization'] | |
| if (authorization) { | |
| var bearer = authorization.split(" "); | |
| var jwtToken = bearer[1]; | |
| if (jwtToken) { | |
| aad.verify(jwtToken, { audience: audience}, function(err, result) { | |
| if (result) { | |
| fs.readFile(__dirname + "/" + "_data.json", 'utf8', function(err, data) { | |
| res.status(200).send(data) | |
| }); | |
| } else { | |
| res.status(401).send('no valid token') | |
| } | |
| }) | |
| } else { | |
| res.status(401).send('no token in header') | |
| } | |
| } else { | |
| res.status(401).send('no auth attr in header') | |
| } | |
| }) | |
| var server = app.listen(8080) | |
| console.log('Server listening on port 8080') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment