Skip to content
This repository was archived by the owner on Feb 26, 2020. It is now read-only.

Commit 152a071

Browse files
committed
Doc cleanup
1 parent 17761f9 commit 152a071

4 files changed

Lines changed: 54 additions & 82 deletions

File tree

README.md

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,22 @@
11
#Windows Azure Active Directory Sample REST API Service for Node.js using MongoDB and Restify
22

3-
This Node.js server will give you with a quick and easy way to set up a REST API Service that's integrated with Windows Azure Active Directory for API protection using the OAuth2 protocol using bearer tokens. The sample server included in the download are designed to run on any platform.
3+
This Node.js server will give you with a quick and easy way to set up a REST API Service that's integrated with Azure Active Directory for API protection using the OAuth2 protocol with bearer tokens. The sample server included in the download are designed to run on any platform.
44

55
This REST API server is built using Restify and MongoDB with the following features:
66

77
* A node.js server running an REST API interface with JSON using MongoDB as persistent storage
88
* REST APIs leveraging OAuth2 API protection for endpoints using Windows Azure Active Directory
99

10-
[Refer to our Wiki](https://github.com/AzureADSamples/WebAPI-Nodejs/wiki) for detailed walkthroughs on how to use this server.
11-
1210
We've released all of the source code for this example in GitHub under an Apache 2.0 license, so feel free to clone (or even better, fork!) and provide feedback on the forums.
1311

14-
## How to Use The Service
15-
16-
This is a simple TODO Server that takes JSON requests through REST and responds with the appropriate JSON objects.
17-
18-
#### To use this without Authentication (for testing the endpoints without Authentication)
19-
20-
$ node server.js
21-
22-
$ curl -isS http://127.0.0.1:8888 | json
23-
HTTP/1.1 200 OK
24-
Connection: close
25-
Content-Type: application/x-www-form-urlencoded
26-
Content-Length: 145
27-
Date: Wed, 29 Jan 2014 03:41:24 GMT
28-
29-
[
30-
"GET /",
31-
"POST /tasks/:name/:task",
32-
"POST /tasks (for JSON body)",
33-
"GET /tasks",
34-
"DELETE /tasks",
35-
"PUT /tasks/:name",
36-
"GET /tasks/:name",
37-
"DELETE /tasks/:task"
38-
]
39-
40-
#### To invoke with OAuth2 Authentication (for use with Windows Azure AD)
41-
42-
$ node server.js -m oauth2
4312

4413
## Quick Start
4514

4615
Getting started with the sample is easy. It is configured to run out of the box with minimal setup.
4716

4817
### Step 1: Register a Windows Azure AD Tenant
4918

50-
To use this sample you will need a Windows Azure Active Directory Tenant. If you're not sure what a tenant is or how you would get one, read [What is a Windows Azure AD tenant](http://technet.microsoft.com/library/jj573650.aspx)? or [Sign up for Windows Azure as an organization](http://azure.microsoft.com/en-us/documentation/articles/sign-up-organization/). These docs should get you started on your way to using Windows Azure AD.
19+
To use this sample you will need a Windows Azure Active Directory Tenant. If you're not sure what a tenant is or how you would get one, read [What is an Azure AD tenant](http://technet.microsoft.com/library/jj573650.aspx)? or [Sign up for Azure as an organization](http://azure.microsoft.com/en-us/documentation/articles/sign-up-organization/). These docs should get you started on your way to using Windows Azure AD.
5120

5221
### Step 2: Register your Web API with your Windows Azure AD Tenant
5322

@@ -73,7 +42,7 @@ Next, clone the sample repo and install the NPM.
7342

7443
From your shell or command line:
7544

76-
* `$ git clone git@github.com:WindowsAzureAD/Azure-AD-TODO-Server-Sample-For-Node.git`
45+
* `$ git clone git@github.com:AzureADSamples/WebAPI-Nodejs.git`
7746
* `$ npm install`
7847

7948
### Step 6: Run the application
@@ -92,6 +61,7 @@ We would like to acknowledge the folks who own/contribute to the following proje
9261
- [Restify](http://mcavage.me/node-restify/) - Restify is a node.js module built specifically to enable you to build correct REST web services. ``` node-restify```
9362
- [Restify-OAuth2](https://github.com/domenic/restify-oauth2) - This package provides a very simple OAuth 2.0 endpoint for the Restify framework. ``` restify-oauth2```
9463
- [node-jwt-simple](https://github.com/hokaccha/node-jwt-simple) - Library for parsing JSON Web Tokens (JWT) ```node-jwt-simple```
64+
- [http-bearer-strategy](https://github.com/jaredhanson/passport-http-bearer) - HTTP Bearer authentication strategy for Passport and Node.js.
9565

9666

9767

node-server/config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Don't commit this file to your public repos
1+
// Don't commit this file to your public repos. This config is for first-run
22
exports.creds = {
33
mongoose_auth_local: 'mongodb://localhost/tasklist', // Your mongo auth uri goes here
44
openid_configuration: 'https://login.microsoftonline.com/hypercubeb2c.onmicrosoft.com/.well-known/openid-configuration?p=b2c_1_B2CSI', // For using Microsoft you should never need to change this.

node-server/lib/oidc_strategy.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) Microsoft Open Technologies, Inc.
2+
Copyright (c) Microsoft Corporation
33
All Rights Reserved
44
Apache License 2.0
55
@@ -59,7 +59,7 @@ var log = bunyan.createLogger({name: 'Microsoft OpenID Connect: Passport Strateg
5959
*
6060
* Examples:
6161
*
62-
* passport.use(new JwtBearerStrategy(
62+
* passport.use(new OIDCBearerStrategy(
6363
* secretOrPublicKey
6464
* function(token, done) {
6565
* User.findById(token.sub, function (err, user) {
@@ -135,10 +135,12 @@ var decoded = jws.decode(token);
135135
done(null, false, "Invalid JWT token.");
136136
}
137137

138-
log.info(decoded, 'was token decrypted. But is it valid?');
138+
log.info('token decoded. But is it valid?: ', decoded);
139139

140140

141-
// We have two different types of token signatures we have to validate here. One provides x5t and the other a kid. We need to call the right one.
141+
// We have two different types of token signatures we have to validate here. One provides x5t and the other a kid.
142+
// We need to call the right one.
143+
142144
if (decoded.header.x5t) {
143145
var PEMkey = this.metadata.generateOidcPEM(decoded.header.x5t);
144146
}

node-server/server.js

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (c) Microsoft Open Technologies, Inc.
2+
Copyright (c) Microsoft Corporation
33
All Rights Reserved
44
Apache License 2.0
55
@@ -32,6 +32,9 @@
3232
var getopt = require('posix-getopt');
3333
var config = require('./config');
3434

35+
36+
// We pass these options in to the ODICBearerStrategy.
37+
3538
var options = {
3639
// The URL of the metadata document for your app. We will put the keys for token validation from the URL found in the jwks_uri tag of the in the metadata.
3740
metadataurl: config.creds.openid_configuration
@@ -54,49 +57,36 @@
5457

5558

5659
// MongoDB setup
57-
58-
/**
59-
* Setup some configuration
60-
*/
61-
60+
// Setup some configuration
6261
var serverPort = process.env.PORT || 8888;
6362
var serverURI = (process.env.PORT) ? config.creds.mongoose_auth_mongohq : config.creds.mongoose_auth_local;
6463

65-
66-
/**
67-
*
68-
* Connect to MongoDB
69-
*/
70-
64+
// Connect to MongoDB
7165
global.db = mongoose.connect(serverURI);
7266
var Schema = mongoose.Schema;
7367
log.info('MongoDB Schema loaded');
7468

75-
76-
/**
77-
/ Here we create a schema to store our tasks and users. Pretty simple schema for now.
78-
*/
79-
69+
// Here we create a schema to store our tasks and users. Pretty simple schema for now.
8070
var TaskSchema = new Schema({
8171
owner: String,
8272
task: String,
8373
completed: Boolean,
8474
date: Date
8575
});
8676

87-
8877
// Use the schema to register a model
89-
9078
mongoose.model('Task', TaskSchema);
9179
var Task = mongoose.model('Task');
9280

9381

9482

9583
/**
9684
*
97-
* APIs
85+
* APIs for our REST Task server
9886
*/
9987

88+
// Create a task
89+
10090
function createTask(req, res, next) {
10191

10292
// Resitify currently has a bug which doesn't allow you to set default headers
@@ -133,9 +123,8 @@ function createTask(req, res, next) {
133123
}
134124

135125

136-
/**
137-
* Deletes a Task by name
138-
*/
126+
// Delete a task by name
127+
139128
function removeTask(req, res, next) {
140129

141130
Task.remove( { task:req.params.task }, function (err) {
@@ -151,21 +140,17 @@ function removeTask(req, res, next) {
151140
});
152141
}
153142

154-
/**
155-
* Deletes all Tasks. A wipe
156-
*/
143+
// Delete all tasks
144+
157145
function removeAll(req, res, next) {
158146
Task.remove();
159147
res.send(204);
160148
return next();
161149
}
162150

163151

164-
/**
165-
*
166-
*
167-
*
168-
*/
152+
// Get a specific task based on name
153+
169154
function getTask(req, res, next) {
170155

171156
log.info('getTask was called');
@@ -182,12 +167,8 @@ function getTask(req, res, next) {
182167
return next();
183168
}
184169

185-
186-
/**
187-
* Simple returns the list of TODOs that were loaded.
188-
*
189-
*/
190-
170+
/// Simple returns the list of TODOs that were loaded.
171+
191172
function listTasks(req, res, next) {
192173
// Resitify currently has a bug which doesn't allow you to set default headers
193174
// This headers comply with CORS and allow us to mongodbServer our response to any origin
@@ -272,7 +253,7 @@ util.inherits(TaskNotFoundError, restify.RestError);
272253

273254
var server = restify.createServer({
274255
name: "Windows Azure Active Directroy TODO Server",
275-
version: "1.0.0"
256+
version: "2.0.1"
276257
});
277258

278259
// Ensure we don't drop data on uploads
@@ -299,15 +280,23 @@ var server = restify.createServer({
299280
server.use(restify.dateParser());
300281
server.use(restify.queryParser());
301282
server.use(restify.gzipResponse());
302-
server.use(restify.bodyParser({ mapParams: true}));
303-
server.use(restify.authorizationParser());
283+
server.use(restify.bodyParser({ mapParams: true})); // Allows for JSON mapping to REST
284+
server.use(restify.authorizationParser()); // Looks for authorization headers
304285

305286
// Let's start using Passport.js
306287

307-
server.use(passport.initialize());
308-
server.use(passport.session());
288+
server.use(passport.initialize()); // Starts passport
289+
server.use(passport.session()); // Provides session support
309290

310-
// Passport stuff
291+
/**
292+
/*
293+
/* Calling the OIDCBearerStrategy and managing users
294+
/*
295+
/* Passport pattern provides the need to manage users and info tokens
296+
/* with a FindorCreate() method that must be provided by the implementor.
297+
/* Here we just autoregister any user and implement a FindById().
298+
/* You'll want to do something smarter.
299+
**/
311300

312301
var findById = function (id, fn) {
313302
for (var i = 0, len = users.length; i < len; i++) {
@@ -342,6 +331,17 @@ var server = restify.createServer({
342331

343332
/// Now the real handlers. Here we just CRUD
344333

334+
/**
335+
/*
336+
/* Each of these handlers are protected by our OIDCBearerStrategy by invoking 'oidc-bearer'
337+
/* in the pasport.authenticate() method. We set 'session: false' as REST is stateless and
338+
/* we don't need to maintain session state. You can experiement removing API protection
339+
/* by removing the passport.authenticate() method like so:
340+
/*
341+
/* server.get('/tasks', listTasks);
342+
/*
343+
**/
344+
345345
server.get('/tasks', passport.authenticate('oidc-bearer', { session: false }), listTasks);
346346
server.get('/tasks', passport.authenticate('oidc-bearer', { session: false }), listTasks);
347347
server.get('/tasks/:owner', passport.authenticate('oidc-bearer', { session: false }), getTask);

0 commit comments

Comments
 (0)