Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ const fromHapiReq = req => {
exports.plugin = {
pkg: require('./package.json'),
register: async function (server, options) {
await Joi.validate(options, Joi.object().keys({
model: Joi.object().required()
}))

const oauthServer = new OAuth2Server(options)
Joi.assert(options, Joi.object().keys({
model: Joi.alternatives().try(Joi.object().required(), Joi.function())
}));

const model = typeof options.model === 'function' ? options.model(server) : options.model;

const oauthServer = new OAuth2Server({
...options,
model: model
})


const oauth = {
authenticate: async (req, options) => {
Expand All @@ -39,6 +46,7 @@ exports.plugin = {
}
}

server.expose('oauth', oauth)
server.decorate('server', 'oauth', oauth);
server.decorate('request', 'oauth', oauth);
}
}
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@
},
"homepage": "https://github.com/getapper/hapi-oauth-2-server#readme",
"devDependencies": {
"code": "5.2.4",
"hapi": "17.8.4",
"lab": "18.0.2",
"moment": "2.24.0"
"@hapi/code": "8.x.x",
"@hapi/hapi": "20.x.x",
"@hapi/lab": "24.x.x",
"date-fns": "2.21.3"
},
"peerDependencies": {
"hapi": "^17.0.0"
"@hapi/hapi": "^18.x.x"
},
"dependencies": {
"joi": "14.3.1",
"oauth2-server": "3.0.1"
"joi": "17.x.x",
"oauth2-server": "3.1.1"
}
}
57 changes: 53 additions & 4 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
const build = require('./server')
const model = require('./model')
const code = require('code')
const labLib = require('lab')
const model = require('./model');
const modelWithServer = require('./modelWithServer');
const code = require('@hapi/code')
const labLib = require('@hapi/lab')

const lab = (exports.lab = labLib.script())
const describe = lab.describe
const before = lab.before
const it = lab.it
const expect = code.expect

describe('Hapi oauth2 server', () => {
describe('Hapi oauth2 server with object model', () => {
let server
let res

Expand Down Expand Up @@ -43,6 +44,54 @@ describe('Hapi oauth2 server', () => {
expect(res.statusCode, 'Wrong HTTP response status code').to.equal(200)
})

it('should get token', async () => {
res = await server.inject({
method: 'POST',
url: '/token',
headers: {
Authorization: 'Bearer foobar',
'Content-Type': 'application/x-www-form-urlencoded'
},
payload: 'client_id=test&client_secret=test&grant_type=authorization_code&code=test'
})
expect(res.statusCode, 'Wrong HTTP response status code').to.equal(200)
})
});

describe('Hapi oauth2 server with model from server callback', () => {
let server
let res

before(async () => {
server = await build(modelWithServer);
})

it('should fail for no authentication provided', async () => {
res = await server.inject({
method: 'GET',
url: '/authenticate'
})
expect(res.statusCode, 'Wrong HTTP response status code').to.equal(401)
})

it('should authenticate', async () => {
res = await server.inject({
method: 'GET',
url: '/authenticate',
headers: {Authorization: 'Bearer foobar'}
})
expect(res.statusCode, 'Wrong HTTP response status code').to.equal(200)
})

it('should authorize', async () => {
res = await server.inject({
method: 'GET',
url: '/authorize?client_id=test&client_secret=test&state=test&response_type=code',
headers: {Authorization: 'Bearer foobar'},
})
expect(res.statusCode, 'Wrong HTTP response status code').to.equal(200)
})

it('should get token', async () => {
res = await server.inject({
method: 'POST',
Expand Down
7 changes: 4 additions & 3 deletions test/model.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
const moment = require('moment')
const add = require('date-fns/add');
const toDate = require('date-fns/toDate');

module.exports = {
getAccessToken: async () => {
return { user: {}, accessTokenExpiresAt: moment().add(3, 'days').toDate() }
return { user: {}, accessTokenExpiresAt: toDate(add(new Date(), {days: 3}))}
},
getAuthorizationCode: async code => {
return {
code: 'be6e365e9f29c19e631078e8e376326bdf086576',
expiresAt: moment().add(3, 'days').toDate(),
expiresAt: toDate(add(new Date(), {days: 3})),
scope: 'test',
client: { id: 'test' },
user: {}
Expand Down
52 changes: 52 additions & 0 deletions test/modelWithServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const add = require('date-fns/add');
const toDate = require('date-fns/toDate');

module.exports = (server) => {
return {
getAccessToken: async () => {

return { user: {}, accessTokenExpiresAt: toDate(add(new Date(), { days: 3 })) }
},
getAuthorizationCode: async code => {
return {
code: 'be6e365e9f29c19e631078e8e376326bdf086576',
expiresAt: toDate(add(new Date(), { days: 3 })),
scope: 'test',
client: { id: 'test' },
user: {}
};
},
getClient: async (clientId, clientSecret) => {
return {
id: clientId,
clientSecret,
redirectUris: ['/ok'],
grants: [
"password",
"authorization_code",
"refresh_token"
]
}
},
saveAuthorizationCode: async (code, client, user) => {
return {
...code,
client: { id: 'test' },
user: { id: user.userId }
}
},
saveToken: async (token, client, user) => {
return {
...token,
client: { id: 'test' },
user: {}
}
},
getUser: async (username, password) => {
return { user: {} }
},
revokeAuthorizationCode: async code => {
return true
}
}
}
8 changes: 4 additions & 4 deletions test/server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Hapi = require('hapi')
const Hapi = require('@hapi/hapi')
const HapiOAuth2Server = require('../')

module.exports = async model => {
Expand All @@ -19,7 +19,7 @@ module.exports = async model => {
path: '/authenticate',
config: {
handler: async (req, h) => {
const { oauth } = req.server.plugins['hapi-oauth2-server-plugin']
const { oauth } = req.server
try {
return await oauth.authenticate(req)
} catch (e) {
Expand All @@ -34,7 +34,7 @@ module.exports = async model => {
path: '/authorize',
config: {
handler: async (req, h) => {
const { oauth } = req.server.plugins['hapi-oauth2-server-plugin']
const { oauth } = req.server
try {
return await oauth.authorize(req)
} catch (e) {
Expand All @@ -49,7 +49,7 @@ module.exports = async model => {
path: '/token',
config: {
handler: async (req, h) => {
const { oauth } = req.server.plugins['hapi-oauth2-server-plugin']
const { oauth } = req.server
try {
return await oauth.token(req)
} catch (e) {
Expand Down