uuid-apikey
Advanced tools
Comparing version 1.1.1 to 1.1.2
68
index.js
@@ -14,18 +14,18 @@ // index.js | ||
constructor() { | ||
this.defaultOptions = { | ||
noDashes: false | ||
} | ||
this.defaultOptions = { | ||
noDashes: false | ||
} | ||
} | ||
checkDashes(positions, str) { | ||
let test = true; | ||
for (let pos in positions) { | ||
let chr = str.charAt(positions[pos]); | ||
test = test && (chr == "-"); | ||
} | ||
return test; | ||
} | ||
checkDashes(positions, str) { | ||
let test = true; | ||
for (let pos in positions) { | ||
let chr = str.charAt(positions[pos]); | ||
test = test && (chr == "-"); | ||
} | ||
return test; | ||
} | ||
isUUID(uuid) { | ||
let uuidCheck = this.checkDashes([8, 13, 18], uuid); // Only check the first three dashes as ColdFusion implementations erroneously omit the last dash | ||
let 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, ''); | ||
@@ -37,3 +37,3 @@ let re = /[0-9A-Fa-f]*/g; | ||
isAPIKey(apiKey) { | ||
apiKey = apiKey.toUpperCase().replace(/-/g, ''); | ||
apiKey = apiKey.toUpperCase().replace(/-/g, ''); | ||
let re = /[0-9A-Z]*/g; | ||
@@ -44,5 +44,5 @@ return (apiKey.length === 28 && re.test(apiKey)); | ||
toAPIKey(uuid, options) { | ||
options = options || this.defaultOptions; | ||
options = options || this.defaultOptions; | ||
if (this.isUUID(uuid)) { | ||
uuid = uuid.replace(/-/g, ''); | ||
uuid = uuid.replace(/-/g, ''); | ||
let s1 = uuid.substr(0,8); | ||
@@ -60,3 +60,3 @@ let s2 = uuid.substr(8,8); | ||
let e4 = base32.encode32(n4); | ||
if (options.noDashes) return `${e1}${e2}${e3}${e4}`; | ||
if (options.noDashes) return `${e1}${e2}${e3}${e4}`; | ||
return `${e1}-${e2}-${e3}-${e4}`; | ||
@@ -70,4 +70,4 @@ } else { | ||
if (this.isAPIKey(apiKey)) { | ||
apiKey = apiKey.replace(/-/g, ''); | ||
let e1 = apiKey.substr(0,7); | ||
apiKey = apiKey.replace(/-/g, ''); | ||
let e1 = apiKey.substr(0,7); | ||
let e2 = apiKey.substr(7,7); | ||
@@ -94,20 +94,20 @@ let e3 = apiKey.substr(14,7); | ||
check(apiKey, uuid) { | ||
let apiTest = this.isAPIKey(apiKey); | ||
let uuidTest = this.isUUID(uuid); | ||
let uuidCheck; | ||
if (apiTest && uuidTest) { | ||
uuidCheck = this.toUUID(apiKey); | ||
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 false; | ||
} | ||
check(apiKey, uuid) { | ||
let apiTest = this.isAPIKey(apiKey); | ||
let uuidTest = this.isUUID(uuid); | ||
let uuidCheck; | ||
if (apiTest && uuidTest) { | ||
uuidCheck = this.toUUID(apiKey); | ||
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 false; | ||
} | ||
create(options) { | ||
options = options || this.defaultOptions; | ||
options = options || this.defaultOptions; | ||
let uid = uuidv4(); // Generate a new UUIDv4 | ||
@@ -114,0 +114,0 @@ let apiKey = this.toAPIKey(uid, options); |
{ | ||
"name": "uuid-apikey", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "A Base32-Crockford encoded API Key generator, validator, and converter to turn UUIDs into human readable API Keys", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,9 +9,9 @@ # uuid-apikey | ||
A generator, validator, and converter that transform UUIDs into human-readable Base32-Crockford encoded API Keys. | ||
A generator, validator, and converter that transforms UUIDs into human-readable Base32-Crockford encoded API Keys. | ||
* API Keys are 31 characters in length consisting of 4 groups of 7 characters separated by dashes (e.g. XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX) | ||
* API Keys are 31 characters in length consisting of 4 groups of 7 characters separated by dashes *(e.g. XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX)* | ||
* They avoids the problem that Base64 encoded values can create | ||
* Fully upper-case, but treat lower-case for their equivalents *(e.g. a = A)* | ||
* No tricky characters, but treat them equivalently *(i.e. 0 = O / 1 = L = I )* | ||
* No characters that inadvertently lead to common profanities (i.e. letter U is omitted) | ||
* No characters that inadvertently lead to common profanities *(i.e. letter U is omitted)* | ||
@@ -18,0 +18,0 @@ |
@@ -8,50 +8,50 @@ const test = require('unit.js'); | ||
let MainClass = require('../'); | ||
let MainClass = require('../'); | ||
it('load', () => { | ||
let myModule = require('../'); | ||
it('load', () => { | ||
let myModule = require('../'); | ||
test.assert(typeof myModule == typeof MainClass); | ||
}); | ||
test.assert(typeof myModule == typeof MainClass); | ||
}); | ||
it('test good UUID', () => { | ||
test.assert(MainClass.isUUID(testKey.uuid)); | ||
}) | ||
it('test good UUID', () => { | ||
test.assert(MainClass.isUUID(testKey.uuid)); | ||
}) | ||
it('test bad UUID', () => { | ||
test.assert(!MainClass.isUUID(testKey.apiKey)); | ||
}) | ||
it('test bad UUID', () => { | ||
test.assert(!MainClass.isUUID(testKey.apiKey)); | ||
}) | ||
it('test good API Key', () => { | ||
test.assert(MainClass.isAPIKey(testKey.apiKey)); | ||
}) | ||
it('test good API Key', () => { | ||
test.assert(MainClass.isAPIKey(testKey.apiKey)); | ||
}) | ||
it('test good lower-case API Key', () => { | ||
test.assert(MainClass.isAPIKey(testKey.apiKey.toLowerCase())); | ||
}) | ||
it('test good lower-case API Key', () => { | ||
test.assert(MainClass.isAPIKey(testKey.apiKey.toLowerCase())); | ||
}) | ||
it('test bad API Key', () => { | ||
test.assert(!MainClass.isAPIKey(testKey.uuid)); | ||
}) | ||
it('test bad API Key', () => { | ||
test.assert(!MainClass.isAPIKey(testKey.uuid)); | ||
}) | ||
it('convert to UUID', () => { | ||
test.assert(MainClass.isUUID(MainClass.toUUID(testKey.apiKey))); | ||
}); | ||
it('convert to UUID', () => { | ||
test.assert(MainClass.isUUID(MainClass.toUUID(testKey.apiKey))); | ||
}); | ||
it('convert to API Key', () => { | ||
test.assert(MainClass.isAPIKey(MainClass.toAPIKey(testKey.uuid))); | ||
}); | ||
it('convert to API Key', () => { | ||
test.assert(MainClass.isAPIKey(MainClass.toAPIKey(testKey.uuid))); | ||
}); | ||
it('test good matching API Key / UUID', () => { | ||
test.assert(MainClass.check(testKey.apiKey, testKey.uuid)); | ||
}); | ||
it('test good matching API Key / UUID', () => { | ||
test.assert(MainClass.check(testKey.apiKey, testKey.uuid)); | ||
}); | ||
it('test not matching API Key / UUID', () => { | ||
test.assert(!MainClass.check(testKey.apiKey, testUUID)); | ||
}); | ||
it('test not matching API Key / UUID', () => { | ||
test.assert(!MainClass.check(testKey.apiKey, testUUID)); | ||
}); | ||
it('key creation', () => { | ||
test.object(MainClass.create()); | ||
}); | ||
it('key creation', () => { | ||
test.object(MainClass.create()); | ||
}); | ||
}); |
11619