uuid-apikey
Advanced tools
Comparing version 1.3.3 to 1.3.4-a
#!/usr/bin/env node | ||
/* eslint no-console: "off" */ | ||
const | ||
program = require('commander') | ||
, colors = require('colors') | ||
, uuidAPIKey = require('../index') | ||
, package = require('../package.json') | ||
; | ||
const program = require('commander'), | ||
colors = require('colors'), | ||
uuidAPIKey = require('../index'), | ||
pkg = require('../package.json'); | ||
program | ||
.version(package.version) | ||
.option('-g, --generate', 'Create a new API Key/UUID pair. Ignores other parameters if passed.') | ||
.option('-u, --uuid <uuid>', 'UUID for operation. If no other parameter is passed this is converted to an API Key.') | ||
.option('-a, --apikey <apikey>', 'API Key for operation. If no other parameter is passed this is converted to an UUID.') | ||
.option('-c, --check', 'Check the API Key and/or UUID provided are valid. If both API Key and UUID are passed then they are checked against each other.'); | ||
.version(pkg.version) | ||
.option( | ||
'-g, --generate', | ||
'Create a new API Key/UUID pair. Ignores other parameters if passed.' | ||
) | ||
.option( | ||
'-u, --uuid <uuid>', | ||
'UUID for operation. If no other parameter is passed this is converted to an API Key.' | ||
) | ||
.option( | ||
'-a, --apikey <apikey>', | ||
'API Key for operation. If no other parameter is passed this is converted to an UUID.' | ||
) | ||
.option( | ||
'-c, --check', | ||
'Check the API Key and/or UUID provided are valid. If both API Key and UUID are passed then they are checked against each other.' | ||
); | ||
program.on('--help', () => { | ||
console.log(''); | ||
console.log(` Version: ${package.version}`); | ||
console.log(` Version: ${pkg.version}`); | ||
}); | ||
@@ -38,3 +49,3 @@ | ||
uuidCheck = uuidAPIKey.isUUID(program.uuid); | ||
out = (uuidCheck?colors.green('valid'):colors.red('invalid')); | ||
out = uuidCheck ? colors.green('valid') : colors.red('invalid'); | ||
console.log(`${colors.cyan('UUID')}(${colors.grey.underline(program.uuid)}) : ${out}`); | ||
@@ -45,3 +56,3 @@ } | ||
apiKeyCheck = uuidAPIKey.isAPIKey(program.apikey); | ||
out = (apiKeyCheck?colors.green('valid'):colors.red('invalid')); | ||
out = apiKeyCheck ? colors.green('valid') : colors.red('invalid'); | ||
console.log(`${colors.cyan('APIKey')}(${colors.grey.underline(program.apikey)}) : ${out}`); | ||
@@ -53,3 +64,3 @@ } | ||
compareCheck = uuidAPIKey.check(program.apikey, program.uuid); | ||
out = (compareCheck?colors.green('true'):colors.red('false')); | ||
out = compareCheck ? colors.green('true') : colors.red('false'); | ||
console.log(`UUID & APIKey are identical : ${out}`); | ||
@@ -56,0 +67,0 @@ } |
@@ -1,11 +0,30 @@ | ||
const | ||
gulp = require('gulp') | ||
, eslint = require('gulp-eslint') // ES6 JS/JSX Lineter -- Check for syntax errors | ||
, mocha = require('gulp-mocha') // Test Framework | ||
; | ||
const gulp = require('gulp'); | ||
// ES6 JS/JSX Lineter -- Check for syntax errors | ||
const eslint = require('gulp-eslint'); | ||
// Test Framework | ||
const mocha = require('gulp-mocha'); | ||
// Prettifying | ||
const prettier = require('gulp-prettier'); | ||
const config = require('./build.config'); | ||
const prettyConf = require('./.prettierrc.json'); | ||
const binFolder = config.binFolder; | ||
const testFolder = config.testFolder; | ||
const allJSFiles = [ | ||
'*.js', | ||
`${testFolder}/**/*.js`, | ||
`${testFolder}/*.js`, | ||
`${binFolder}/**/*.js`, | ||
`${binFolder}/*.js` | ||
]; | ||
const esLintOpts = { configFile: '.eslintrc.json', fix: true }; | ||
// Lint JS/JSX Files (For Express) | ||
gulp.task('lint', () => { | ||
return gulp.src('index.js') | ||
.pipe(eslint({ configFile: 'eslint.json'})) | ||
return gulp | ||
.src(allJSFiles) | ||
.pipe(eslint({ configFile: '.eslintrc.json' })) | ||
.pipe(eslint.format()) | ||
@@ -16,5 +35,6 @@ .pipe(eslint.failAfterError()); | ||
gulp.task('test', ['lint'], () => { | ||
return gulp.src('test/index.js', {read: false}) | ||
return gulp | ||
.src('test/index.js', { read: false }) | ||
.pipe(mocha()) | ||
.once('error', function() { | ||
.once('error', () => { | ||
process.exit(1); | ||
@@ -24,2 +44,23 @@ }); | ||
gulp.task('fix', () => { | ||
return gulp | ||
.src(allJSFiles) | ||
.pipe(eslint(esLintOpts)) | ||
.pipe(eslint.format()) | ||
.pipe(eslint.failAfterError()) | ||
.pipe(gulp.dest((file) => { | ||
return file.base; | ||
})); | ||
}); | ||
gulp.task('pretty', () => { | ||
return gulp | ||
.src(allJSFiles) | ||
.pipe(prettier(prettyConf)) | ||
.pipe(eslint(esLintOpts)) | ||
.pipe(gulp.dest((file) => { | ||
return file.base; | ||
})); | ||
}); | ||
gulp.task('default', ['test']); |
145
index.js
// index.js | ||
// Dependencies | ||
const | ||
base32 = require('encode32') | ||
, uuidv4 = require('uuid/v4') | ||
; | ||
const base32 = require('encode32'), uuidv4 = require('uuid/v4'); | ||
@@ -14,5 +11,3 @@ // APIKeys are a Base32-Crockford encoded representation of UUIDs | ||
constructor() { | ||
this.defaultOptions = { | ||
noDashes: false | ||
}; | ||
this.defaultOptions = { noDashes: false }; | ||
} | ||
@@ -22,5 +17,7 @@ | ||
let test = true; | ||
for (let pos in positions) { | ||
let chr = str.charAt(positions[pos]); | ||
test = test && (chr === '-'); | ||
for (const pos in positions) { | ||
if (positions.hasOwnProperty(pos)) { | ||
const chr = str.charAt(positions[pos]); | ||
test = test && chr === '-'; | ||
} | ||
} | ||
@@ -31,82 +28,97 @@ return test; | ||
isUUID(uuid) { | ||
if (!uuid) { throw new ReferenceError('The required parameter \'uuid\' is undefined.'); } | ||
let uuidCheck = this.checkDashes([8, 13, 18], uuid); // Only check the first three dashes as ColdFusion implementations erroneously omit the last dash | ||
if (!uuid) { | ||
throw new ReferenceError('The required parameter \'uuid\' is undefined.'); | ||
} | ||
const uuidCheck = this.checkDashes([8, 13, 18], uuid); | ||
// Only check the first three dashes as ColdFusion implementations erroneously omit the last dash | ||
uuid = uuid.replace(/-/g, ''); | ||
let re = /[0-9A-Fa-f]*/g; | ||
return (uuidCheck && uuid.length === 32 && re.test(uuid)); | ||
const re = /[0-9A-Fa-f]*/g; | ||
return uuidCheck && uuid.length === 32 && re.test(uuid); | ||
} | ||
isAPIKey(apiKey) { | ||
if (!apiKey) { throw new ReferenceError('The required parameter \'apiKey\' is undefined.'); } | ||
if (!apiKey) { | ||
throw new ReferenceError('The required parameter \'apiKey\' is undefined.'); | ||
} | ||
apiKey = apiKey.toUpperCase().replace(/-/g, ''); | ||
let re = /[0-9A-Z]*/g; | ||
return (apiKey.length === 28 && re.test(apiKey)); | ||
const re = /[0-9A-Z]*/g; | ||
return apiKey.length === 28 && re.test(apiKey); | ||
} | ||
toAPIKey(uuid, options) { | ||
if (!uuid) { throw new ReferenceError('The required parameter \'uuid\' is undefined.'); } | ||
if (!uuid) { | ||
throw new ReferenceError('The required parameter \'uuid\' is undefined.'); | ||
} | ||
options = options || this.defaultOptions; | ||
if (this.isUUID(uuid)) { | ||
uuid = uuid.replace(/-/g, ''); | ||
let s1 = uuid.substr(0,8); | ||
let s2 = uuid.substr(8,8); | ||
let s3 = uuid.substr(16,8); | ||
let s4 = uuid.substr(24,8); | ||
let n1 = Number('0x' + s1); | ||
let n2 = Number('0x' + s2); | ||
let n3 = Number('0x' + s3); | ||
let n4 = Number('0x' + s4); | ||
let e1 = base32.encode32(n1); | ||
let e2 = base32.encode32(n2); | ||
let e3 = base32.encode32(n3); | ||
let e4 = base32.encode32(n4); | ||
if (options.noDashes) { return `${e1}${e2}${e3}${e4}`; } | ||
const s1 = uuid.substr(0, 8); | ||
const s2 = uuid.substr(8, 8); | ||
const s3 = uuid.substr(16, 8); | ||
const s4 = uuid.substr(24, 8); | ||
const n1 = Number(`0x${s1}`); | ||
const n2 = Number(`0x${s2}`); | ||
const n3 = Number(`0x${s3}`); | ||
const n4 = Number(`0x${s4}`); | ||
const e1 = base32.encode32(n1); | ||
const e2 = base32.encode32(n2); | ||
const e3 = base32.encode32(n3); | ||
const e4 = base32.encode32(n4); | ||
if (options.noDashes) { | ||
return `${e1}${e2}${e3}${e4}`; | ||
} | ||
return `${e1}-${e2}-${e3}-${e4}`; | ||
} else { | ||
throw(new TypeError(`The value provide '${uuid}' is not a valid uuid.`)); | ||
} | ||
throw new TypeError(`The value provide '${uuid}' is not a valid uuid.`); | ||
} | ||
toUUID(apiKey) { | ||
if (!apiKey) { throw new ReferenceError('The required parameter \'apiKey\' is undefined.'); } | ||
if (!apiKey) { | ||
throw new ReferenceError('The required parameter \'apiKey\' is undefined.'); | ||
} | ||
if (this.isAPIKey(apiKey)) { | ||
apiKey = apiKey.replace(/-/g, ''); | ||
let e1 = apiKey.substr(0,7); | ||
let e2 = apiKey.substr(7,7); | ||
let e3 = apiKey.substr(14,7); | ||
let e4 = apiKey.substr(21,7); | ||
let n1 = base32.decode32(e1); | ||
let n2 = base32.decode32(e2); | ||
let n3 = base32.decode32(e3); | ||
let n4 = base32.decode32(e4); | ||
let s1 = n1.toString(16).padStart(8,'0'); | ||
let s2 = n2.toString(16).padStart(8,'0'); | ||
let s3 = n3.toString(16).padStart(8,'0'); | ||
let s4 = n4.toString(16).padStart(8,'0'); | ||
let s2a = s2.substr(0,4); | ||
let s2b = s2.substr(4,4); | ||
let s3a = s3.substr(0,4); | ||
let s3b = s3.substr(4,4); | ||
const e1 = apiKey.substr(0, 7); | ||
const e2 = apiKey.substr(7, 7); | ||
const e3 = apiKey.substr(14, 7); | ||
const e4 = apiKey.substr(21, 7); | ||
const n1 = base32.decode32(e1); | ||
const n2 = base32.decode32(e2); | ||
const n3 = base32.decode32(e3); | ||
const n4 = base32.decode32(e4); | ||
const s1 = n1.toString(16).padStart(8, '0'); | ||
const s2 = n2.toString(16).padStart(8, '0'); | ||
const s3 = n3.toString(16).padStart(8, '0'); | ||
const s4 = n4.toString(16).padStart(8, '0'); | ||
const s2a = s2.substr(0, 4); | ||
const s2b = s2.substr(4, 4); | ||
const s3a = s3.substr(0, 4); | ||
const s3b = s3.substr(4, 4); | ||
return `${s1}-${s2a}-${s2b}-${s3a}-${s3b}${s4}`; | ||
} else { | ||
throw(new TypeError(`The value provide '${apiKey}' is not a valid apiKey.`)); | ||
} | ||
throw new TypeError(`The value provide '${apiKey}' is not a valid apiKey.`); | ||
} | ||
check(apiKey, uuid) { | ||
if (!apiKey) { throw new ReferenceError('The required parameter \'apiKey\' is undefined.'); } | ||
if (!uuid) { throw new ReferenceError('The required parameter \'uuid\' is undefined.'); } | ||
let apiTest = this.isAPIKey(apiKey.toUpperCase()); | ||
let uuidTest = this.isUUID(uuid); | ||
if (!apiKey) { | ||
throw new ReferenceError('The required parameter \'apiKey\' is undefined.'); | ||
} | ||
if (!uuid) { | ||
throw new ReferenceError('The required parameter \'uuid\' is undefined.'); | ||
} | ||
const apiTest = this.isAPIKey(apiKey.toUpperCase()); | ||
const uuidTest = this.isUUID(uuid); | ||
let uuidCheck; | ||
if (apiTest && uuidTest) { | ||
uuidCheck = this.toUUID(apiKey); | ||
console.log(uuidCheck); | ||
return (uuid === uuidCheck); | ||
} else { | ||
let errMsg = ''; | ||
if (!apiTest) { errMsg += `The value provide '${apiKey}' is not a valid apiKey. `; } | ||
if (!uuidTest) { errMsg += `The value provide '${uuid}' is not a valid uuid. `; } | ||
throw(new TypeError(errMsg)); | ||
return uuid === uuidCheck; | ||
} | ||
let errMsg = ''; | ||
if (!apiTest) { | ||
errMsg += `The value provide '${apiKey}' is not a valid apiKey. `; | ||
} | ||
if (!uuidTest) { | ||
errMsg += `The value provide '${uuid}' is not a valid uuid. `; | ||
} | ||
throw new TypeError(errMsg); | ||
} | ||
@@ -116,5 +128,6 @@ | ||
options = options || this.defaultOptions; | ||
let uid = uuidv4(); // Generate a new UUIDv4 | ||
let apiKey = this.toAPIKey(uid, options); | ||
return { 'apiKey': apiKey, 'uuid': uid }; | ||
const uid = uuidv4(); | ||
// Generate a new UUIDv4 | ||
const apiKey = this.toAPIKey(uid, options); | ||
return { apiKey: apiKey, uuid: uid }; | ||
} | ||
@@ -121,0 +134,0 @@ } |
{ | ||
"name": "uuid-apikey", | ||
"version": "1.3.3", | ||
"version": "1.3.4a", | ||
"description": "A Base32-Crockford encoded API Key generator, validator, and converter to turn UUIDs into human readable API Keys", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "mocha test", | ||
"build": "gulp" | ||
"test": "nyc mocha", | ||
"build": "gulp", | ||
"fix": "gulp fix", | ||
"lint": "gulp lint", | ||
"coverage": "nyc --reporter=lcov mocha && cat ./coverage/lcov.info | codacy-coverage", | ||
"pretty": "gulp pretty" | ||
}, | ||
@@ -29,7 +33,11 @@ "bin": { | ||
"devDependencies": { | ||
"eslint": "^4.2.0", | ||
"codacy-coverage": "^2.0.3", | ||
"eslint": "^4.10.0", | ||
"gulp": "^3.9.1", | ||
"gulp-eslint": "^4.0.0", | ||
"gulp-mocha": "^4.3.1", | ||
"gulp-prettier": "^1.1.0", | ||
"mocha": "^3.4.2", | ||
"nyc": "^11.3.0", | ||
"prettier": "^1.8.2", | ||
"unit.js": "^2.0.0" | ||
@@ -36,0 +44,0 @@ }, |
# uuid-apikey | ||
[![NPM](https://nodei.co/npm/uuid-apikey.png?downloads=true)](https://nodei.co/npm/uuid-apikey/) | ||
[![Actual version published on npm](http://img.shields.io/npm/v/uuid-apikey.svg)](https://www.npmjs.org/package/uuid-apikey) | ||
[![Travis build status](https://travis-ci.org/chronosis/uuid-apikey.svg)](https://www.npmjs.org/package/uuid-apikey) | ||
[![Total npm module downloads](http://img.shields.io/npm/dt/uuid-apikey.svg)](https://www.npmjs.org/package/uuid-apikey) | ||
[![npm package quality](http://npm.packagequality.com/shield/uuid-apikey.svg)](https://www.npmjs.org/package/uuid-apikey) | ||
[![Package Quality](http://npm.packagequality.com/shield/uuid-apikey.svg)](http://packagequality.com/#?package=uuid-apikey) | ||
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/6c4ea28976c54c0493f8c0a4e742a95a)](https://www.codacy.com/app/chronosis/uuid-apikey?utm_source=github.com&utm_medium=referral&utm_content=chronosis/uuid-apikey&utm_campaign=Badge_Grade) | ||
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/6c4ea28976c54c0493f8c0a4e742a95a)](https://www.codacy.com/app/chronosis/uuid-apikey?utm_source=github.com&utm_medium=referral&utm_content=chronosis/uuid-apikey&utm_campaign=Badge_Coverage) | ||
[![NPM](https://nodei.co/npm/uuid-apikey.png?downloads=true)](https://nodei.co/npm/uuid-apikey/) | ||
*"API Keys for people"* | ||
@@ -11,0 +13,0 @@ |
const test = require('unit.js'); | ||
let testKey = { uuid: '872a6f67-7d93-4e9c-8d9a-4637a3feff65', apiKey: 'GWN6YSX-FP9MX73-HPD4CDY-MFZFYSC' }; | ||
let testUUID = '0b9ca335-92a8-46d8-b277-ec2ed83ac427'; | ||
const testKey = { | ||
uuid: '872a6f67-7d93-4e9c-8d9a-4637a3feff65', | ||
apiKey: 'GWN6YSX-FP9MX73-HPD4CDY-MFZFYSC' | ||
}; | ||
const testUUID = '0b9ca335-92a8-46d8-b277-ec2ed83ac427'; | ||
describe('uuid-apikey', () => { | ||
const MainClass = require('../'); | ||
let MainClass = require('../'); | ||
it('load', () => { | ||
let myModule = require('../'); | ||
const myModule = require('../'); | ||
@@ -55,3 +57,2 @@ test.assert(typeof myModule === typeof MainClass); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
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
24282
11
442
174
10
1
2