-
Notifications
You must be signed in to change notification settings - Fork 16
added method _isNotExpired #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| ## nodejs-license-file [](https://travis-ci.org/bushev/nodejs-license-file) | ||
| # nodejs-license-file | ||
|
|
||
| [](https://travis-ci.org/bushev/nodejs-license-file) | ||
|
|
||
| A lightweight License file generator and parser for NodeJS. | ||
|
|
||
|
|
@@ -15,79 +17,75 @@ A lightweight License file generator and parser for NodeJS. | |
| ## Generating license file | ||
|
|
||
| ```javascript | ||
| const licenseFile = require('nodejs-license-file'); | ||
| const licenseFile = require("nodejs-license-file"); | ||
|
|
||
| const template = [ | ||
| '====BEGIN LICENSE====', | ||
| '{{&licenseVersion}}', | ||
| '{{&applicationVersion}}', | ||
| '{{&firstName}}', | ||
| '{{&lastName}}', | ||
| '{{&email}}', | ||
| '{{&expirationDate}}', | ||
| '{{&serial}}', | ||
| '=====END LICENSE=====' | ||
| ].join('\n'); | ||
| "====BEGIN LICENSE====", | ||
| "{{&licenseVersion}}", | ||
| "{{&applicationVersion}}", | ||
| "{{&firstName}}", | ||
| "{{&lastName}}", | ||
| "{{&email}}", | ||
| "{{&expirationDate}}", | ||
| "{{&serial}}", | ||
| "=====END LICENSE=====" | ||
| ].join("\n"); | ||
|
|
||
| try { | ||
|
|
||
| const licenseFileContent = licenseFile.generate({ | ||
| privateKeyPath: 'path/to/key.pem', | ||
| privateKeyPath: "path/to/key.pem", | ||
| template, | ||
| data: { | ||
| licenseVersion: '1', | ||
| applicationVersion: '1.0.0', | ||
| firstName: 'Name', | ||
| lastName: 'Last Name', | ||
| email: 'some@email.com', | ||
| expirationDate: '12/10/2025' | ||
| licenseVersion: "1", | ||
| applicationVersion: "1.0.0", | ||
| firstName: "Name", | ||
| lastName: "Last Name", | ||
| email: "some@email.com", | ||
| expirationDate: "2025-11-15" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why change the date format? Used to be separated by
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dayjs handles date with this specific format by default, plus it's less confusing |
||
| } | ||
| }); | ||
|
|
||
| console.log(licenseFileContent); | ||
|
|
||
| console.log(licenseFileContent); | ||
| } catch (err) { | ||
|
|
||
| console.log(err); | ||
| } | ||
| ``` | ||
|
|
||
| This will produce a license key, which uses the default template and will look similar to this: | ||
| ``` | ||
|
|
||
| ```txt | ||
| ====BEGIN LICENSE==== | ||
| 1 | ||
| 1.0.0 | ||
| Name | ||
| Last Name | ||
| some@email.com | ||
| 12/10/2025 | ||
| 2025-11-15 | ||
| xxxxxxxxxxxxxxxxxxxxx | ||
| =====END LICENSE===== | ||
| ``` | ||
|
|
||
| ## Parse and verify license file | ||
|
|
||
| ```javascript | ||
| const licenseFile = require('nodejs-license-file'); | ||
| const licenseFile = require("nodejs-license-file"); | ||
|
|
||
| try { | ||
|
|
||
| const data = licenseFile.parse({ | ||
| publicKeyPath: 'path/to/key.pub', | ||
| licenseFilePath: 'path/to/file.lic', | ||
| publicKeyPath: "path/to/key.pub", | ||
| licenseFilePath: "path/to/file.lic", | ||
| template | ||
| }); | ||
|
|
||
| console.log(data); | ||
|
|
||
| } catch (err) { | ||
|
|
||
| console.log(err); | ||
| } | ||
| ``` | ||
|
|
||
| There is an execution result: | ||
| ``` | ||
|
|
||
| ```json | ||
| { | ||
| valid: true, | ||
| serial: 'oZDqoEr2avwhAqwV4HInq9otNzeBeD/azq2yn2jA ...', | ||
|
|
@@ -97,7 +95,7 @@ There is an execution result: | |
| firstName: 'Name', | ||
| lastName: 'Last Name', | ||
| email: 'some@email.com', | ||
| expirationDate: '12/10/2025' | ||
| expirationDate: '2025-11-15' | ||
| } | ||
| } | ||
| ``` | ||
|
|
@@ -107,38 +105,36 @@ NOTICE: All numeric data will be converted to strings after parsing. You need to | |
| ## Parse and verify license string | ||
|
|
||
| ```javascript | ||
| const licenseFile = require('nodejs-license-file'); | ||
| const licenseFile = require("nodejs-license-file"); | ||
|
|
||
| try { | ||
|
|
||
| const licence = ` | ||
| ====BEGIN LICENSE==== | ||
| 1 | ||
| 1.0.0 | ||
| Name | ||
| Last Name | ||
| some@email.com | ||
| 12/10/2025 | ||
| 2025-11-15 | ||
| xxxxxxxxxxxxxxxxxxxxx | ||
| =====END LICENSE===== | ||
| `; | ||
|
|
||
| const data = licenseFile.parse({ | ||
| publicKeyPath: 'path/to/key.pub', | ||
| publicKeyPath: "path/to/key.pub", | ||
| licenseFile: licence, | ||
| template | ||
| }); | ||
|
|
||
| console.log(data); | ||
|
|
||
| } catch (err) { | ||
|
|
||
| console.log(err); | ||
| } | ||
| ``` | ||
|
|
||
| There is an execution result: | ||
| ``` | ||
|
|
||
| ```json | ||
| { | ||
| valid: true, | ||
| serial: 'oZDqoEr2avwhAqwV4HInq9otNzeBeD/azq2yn2jA ...', | ||
|
|
@@ -148,7 +144,7 @@ There is an execution result: | |
| firstName: 'Name', | ||
| lastName: 'Last Name', | ||
| email: 'some@email.com', | ||
| expirationDate: '12/10/2025' | ||
| expirationDate: '2025-11-15' | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ const crypto = require('crypto'); | |
| */ | ||
| const stringify = require('json-stable-stringify'); | ||
|
|
||
| const dayjs = require('dayjs'); | ||
| /** | ||
| * LicenseFile Class | ||
| */ | ||
|
|
@@ -167,7 +168,7 @@ class LicenseFile { | |
|
|
||
| verify.update(stringify(dataObj.data)); | ||
|
|
||
| const valid = verify.verify(publicKey, dataObj.serial, 'base64'); | ||
| const valid = verify.verify(publicKey, dataObj.serial, 'base64') && LicenseFile._isNotExpired(dataObj); | ||
|
|
||
| return { | ||
| valid: valid, | ||
|
|
@@ -227,6 +228,18 @@ class LicenseFile { | |
|
|
||
| return result; | ||
| } | ||
|
|
||
| static _isNotExpired(options) { | ||
| let data = options.data; | ||
|
|
||
| if (typeof options.data === 'object' && typeof data.expirationDate === 'string') { | ||
| const expirationDate = data.expirationDate; | ||
| const isBefore = dayjs().isBefore(dayjs(expirationDate)); | ||
| const isSame = dayjs().isSame(dayjs(expirationDate)) | ||
| return isBefore || isSame; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I see you're doing is checking if I believe you can compare two plain JavaScript date objects with the You can get a date object of the current time via the date constructor without parameters So, it would be something like: |
||
| } | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| }, | ||
| "homepage": "https://github.com/bushev/nodejs-license-file", | ||
| "dependencies": { | ||
| "dayjs": "1.8.17", | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's trivial to compare dates in JavaScript, so you don't need a dependency for that.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's easier to manipulate dates with dayjs, which is the lightest date library I've found There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While that's true, the manipulation that you perform is rather trivial even without a library. |
||
| "json-stable-stringify": "1.0.1" | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why change the string quotes convention?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My linter did that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This project does not have an
.eslintrcfile so you shouldn't be applying your personal preferences to it. It's better to maintain the existing conventions.