Skip to content

Commit

Permalink
Merge pull request #11 from onelastjedi/dev-1.0.3
Browse files Browse the repository at this point in the history
Dev 1.0.3
  • Loading branch information
onelastjedi authored Oct 15, 2023
2 parents c65a554 + c32fa21 commit e074b9d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
38 changes: 28 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
var crypto = require('node:crypto')

/*
* Some usefull helpers
* Let's start from helpers
*
*
*/ // replace :: Regex -> String
Expand Down Expand Up @@ -93,17 +93,35 @@ var getHead = alg => encode({
* sign({ foo: "bar" }, 'secret') -> eyJhbGc.....
* sign({ foo: "bar" }, 'secret', 'HS512') -> eyJhbGc.....
*/
function sign (body, secret, alg = 'HS256') {
Object.hasOwn(body, 'exp')
&& (body.exp = after(body.exp))
;
function sign ({ exp, ...rest }, secret, alg = 'HS256') {

var head = getHead(alg),
body = encode({ ...body, iat: now(Date) }),
signer = getSigner(alg)(secret)
;
body = encode(

{

...rest,

iat: now(Date),


...( exp


return `${head}.${body}.${signer(head + '.' + body)}`
&&

{

exp: after(exp) })

}


),
signer = getSigner(alg)(secret)


; return `${head}.${body}.${signer(head + '.' + body)}`
}

/**
Expand Down Expand Up @@ -140,4 +158,4 @@ function verify (token, secret) {
module.exports = {
sign,
verify
}
}
1 change: 1 addition & 0 deletions index.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@onelastjedi/node-jwt",
"version": "1.0.2",
"version": "1.0.3",
"description": "Sign and verify JSON Web Tokens in it's simplest form",
"homepage": "https://phon.one/node-jwt",
"repository": {
Expand All @@ -20,13 +20,11 @@
"name": "J.D",
"url": "https://phon.one"
},
"main": "src/index.js",
"main": "index.min.js",
"scripts": {
"build": "esbuild index.js --bundle --platform=node --minify --analyze --outfile=index.min.js",
"test": "mocha"
},
"devDependencies": {
"mocha": "^10.2.0"
},
"engines": {
"node": ">=12"
}
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('Library is a object which exports:', () => {

describe(`'verify' return errors:`, () => {
const invalid = () => lib.verify(token, 'secre')
const expired = lib.sign({ exp: 0 }, secret)
const expired = lib.sign({ exp: -1 }, secret)
const exp = () => lib.verify(expired, secret)

it('for invalid signature', () =>
Expand Down

0 comments on commit e074b9d

Please sign in to comment.