Skip to content

Commit

Permalink
feat: Add support for hapi 18 (#397)
Browse files Browse the repository at this point in the history
This PR includes two fixes related to [hapi 18](hapijs/hapi#3871):

1. Many tests broke because hapi now strips the default port from `request.info.host` (a side effect of using the WHATWG URL API) – causing lots of assertions to fail. I decided to use a non-default port in the tests instead of removing the default port from the assertions. This verifies that port info is still propagated correctly.
2. `request.url.query` is no longer available. I’ve changed it to `request.query` which works for hapi 18 and older versions. (I’ve tested it with hapi 17 and 18). This fixes #394.
  • Loading branch information
rluba authored and AdriVanHoudt committed Feb 27, 2019
1 parent b4fe168 commit 5c28ac3
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 101 deletions.
2 changes: 1 addition & 1 deletion lib/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ internals.refreshRedirect = function (request, name, protocol, settings, credent
return h.unauthenticated(Boom.internal('Missing ' + name + ' request token cookie'), { credentials });
}

const refreshQuery = Object.assign({}, request.url.query, { refresh: 1 });
const refreshQuery = Object.assign({}, request.query, { refresh: 1 });
const refreshUrl = internals.location(request, protocol, settings.location) + '?' + internals.queryString(refreshQuery);
return h.response(`<html><head><meta http-equiv="refresh" content="0;URL='${refreshUrl}'"></head><body></body></html>`).takeover();
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
"devDependencies": {
"code": "5.x.x",
"hapi": "17.x.x",
"hapi": "18.x.x",
"hawk": "7.x.x",
"lab": "18.x.x",
"teamwork": "3.x.x"
Expand Down
38 changes: 19 additions & 19 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Bell', () => {
it('authenticates an endpoint via oauth', async (flags) => {

const mock = await Mock.v1(flags);
const server = Hapi.server({ host: 'localhost', port: 80 });
const server = Hapi.server({ host: 'localhost', port: 8080 });
await server.register(Bell);

server.auth.strategy('custom', 'bell', {
Expand Down Expand Up @@ -53,7 +53,7 @@ describe('Bell', () => {
expect(res1.headers.location).to.equal(mock.uri + '/auth?oauth_token=1');

const res2 = await mock.server.inject(res1.headers.location);
expect(res2.headers.location).to.equal('http://localhost:80/login?oauth_token=1&oauth_verifier=123');
expect(res2.headers.location).to.equal('http://localhost:8080/login?oauth_token=1&oauth_verifier=123');

const res3 = await server.inject({ url: res2.headers.location, headers: { cookie } });
expect(res3.result.provider).to.equal('custom');
Expand All @@ -63,7 +63,7 @@ describe('Bell', () => {
it('authenticates an endpoint via oauth using RSA-SHA1 signing', async (flags) => {

const mock = await Mock.v1(flags, { signatureMethod: 'RSA-SHA1' });
const server = Hapi.server({ host: 'localhost', port: 80 });
const server = Hapi.server({ host: 'localhost', port: 8080 });
await server.register(Bell);

server.auth.strategy('custom', 'bell', {
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('Bell', () => {
expect(res1.headers.location).to.equal(mock.uri + '/auth?oauth_token=1');

const res2 = await mock.server.inject(res1.headers.location);
expect(res2.headers.location).to.equal('http://localhost:80/login?oauth_token=1&oauth_verifier=123');
expect(res2.headers.location).to.equal('http://localhost:8080/login?oauth_token=1&oauth_verifier=123');

const res3 = await server.inject({ url: res2.headers.location, headers: { cookie } });
expect(res3.result.provider).to.equal('custom');
Expand All @@ -101,7 +101,7 @@ describe('Bell', () => {
it('authenticates an endpoint via oauth2', async (flags) => {

const mock = await Mock.v2(flags);
const server = Hapi.server({ host: 'localhost', port: 80 });
const server = Hapi.server({ host: 'localhost', port: 8080 });
await server.register(Bell);

server.auth.strategy('custom', 'bell', {
Expand All @@ -125,11 +125,11 @@ describe('Bell', () => {
});

const res1 = await server.inject('/login');
expect(res1.headers.location).to.contain(mock.uri + '/auth?client_id=test&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A80%2Flogin&state=');
expect(res1.headers.location).to.contain(mock.uri + '/auth?client_id=test&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Flogin&state=');
const cookie = res1.headers['set-cookie'][0].split(';')[0] + ';';

const res2 = await mock.server.inject(res1.headers.location);
expect(res2.headers.location).to.contain('http://localhost:80/login?code=1&state=');
expect(res2.headers.location).to.contain('http://localhost:8080/login?code=1&state=');

const res3 = await server.inject({ url: res2.headers.location, headers: { cookie } });
expect(res3.result.provider).to.equal('custom');
Expand All @@ -138,7 +138,7 @@ describe('Bell', () => {
it('authenticates an endpoint via oauth2 and basic authentication', async (flags) => {

const mock = await Mock.v2(flags, { useParamsAuth: false });
const server = Hapi.server({ host: 'localhost', port: 80 });
const server = Hapi.server({ host: 'localhost', port: 8080 });
await server.register(Bell);

server.auth.strategy('custom', 'bell', {
Expand All @@ -163,10 +163,10 @@ describe('Bell', () => {

const res1 = await server.inject('/login');
const cookie = res1.headers['set-cookie'][0].split(';')[0] + ';';
expect(res1.headers.location).to.contain(mock.uri + '/auth?client_id=test&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A80%2Flogin&state=');
expect(res1.headers.location).to.contain(mock.uri + '/auth?client_id=test&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Flogin&state=');

const res2 = await mock.server.inject(res1.headers.location);
expect(res2.headers.location).to.contain('http://localhost:80/login?code=1&state=');
expect(res2.headers.location).to.contain('http://localhost:8080/login?code=1&state=');

const res3 = await server.inject({ url: res2.headers.location, headers: { cookie } });
expect(res3.result.provider).to.equal('custom');
Expand All @@ -175,7 +175,7 @@ describe('Bell', () => {
it('authenticates an endpoint via oauth2 with custom client secret options', async (flags) => {

const mock = await Mock.v2(flags, false);
const server = Hapi.server({ host: 'localhost', port: 80 });
const server = Hapi.server({ host: 'localhost', port: 8080 });
await server.register(Bell);

server.auth.strategy('custom', 'bell', {
Expand All @@ -200,10 +200,10 @@ describe('Bell', () => {

const res1 = await server.inject('/login');
const cookie = res1.headers['set-cookie'][0].split(';')[0] + ';';
expect(res1.headers.location).to.contain(mock.uri + '/auth?client_id=customSecret&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A80%2Flogin&state=');
expect(res1.headers.location).to.contain(mock.uri + '/auth?client_id=customSecret&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Flogin&state=');

const res2 = await mock.server.inject(res1.headers.location);
expect(res2.headers.location).to.contain('http://localhost:80/login?code=1&state=');
expect(res2.headers.location).to.contain('http://localhost:8080/login?code=1&state=');

const res3 = await server.inject({ url: res2.headers.location, headers: { cookie } });
expect(res3.result.provider).to.equal('custom');
Expand All @@ -213,7 +213,7 @@ describe('Bell', () => {
it('authenticates an endpoint via oauth2 with custom client secret options and params auth', async (flags) => {

const mock = await Mock.v2(flags, true); // Sets useParamsAuth = true
const server = Hapi.server({ host: 'localhost', port: 80 });
const server = Hapi.server({ host: 'localhost', port: 8080 });
await server.register(Bell);

server.auth.strategy('custom', 'bell', {
Expand All @@ -238,10 +238,10 @@ describe('Bell', () => {

const res1 = await server.inject('/login');
const cookie = res1.headers['set-cookie'][0].split(';')[0] + ';';
expect(res1.headers.location).to.contain(mock.uri + '/auth?client_id=customSecret&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A80%2Flogin&state=');
expect(res1.headers.location).to.contain(mock.uri + '/auth?client_id=customSecret&response_type=code&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Flogin&state=');

const res2 = await mock.server.inject(res1.headers.location);
expect(res2.headers.location).to.contain('http://localhost:80/login?code=1&state=');
expect(res2.headers.location).to.contain('http://localhost:8080/login?code=1&state=');

const res3 = await server.inject({ url: res2.headers.location, headers: { cookie } });
expect(res3.result.provider).to.equal('custom');
Expand All @@ -251,7 +251,7 @@ describe('Bell', () => {
it('overrides cookie name', async (flags) => {

const mock = await Mock.v1(flags);
const server = Hapi.server({ host: 'localhost', port: 80 });
const server = Hapi.server({ host: 'localhost', port: 8080 });
await server.register(Bell);

server.auth.strategy('custom', 'bell', {
Expand Down Expand Up @@ -282,7 +282,7 @@ describe('Bell', () => {
it('allows multiple custom provider names', async (flags) => {

const mock = await Mock.v1(flags);
const server = Hapi.server({ host: 'localhost', port: 80 });
const server = Hapi.server({ host: 'localhost', port: 8080 });
await server.register(Bell);

server.auth.strategy('custom_1', 'bell', {
Expand Down Expand Up @@ -341,7 +341,7 @@ describe('Bell', () => {

it('exposes OAuth via plugin', async () => {

const server = Hapi.server({ host: 'localhost', port: 80 });
const server = Hapi.server({ host: 'localhost', port: 8080 });
await server.register(Bell);

expect(server.plugins.bell.oauth.Client).to.be.function();
Expand Down
Loading

0 comments on commit 5c28ac3

Please sign in to comment.