A.1. An Introduction to Our Code Framework

Throughout this book, we’ll be developing applications in JavaScript using the Express.js [ 1 ] web application framework running on Node.js, [ 2 ] a server-side JavaScript engine. Although the examples themselves will be written in JavaScript, all concepts in the examples should be readily portable to other platforms and application frameworks. Wherever possible, we’ve tried to keep the specific quirks of JavaScript (such as closures and function callbacks) away from the bits of code that you’ll be working with directly, since the goal of this book is not to make you a proficient JavaScript programmer. We also will be making use of library code for non-OAuth-specific functionality in these examples so that you can focus on what is the core goal of this book: understanding in detail how the OAuth protocol works.

1

http://expressjs.com/

2

https://nodejs.org/

In a real application, it may be desirable to use an OAuth library to handle many of the functions that we’ll be coding by hand here. However, in this book, we’ll be building things up by hand so that you can get hands on with the OAuth functionality without getting sidetracked by the specifics of the Node.js application. All of the code found in this book is available on GitHub [ 3 ] and from the publisher’s site for this book. [ 4 ] Each exercise is in a separate directory, sorted by chapter number and example number.

3

https://github.com/oauthinaction/oauth-in-action-code/

4

https://www.manning.com/books/oauth-2-in-action

Let’s get started. First, before you’ll be able to run anything, you’ll need to install Node.js and the Node Package Manager (NPM) for your platform. The details vary from system to system; for example, on a MacOSX system running MacPorts, this can be installed with the following commands:

> sudo port install node
> sudo port install npm

You can verify that these have been properly installed by asking each for their version number, which will print out something like the following messages:

> node -v
v14.13.1
> npm -v
6.14.8

With these core libraries installed, we can unpack our example code. Enter the ap-A-ex-0 directory and run the npm install command to install the project dependencies for this example. This action pulls down the dependencies for this example and installs them into the node_modules directory. The npm program will detail all of the packages being installed automatically to satisfy the dependencies for this project, and the output will look something like this:

ap-A-ex-0> npm install
[email protected] node_modules/underscore

[email protected] node_modules/body-parser
    [email protected]
    [email protected]
There's a lot of information that will print to your console here; we're not going to copy everything.

    [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])
    [email protected] ([email protected], [email protected])
    [email protected] ([email protected], [email protected])

Having done that, you should now have a directory that contains all of the code required for this example.

Note

The npm install step must be run separately for every exercise.

Each of the exercises contains three JavaScript source files: client.js , authorizationServer.js , and protectedResource.js , along with other support files and libraries to make things run. Each of these needs to be run separately using the node command, and we suggest running each in a different terminal window to avoid confusing the log files. The order of starting them up does not matter, but they do have to be running at the same time in order for most example programs to work.

For example, running the client application should produce output like the following in the terminal window:

> node client.js
OAuth Client is listening at http://127.0.0.1:9000

The authorization server is launched like this:

> node authorizationServer.js
OAuth Authorization Server is listening at http://127.0.0.1:9001

And the protected resource is launched like this:

> node protectedResource.js
OAuth Protected Resource is listening at http://127.0.0.1:9002

We recommend running all three in separate terminal windows so that you can see the output of the programs as they run. See figure A.1 .

Figure A.1. Three terminal windows running each component in parallel

Each component is set up to run on a different port on localhost , in a separate process:

All of the applications have been set up to serve static files such as images and Cascading Style Sheets (CSS). These are included in the files directory in the project and won’t need to be edited for any of the exercises. In addition, there are HTML templates in the files directory. These are used in the applications to generate HTML pages based on variable inputs. When templates are used, they’re set up at the beginning of the application with the following code:

app.engine('html', cons.underscore);
app.set('view engine', 'html');
app.set('views', 'files');

The templates won’t need to be edited during the exercises, but they will occasionally be reviewed to showcase functionality. We’re using the Underscore.js [ 5 ] templating system along with the Consolidate.js [ 6 ] library to create and manage all of the templates in all of the examples. You can pass variables to these templates and have them render their output using the render call on the response object, like this:

5

http://underscorejs.org/

6

https://github.com/tj/consolidate.js

res.render('index', {access_token: access_token});

The three code files in this first example contain no actual functionality, but if you can see the welcome page for each then you’ll know that the applications are running correctly and the dependencies have been installed. For example, visiting the OAuth Client’s URL http://localhost:9000/ in a web browser on your machine should produce what you see in figure A.2 .

Figure A.2. The homepage for the client shell

Likewise, the authorization server on http://localhost:9001/ will look like what you see in figure A.3 .

Figure A.3. The homepage for the authorization server shell

And finally, the protected resource on http://localhost:9002/ will display what is shown in figure A.4 (note that the protected resource doesn’t usually have a user-facing component).

Figure A.4. The homepage for the protected resource shell

To add HTTP handlers to our applications, we need to add them as routes to the Express.js application object. In each route, we tell the application which HTTP methods to listen for, which URL patterns to listen for, and which function to call when these conditions are matched. The function is passed a request object and a response object as parameters. For instance, this example listens for HTTP GET requests on /foo and calls the given anonymous function.

app.get('/foo', function (req, res) {

});

We’ll be following the convention of referring to the request object as req and the response object as res throughout our exercises. The request object contains information about the incoming HTTP request, including headers, URL, query parameters, and other things that correspond to the incoming request. The response object is used to send information back in the HTTP response, including a status code, headers, a response body, and more.

We’ll be storing a lot of stateful information in global variables, declared at the top of the respective file. In any reasonable web application, all of these pieces of state would be tied to the user’s session instead of global variables in the application itself. A native application is likely to use an approach similar to our framework and rely on the features of the host operating system to provide local user session authentication.

You’ll use this simple framework throughout the book to build OAuth clients, protected resources, and authorization servers. For the most part, each exercise will come set up with things nearly complete, and you’ll be required only to fill in the small bits of OAuth-related functionality that we’re discussing in a given exercise.

For every exercise, you’ll be able to find the completed code in the completed directory under the exercise directory. If you get stuck, we recommend opening up these files to take a look at how the “official” answer works.

And finally, even though the code from the exercises is compliant with all of the standards, we implore you not to use it in a production system.