WebFund 2014W: Tutorial 4
In this tutorial we will be playing with a program that is similar to last tutorial's sessions demo except that we now:
- authenticate the user with a password,
- secure communication using https (using a self-signed SSL certificate), and
- have persistence across server restarts.
Because we are using SSL, you will need to connect to https://localhost:3000 rather than the standard http address. You will also get a warning about the self-signed certificate; this is normal. However, you may want to try examining the certificate to see what information it contains.
The sample express application is auth-ssl-demo.
If you were doing authentication in a real application, you should probably use a more mature solution like everyauth or Passport; however, this solution does follow standard practice of storing the password in a form that is (somewhat) hard to reverse (hashed and salted) and we are only transmitting it over an encrypted channel.
You should get the application running, look at the code, and then attempt to answer the questions below about the code and make the suggested modifications.
Running the code outside of the class VM
Note that if you are running this code outside of the class VM, you will probably need to delete the node_modules directory and run npm install as some of the modules for this class use native code. In particular, this code uses OpenSSL's implementation of bcrypt.
npm install should work fine on Linux as they often include OpenSSL as part of the standard development environment. Thus building this on Windows machines can be tricky, however, as OpenSSL is not installed normally; you'll have to install it separately in addition to Visual Studio. See the node bcrypt package documentation for more information on how to get it to run on Windows.
You will get full credit for this tutorial for attending and showing a TA that you can at least answer a few of the questions below. You are highly encouraged, though, to try and answer all of the following during tutorial.
- What is the difference between the Login and Register button on the initial screen? Do they work the same way?
- Generate your own SSL certificate for the application. How do you know you succeeded?
- MongoDB's "tables" are collections; they are grouped together into databases. What MongoDB database is used by this application? What collections?
- How long before this app's session cookies expire?
- Do sessions and user accounts persist across web application restarts?
- Once the application is running successfully, kill the MongoDB server and see how the application behaves when you attempt to register a new user. Does it "succeed" or does it report an error? Is the user properly registered? (You can stop and start the server in the VM using the command sudo service mongodb stop and sudo service mongodb start, respectively.)
- In the POST function for /login, it processes a username and password supplied by the user. How are they accessed? Where did this information come from? And, are they validated in any way?
- Why are there three arguments to the app.get()'s, rather than the previous two?
- How can you change this app to list all of the currently logged in users on /users?
- The routes.register() has multiple nested functions. What do each of them do, and why are they nested the way they are?
- What is toArray() doing in the calls to find()? Specifically, what does the syntax mean, and why is the toArray() call necessary?
- Type mongo to connect to the running mongodb instance on your machine. What do the following commands do? What does this show you about how passwords are stored in this application?
help show dbs show collections use auth-hash-demo db.sessions.find() db.users.find() db.system.indexes.find()