Skip to content

Commit 3220af1

Browse files
author
Rob McGuinness
committedNov 30, 2017
h2o2 and hapi-auth-basic updates
1 parent 81d3c43 commit 3220af1

File tree

7 files changed

+272
-460
lines changed

7 files changed

+272
-460
lines changed
 

‎lib/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const schema = Joi.object({
3434
deReference: Joi.boolean(),
3535
validatorUrl: Joi.string().allow(null),
3636
acceptToProduce: Joi.boolean(),
37-
connectionLabel: Joi.array().items(Joi.string()).single().allow(null),
3837
cors: Joi.boolean(),
3938
pathReplacements: Joi.array().items(Joi.object({
4039
replaceIn: Joi.string().valid(['groups', 'endpoints', 'all']),
@@ -51,7 +50,7 @@ const schema = Joi.object({
5150
* @param {Function} next
5251
*/
5352
exports.plugin = {
54-
register: async (server, options) => {
53+
register: (server, options) => {
5554

5655
let settings = Hoek.applyToDefaults(Defaults, options, true);
5756
const publicDirPath = Path.resolve(__dirname, '..', 'public');

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
"good": "^7.3.0",
3838
"good-console": "^6.1.2",
3939
"good-squeeze": "^5.0.2",
40-
"h2o2": "^6.0.1",
40+
"h2o2": "^7.0.0",
4141
"hapi": "^17.0.2",
4242
"hapi-api-version": "^1.4.0",
43-
"hapi-auth-basic": "^4.2.0",
43+
"hapi-auth-basic": "^5.0.0",
4444
"hapi-auth-bearer-token": "^6.0.1",
4545
"hapi-auth-jwt2": "^7.3.0",
4646
"inert": "^5.0.1",

‎test/Integration/authentication-test.js

+78-113
Original file line numberDiff line numberDiff line change
@@ -1,84 +1,70 @@
1-
'use strict';
21
const Code = require('code');
32
const Joi = require('joi');
43
const Lab = require('lab');
54
const Helper = require('../helper.js');
5+
const Validate = require('../../lib/validate.js');
66

77
const expect = Code.expect;
88
const lab = exports.lab = Lab.script();
99

1010

1111
lab.experiment('default `auth` settings', () => {
12-
const routes = [
13-
{
14-
method: 'GET',
15-
path: '/',
16-
options: {
17-
auth: false,
18-
handler: function (request, reply) {
19-
20-
reply({ text: 'Token not required' });
21-
}
22-
}
23-
}, {
24-
method: 'GET',
25-
path: '/restricted',
26-
options: {
27-
auth: 'jwt',
28-
tags: ['api'],
29-
plugins: {
30-
'hapi-swagger': {
31-
security: [{ 'jwt': [] }]
32-
}
33-
},
34-
handler: function (request, reply) {
35-
36-
reply({ text: 'You used a Token! ' + request.auth.credentials.name })
37-
.header('Authorization', request.headers.authorization);
38-
}
39-
}
40-
}
41-
];
42-
43-
44-
lab.test('get documentation page should not be restricted', (done) => {
45-
46-
const requestOptions = {
47-
method: 'GET',
48-
url: '/documentation'
49-
};
50-
51-
Helper.createJWTAuthServer({}, routes, (err, server) => {
52-
53-
server.inject(requestOptions, function (response) {
54-
55-
expect(err).to.equal(null);
56-
//console.log(JSON.stringify(response.result));
57-
expect(response.statusCode).to.equal(200);
58-
done();
59-
});
60-
});
61-
});
62-
63-
64-
lab.test('get documentation page should be restricted 401', (done) => {
65-
66-
const requestOptions = {
67-
method: 'GET',
68-
url: '/documentation'
69-
};
70-
71-
Helper.createJWTAuthServer({ auth: undefined }, routes, (err, server) => {
72-
73-
server.inject(requestOptions, function (response) {
74-
75-
expect(err).to.equal(null);
76-
//console.log(JSON.stringify(response.result));
77-
expect(response.statusCode).to.equal(401);
78-
done();
79-
});
80-
});
81-
});
12+
// const routes = [
13+
// {
14+
// method: 'GET',
15+
// path: '/',
16+
// options: {
17+
// auth: false,
18+
// handler: function (request, reply) {
19+
20+
// reply({ text: 'Token not required' });
21+
// }
22+
// }
23+
// }, {
24+
// method: 'GET',
25+
// path: '/restricted',
26+
// options: {
27+
// auth: 'jwt',
28+
// tags: ['api'],
29+
// plugins: {
30+
// 'hapi-swagger': {
31+
// security: [{ 'jwt': [] }]
32+
// }
33+
// },
34+
// handler: function (request, reply) {
35+
36+
// reply({ text: 'You used a Token! ' + request.auth.credentials.name })
37+
// .header('Authorization', request.headers.authorization);
38+
// }
39+
// }
40+
// }
41+
// ];
42+
43+
44+
// lab.test('get documentation page should not be restricted', async() => {
45+
46+
// const requestOptions = {
47+
// method: 'GET',
48+
// url: '/documentation'
49+
// };
50+
51+
// const server = await Helper.createJWTAuthServer({}, routes);
52+
// const response = await server.inject(requestOptions);
53+
// expect(response.statusCode).to.equal(200);
54+
// });
55+
56+
57+
// lab.test('get documentation page should be restricted 401', async() => {
58+
59+
// const requestOptions = {
60+
// method: 'GET',
61+
// url: '/documentation'
62+
// };
63+
64+
// const server = await Helper.createJWTAuthServer({ auth: undefined });
65+
// const response = await server.inject(requestOptions);
66+
// expect(response.statusCode).to.equal(401);
67+
// });
8268

8369

8470
});
@@ -99,9 +85,7 @@ lab.experiment('authentication', () => {
9985
}
10086
},
10187
tags: ['api'],
102-
auth: {
103-
strategies: ['bearer']
104-
},
88+
auth: 'bearer',
10589
validate: {
10690
headers: Joi.object({
10791
authorization: Joi.string()
@@ -119,7 +103,7 @@ lab.experiment('authentication', () => {
119103
};
120104

121105

122-
lab.test('get plug-in interface with bearer token', (done) => {
106+
lab.test('get plug-in interface with bearer token', async() => {
123107

124108
const requestOptions = {
125109
method: 'GET',
@@ -129,41 +113,34 @@ lab.experiment('authentication', () => {
129113
}
130114
};
131115

132-
Helper.createAuthServer({}, routes, (err, server) => {
116+
const server = await Helper.createAuthServer({}, routes);
117+
const response = await server.inject(requestOptions);
133118

134-
server.inject(requestOptions, function (response) {
119+
expect(response.statusCode).to.equal(200);
120+
const isValid = await Validate.test(response.result);
121+
expect(isValid).to.be.true();
135122

136-
expect(err).to.equal(null);
137-
//console.log(JSON.stringify(response.result));
138-
expect(response.statusCode).to.equal(200);
139-
Helper.validate(response, done, expect);
140-
});
141-
});
142123
});
143124

144125

145-
lab.test('get plug-in interface without bearer token', (done) => {
126+
lab.test('get plug-in interface without bearer token', async() => {
146127

147128
const requestOptions = {
148129
method: 'GET',
149130
url: '/swagger.json'
150131
};
151132

152133
// plugin routes should be not be affected by auth on API
153-
Helper.createAuthServer({}, routes, (err, server) => {
154-
155-
server.inject(requestOptions, function (response) {
134+
const server = await Helper.createAuthServer({}, routes);
135+
const response = await server.inject(requestOptions);
136+
expect(response.statusCode).to.equal(200);
137+
const isValid = await Validate.test(response.result);
138+
expect(isValid).to.be.true();
156139

157-
expect(err).to.equal(null);
158-
//console.log(JSON.stringify(response.result));
159-
expect(response.statusCode).to.equal(200);
160-
Helper.validate(response, done, expect);
161-
});
162-
});
163140
});
164141

165142

166-
lab.test('get API interface with bearer token', (done) => {
143+
lab.test('get API interface with bearer token', async() => {
167144

168145
const requestOptions = {
169146
method: 'POST',
@@ -176,20 +153,14 @@ lab.experiment('authentication', () => {
176153
}
177154
};
178155

179-
Helper.createAuthServer({}, routes, (err, server) => {
156+
const server = await Helper.createAuthServer({}, routes);
157+
const response = await server.inject(requestOptions);
158+
expect(response.statusCode).to.equal(200);
180159

181-
server.inject(requestOptions, function (response) {
182-
183-
expect(err).to.equal(null);
184-
//console.log(JSON.stringify(response.result));
185-
expect(response.statusCode).to.equal(200);
186-
done();
187-
});
188-
});
189160
});
190161

191162

192-
lab.test('get API interface with incorrect bearer token', (done) => {
163+
lab.test('get API interface with incorrect bearer token', async() => {
193164

194165
const requestOptions = {
195166
method: 'POST',
@@ -202,16 +173,10 @@ lab.experiment('authentication', () => {
202173
}
203174
};
204175

205-
Helper.createAuthServer({}, routes, (err, server) => {
206-
207-
server.inject(requestOptions, function (response) {
176+
const server = await Helper.createAuthServer({}, routes);
177+
const response = await server.inject(requestOptions);
178+
expect(response.statusCode).to.equal(401);
208179

209-
expect(err).to.equal(null);
210-
//console.log(JSON.stringify(response.result));
211-
expect(response.statusCode).to.equal(401);
212-
done();
213-
});
214-
});
215180
});
216181

217182

‎test/Integration/plugin-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ lab.experiment('plugin', () => {
214214
expect(response.result.paths['/store/'].post.parameters[0].name).to.equal('body');
215215
});
216216

217-
lab.test('disable cors settings, should return headers without origin settings', async(done) => {
217+
lab.test('disable cors settings, should return headers without origin settings', async() => {
218218

219219
const swaggerOptions = {
220220
'cors': false

0 commit comments

Comments
 (0)
Please sign in to comment.