Skip to content

Commit 0952f08

Browse files
committedApr 8, 2015
Add integration test to test authorization
expecting some failures since we haven't implemented the Lua script change yet
1 parent 9129b5a commit 0952f08

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
 

‎hosts/backend/server.js

+8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ app.get('/secure', function (req, res) {
1616
});
1717
});
1818

19+
app.get('/secure/admin', function (req, res) {
20+
console.log('Authorization header:', req.get('Authorization'));
21+
22+
res.json({
23+
message: 'This endpoint needs to be secure for an admin.'
24+
});
25+
});
26+
1927
var server = app.listen(5000, function () {
2028
var host = server.address().address;
2129
var port = server.address().port;

‎hosts/proxy/normal-secret/nginx/conf/nginx.conf

+9
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,14 @@ http {
2323

2424
proxy_pass http://backend_host:5000/secure;
2525
}
26+
27+
location /secure/admin {
28+
access_by_lua '
29+
local jwt = require("nginx-jwt")
30+
jwt.auth({roles="admin"})
31+
';
32+
33+
proxy_pass http://backend_host:5000/secure/admin;
34+
}
2635
}
2736
}

‎test/test_integration.js

+31
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,37 @@ describe('proxy', function () {
5656
.end();
5757
});
5858
});
59+
60+
describe("GET /secure/admin", function () {
61+
it("should return 401 when an authenticated user still isn't authorized via claims", function () {
62+
var token = jwt.sign(
63+
{ sub: 'foo-user', roles: 'customer' },
64+
secret
65+
);
66+
67+
return request(url)
68+
.get('/secure/admin')
69+
.headers({'Authorization': 'Bearer ' + token})
70+
.expect(401)
71+
.end();
72+
});
73+
74+
it("should return 200 when an authenticated user is authorized via claims", function () {
75+
var token = jwt.sign(
76+
{ sub: 'foo-user', roles: 'customer admin' },
77+
secret
78+
);
79+
80+
return request(url)
81+
.get('/secure/admin')
82+
.headers({'Authorization': 'Bearer ' + token})
83+
.expect(200)
84+
.expect('Content-Type', /json/)
85+
.expect({ message: 'This endpoint needs to be secure for an admin.' })
86+
.expect('X-Auth-UserId', 'foo-user')
87+
.end();
88+
});
89+
});
5990
});
6091

6192
describe("configured with URL-safe base-64 encoded secret", function () {

0 commit comments

Comments
 (0)
Please sign in to comment.