diff --git a/package.json b/package.json index 7608cc22..00004f75 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "tenso", "description": "Tensō is an elastic REST API gateway for node.js", - "version": "4.1.9", + "version": "4.2.0", "homepage": "http://avoidwork.github.io/tenso", "author": "Jason Mulligan ", "repository": { diff --git a/sample.js b/sample.js index f4e2223a..604998b2 100644 --- a/sample.js +++ b/sample.js @@ -1,5 +1,3 @@ -var jwt = require("jsonwebtoken"); - require("./index.js")({ port: 8000, routes: require("./test/routes.js"), @@ -10,7 +8,7 @@ require("./index.js")({ }, auth: { local: { - enabled: true, + enabled: false, auth: function (username, password, callback) { if (username === "test" && password === 123) { callback(null, {username: username, password: password}); @@ -19,6 +17,13 @@ require("./index.js")({ } } }, + jwt: { + enabled: true, + auth: function (token, cb) { + cb(null, token); + }, + secretOrKey: "jennifer" + }, protect: ["/uuid"] }, security: { diff --git a/test/auth_test.js b/test/auth_test.js index 0beb0b00..55048769 100644 --- a/test/auth_test.js +++ b/test/auth_test.js @@ -1,5 +1,6 @@ var hippie = require("hippie"), emitter = require("events"), + jwt = require("jsonwebtoken"), tenso = require("../index"), routes = require("./routes.js"), array = require("retsu"), @@ -344,3 +345,58 @@ describe("Local", function () { }); }); }); + +describe("JWT", function () { + var port = 8012, + secret = "jennifer", + token = jwt.sign({username: "jason@attack.io"}, secret); + + tenso({ + port: port, + routes: routes, + logging: {level: "error"}, + auth: { + jwt: { + enabled: true, + auth: function (token, cb) { + if (token.username === 'jason@attack.io') { + cb(null, token); + } else { + cb(new Error('Invalid token'), null); + } + }, + secretOrKey: secret + }, + security: { + csrf: false + }, + protect: ["/uuid"] + } + }); + + this.timeout(5000); + + it("GET /uuid - returns a uuid (authorized)", function (done) { + api(port, false) + .header('Authorization', 'Bearer ' + token) + .get("/uuid") + .expectStatus(200) + .expectValue("links", [{uri: "/", rel: "collection"}]) + .expectValue("error", null) + .expectValue("status", 200) + .end(function (err) { + if (err) throw err; + done(); + }); + }); + + it("GET /uuid - returns an 'unauthorized' error", function (done) { + api(port, true) + .get("/uuid") + .expectStatus(401) + .end(function (err) { + if (err) throw err; + done(); + }); + }); +}); \ No newline at end of file