@koopjs/auth-direct-file
Advanced tools
Comparing version 2.0.3 to 3.0.0-alpha.0
{ | ||
"name": "@koopjs/auth-direct-file", | ||
"version": "2.0.3", | ||
"version": "3.0.0-alpha.0", | ||
"description": "Module for implementing a direct authentication pattern with file-based user-store in Koop", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -9,3 +9,2 @@ const fs = require('fs'); | ||
const optionsSchema = joi.object({ | ||
useHttp: joi.boolean().default(false), | ||
tokenExpirationMinutes: joi | ||
@@ -18,3 +17,2 @@ .number() | ||
let _useHttp; | ||
let _tokenExpirationMinutes; | ||
@@ -30,3 +28,2 @@ let _secret; | ||
* @param {integer} options.tokenExpirationMinutes - number of minutes until token expires | ||
* @param {boolean} options.useHttp - direct consumers of authenticationSpecifcation to use HTTP instead of HTTPS | ||
*/ | ||
@@ -44,3 +41,3 @@ function initAuthPlugin(secret, userStoreFilePath, options = {}) { | ||
error, | ||
value: { tokenExpirationMinutes, useHttp }, | ||
value: { tokenExpirationMinutes }, | ||
} = optionsSchema.validate(options); | ||
@@ -52,3 +49,2 @@ | ||
_useHttp = useHttp; | ||
_tokenExpirationMinutes = tokenExpirationMinutes; | ||
@@ -58,3 +54,2 @@ | ||
type: 'auth', | ||
authenticationSpecification, | ||
authenticate, | ||
@@ -65,11 +60,2 @@ authorize, | ||
/** | ||
* Return "authenticationSpecification" object for use in output-services | ||
* @returns {object} | ||
*/ | ||
function authenticationSpecification() { | ||
return { | ||
useHttp: _useHttp, | ||
}; | ||
} | ||
@@ -82,5 +68,13 @@ /** | ||
async function authenticate(req) { | ||
const expires = Date.now() + _tokenExpirationMinutes * 60 * 1000; | ||
const { query = {}, body = {} } = req; | ||
const { username, password } = {...query, ...body}; | ||
const { username, password, token } = { ...query, ...body }; | ||
if (token) { | ||
const { sub } = decodeToken(token); | ||
return { | ||
token: createToken(sub, expires), | ||
expires, | ||
}; | ||
} | ||
@@ -101,8 +95,5 @@ // Validate user's credentials | ||
// Create access token and wrap in response object | ||
const expires = Date.now() + _tokenExpirationMinutes * 60 * 1000; | ||
return { | ||
token: jwt.sign( | ||
{ exp: Math.floor(expires / 1000), sub: username }, | ||
_secret, | ||
), | ||
token: createToken(username, expires), | ||
expires, | ||
@@ -126,2 +117,6 @@ }; | ||
// Verify token with async decoded function | ||
return decodeToken(token); | ||
} | ||
async function decodeToken(token) { | ||
try { | ||
@@ -136,2 +131,6 @@ const decoded = await jwt.verify(token, _secret); | ||
function createToken(sub, expires) { | ||
return jwt.sign({ exp: Math.floor(expires / 1000), sub }, _secret); | ||
} | ||
module.exports = initAuthPlugin; |
@@ -29,16 +29,5 @@ const helpers = require('./validate-credentials'); | ||
} catch (error) { | ||
expect(error.message).toMatch(/^Auth plugin: .+userz-store.json not found$/); | ||
} | ||
}); | ||
test('should fail to initialize due to invalid useHttp option', () => { | ||
try { | ||
require('./index.js')( | ||
secret, | ||
path.join(__dirname, '../test/fixtures/user-store.json'), | ||
{ useHttp: 'boo' } | ||
expect(error.message).toMatch( | ||
/^Auth plugin: .+userz-store.json not found$/, | ||
); | ||
fail('should have thrown'); | ||
} catch (error) { | ||
expect(error.message).toMatch('Auth plugin: "useHttp" must be a boolean'); | ||
} | ||
@@ -48,14 +37,2 @@ }); | ||
describe('authenticationSpecification', () => { | ||
test('should return expected specification', () => { | ||
const authPlugin = require('./index.js')( | ||
secret, | ||
path.join(__dirname, '../test/fixtures/user-store.json'), | ||
{ useHttp: true } | ||
); | ||
const spec = authPlugin.authenticationSpecification(); | ||
expect(spec.useHttp).toEqual(true); | ||
}); | ||
}); | ||
describe('authenticate', () => { | ||
@@ -66,7 +43,8 @@ test('should fail to validate', async () => { | ||
path.join(__dirname, '../test/fixtures/user-store.json'), | ||
{ useHttp: true } | ||
); | ||
try { | ||
await authPlugin.authenticate({ query: { username: 'foo', password: 'bar' } }); | ||
await authPlugin.authenticate({ | ||
query: { username: 'foo', password: 'bar' }, | ||
}); | ||
fail('should have thrown'); | ||
@@ -91,6 +69,7 @@ } catch (error) { | ||
path.join(__dirname, '../test/fixtures/user-store.json'), | ||
{ useHttp: true } | ||
); | ||
const result = await authPlugin.authenticate({ query: { username: 'foo', password: 'bar' } }); | ||
const result = await authPlugin.authenticate({ | ||
query: { username: 'foo', password: 'bar' }, | ||
}); | ||
@@ -113,6 +92,7 @@ expect(result.token).toEqual('abc'); | ||
path.join(__dirname, '../test/fixtures/user-store.json'), | ||
{ useHttp: true } | ||
); | ||
const result = await authPlugin.authenticate({ body: { username: 'foo', password: 'bar' } }); | ||
const result = await authPlugin.authenticate({ | ||
body: { username: 'foo', password: 'bar' }, | ||
}); | ||
@@ -125,11 +105,10 @@ expect(result.token).toEqual('abc'); | ||
describe('authorize', () => { | ||
test('should fail to authorize due to missing token', async () => { | ||
test('should fail to authorize due to missing token', async () => { | ||
const authPlugin = require('./index.js')( | ||
secret, | ||
path.join(__dirname, '../test/fixtures/user-store.json'), | ||
{ useHttp: true } | ||
); | ||
try { | ||
await authPlugin.authorize({ query: { }, headers: [] }); | ||
await authPlugin.authorize({ query: {}, headers: [] }); | ||
fail('should have thrown'); | ||
@@ -146,7 +125,6 @@ } catch (error) { | ||
}); | ||
const authPlugin = require('./index.js')( | ||
secret, | ||
path.join(__dirname, '../test/fixtures/user-store.json'), | ||
{ useHttp: true } | ||
); | ||
@@ -168,9 +146,14 @@ | ||
const expires = Date.now() + 5 * 60 * 1000; | ||
const token = jwt.sign( | ||
{ exp: Math.floor(expires / 1000), sub: 'fezzik' }, | ||
secret, | ||
); | ||
const authPlugin = require('./index.js')( | ||
secret, | ||
path.join(__dirname, '../test/fixtures/user-store.json'), | ||
{ useHttp: true } | ||
); | ||
const result = await authPlugin.authorize({ query: { token: 'foo' } }); | ||
const result = await authPlugin.authorize({ query: { token } }); | ||
@@ -180,2 +163,2 @@ expect(result).toEqual('abc'); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
59604
475
1