uuid-apikey
Advanced tools
Comparing version 1.0.1 to 1.1.0
40
index.js
@@ -14,19 +14,33 @@ // index.js | ||
constructor() { | ||
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; | ||
} | ||
isUUID(uuid) { | ||
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, ''); | ||
let re = /[0-9A-Fa-f]*/g; | ||
return (uuid.length == 32 && re.test(uuid)); | ||
return (uuidCheck && uuid.length === 32 && re.test(uuid)); | ||
} | ||
isAPIKey(apiKey) { | ||
apiKey = apiKey.replace(/-/g, ''); | ||
apiKey = apiKey.replace(/-/g, ''); | ||
let re = /[0-9A-Z]*/g; | ||
return (apiKey.length == 28 && re.test(apiKey)); | ||
return (apiKey.length === 28 && re.test(apiKey)); | ||
} | ||
toAPIKey(uuid) { | ||
uuid = uuid.replace(/-/g, ''); | ||
toAPIKey(uuid, options) { | ||
options = options || this.defaultOptions; | ||
if (this.isUUID(uuid)) { | ||
uuid = uuid.replace(/-/g, ''); | ||
let s1 = uuid.substr(0,8); | ||
@@ -44,2 +58,3 @@ let s2 = uuid.substr(8,8); | ||
let e4 = base32.encode32(n4); | ||
if (options.noDashes) return `${e1}${e2}${e3}${e4}`; | ||
return `${e1}-${e2}-${e3}-${e4}`; | ||
@@ -53,7 +68,7 @@ } else { | ||
if (this.isAPIKey(apiKey)) { | ||
let parts = apiKey.split("-"); | ||
let e1 = parts[0]; | ||
let e2 = parts[1]; | ||
let e3 = parts[2]; | ||
let e4 = parts[3]; | ||
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); | ||
@@ -93,5 +108,6 @@ let n2 = base32.decode32(e2); | ||
create() { | ||
create(options) { | ||
options = options || this.defaultOptions; | ||
let uid = uuidv4(); // Generate a new UUIDv4 | ||
let apiKey = this.toAPIKey(uid); | ||
let apiKey = this.toAPIKey(uid, options); | ||
return { 'apiKey': apiKey, 'uuid': uid }; | ||
@@ -98,0 +114,0 @@ } |
{ | ||
"name": "uuid-apikey", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"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", |
# uuid-apikey | ||
[![Actual version published on npm](http://img.shields.io/npm/v/uuid-apikey.svg)](https://www.npmjs.org/package/unit.js) | ||
[![Travis build status](https://travis-ci.org/chronosis/uuid-apikey.svg)](https://www.npmjs.org/package/unit.js) | ||
[![Total npm module downloads](http://img.shields.io/npm/dt/uuid-apikey.svg)](https://www.npmjs.org/package/unit.js) | ||
[![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) | ||
A Base32-Crockford encoded API Key generator, validator, and converter to turn UUIDs into human readable API Keys | ||
*"API Keys for people"* | ||
API Keys are 31 characters in length (e.g. XXXXXXX-XXXXXXX-XXXXXXX-XXXXXXX), are fully-uppercase, tricky characters are treated equivalently (0 = O / 1 = L = I ), and the letter U is omitted to avoid common profanities. | ||
A generator, validator, and converter that transform UUIDs into human-readable Base32-Crockford encoded API Keys. | ||
A common use for this library is to allow APIKeys to be stored in a host DB as UUID, but to display the APIKey version in its encoded form to users. | ||
* 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) | ||
A common use for this library is to allow UUIDs to be stored in a host DB, but to display a human-readable API Key to a user. | ||
## Installation | ||
@@ -38,2 +45,3 @@ You can install `uuid-apikey` with NPM. | ||
``` | ||
**Output**: | ||
``` | ||
@@ -50,2 +58,3 @@ true | ||
``` | ||
**Output**: | ||
``` | ||
@@ -56,11 +65,19 @@ true | ||
### .toAPIKey(uuid) | ||
### .toAPIKey(uuid, [options]) | ||
Converts a valid UUID into an API Key. Throws a `TypeError` if the UUID is invalid. | ||
```es2016 | ||
uuidAPIKey.toAPIKey('0b9ca335-92a8-46d8-b477-eb2ed83ac927'); | ||
uuidAPIKey.toAPIKey('0b9ca335-92a8-46d8-b477-eb2ed83ac927', { 'noDashes': true }); | ||
``` | ||
**Output**: | ||
``` | ||
'1EEA6DC-JAM4DP2-PHVYPBN-V0XCJ9X' | ||
'1EEA6DCJAM4DP2PHVYPBNV0XCJ9X' | ||
``` | ||
**Options** | ||
| Option | Type | Desc | | ||
| ------ | ---- | ---- | | ||
| noDashes | boolean | Generates an APIKey without dashes | | ||
### .toUUID(apiKey) | ||
@@ -71,2 +88,3 @@ Converts a valid API Key into an UUID. Throws a `TypeError` if the API Key is invalid. | ||
``` | ||
**Output**: | ||
``` | ||
@@ -81,2 +99,3 @@ '0b9ca335-92a8-46d8-b477-eb2ed83ac927' | ||
``` | ||
**Output**: | ||
``` | ||
@@ -86,3 +105,3 @@ true | ||
### .create() | ||
### .create([options]) | ||
Returns a new UUID and API Key pair | ||
@@ -92,2 +111,3 @@ ```es2016 | ||
``` | ||
**Output**: | ||
``` | ||
@@ -97,4 +117,9 @@ { uuid: '0b9ca335-92a8-46d8-b477-eb2ed83ac927', | ||
``` | ||
**Options** | ||
| Option | Type | Desc | | ||
| ------ | ---- | ---- | | ||
| noDashes | boolean | Generates an APIKey without dashes | | ||
## License | ||
MIT |
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
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
11368
167
118