Node.js use Passport with Bearer Strategy
I'm going to demonstrate how to use Bearer Strategy and jsonwebtoken in this post. The workflow is shown as below picture. The client need to login first and get the token from the server. Then the client request another API service while embedded the token in the http request header. The server verify the token and respond the result to the client. The jsonwebtoken can encode a little information into the token, like user id and username (those isn't sensitive data). The jsonwebtoken can decode back the information on server side. We do not use Session to store in this example because the token also acts as authentication check.
Starting with the code using passport with LocalStrategy and custom callback(v1.2), we first install the Bearer Strategy and token generator module.
npm install passport-http-bearer
npm install jsonwebtoken
Secondly, we import the modules and setup simply for test. I just set the expired time as 1 day and hard code the secret key used for jsonwebtoken generation. In real case, we need more configuration and keep the secret key confidential.
Thirdly, we register the BearerStrategy vertification to passport module.
We will generate the token only when the user logins successfully. We call jwt.sign() to generate the token and send back to client.
We can test with Postman (POST /login) and receive the token from server response.
Then we create 2 new route GET /user1 and GET /user2 to test the Bearer Strategy default callback manner and our custom callback. The default manner will put the decoded data to req.user, similar to load user information to req.user from Session with Local Strategy.
Default Manner
Now we test these 2 routes with Postman. First we login to get the token. Then we click the Authorization tab below the path in Postman. Select Bearer Token type and paste the token we got on the right.
We test the default manner first. (GET /user1) and get an object back from the server. It contains name and id property, which we had passed to jwt.sign() function (1st parameter). If we changed the last 2 characters of the token and visit the path again, the server return a html file with message invalid signature.
Custom callback
Now we test the customer callback (GET /user2). If we leave the blank token column or set the Type as no Auth, the server returns the message that we set when decodedData is false.
If we type in wrong token, the server returns a string of message indicating the error occurred during the jwt decryption. If we pass the correct token, the server returns the user name and id to us as the default manner did.We add 1 line console.log(req.user) on the server side. The terminal show "undefined". That means we have to mount the user information(decoded data) to req.user by ourself if we want the custom callback behave as the default manner.
The source code(v3.1) of this post is stored in github,
We can find more detail on the bearer strategy and jsonwebtoken website.
Note:
There are some potential security issues if we use Bearer token only. For example, if someone steal our token and request the api service, the server cannot cancel the validity of the token until the expiration date. We need extra tools/technique to help to tackle those security issues. This will be another topic.










Comments
Post a Comment