Skip to content

feature/modern-with-austere #3

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,23 @@ ES512 | ECDSA using P-521 curve and SHA-512 hash algorithm
Please note that PSXXX only works on Node 6.12+ (excluding 7.x).

## Test
`npm test`

### Feature Test
```bash
npm test
```

### ESM Test
```bash
node test.mjs
```

## Example
```javascript
// import module
const pJWT = require('pico-jwt')
import pJWT from 'pico-jwt'
// supported commonjs require as well
// const pJWT = require('pico-jwt')

// instantiate the module
const jwt = new pJWT('HS256', 'secretKey')
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const fs = require('fs')
const path = require('path')
const crypto = require('crypto')
const fs = require('node:fs')
const path = require('node:path')
const crypto = require('node:crypto')
const ecdsa = require('ecdsa-sig-formatter')

const SEP = '.'
Expand Down
2 changes: 2 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const pJWT = require('./index')
export default pJWT
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
{
"name": "pico-jwt",
"version": "0.3.3",
"version": "0.4.0",
"description": "A pico-sized JWT library",
"type": "commonjs",
"main": "index.js",
"module": "index.mjs",
"scripts": {
"test": "DEBUG=1 node test.js",
"lint": "eslint .",
Expand Down
36 changes: 36 additions & 0 deletions test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// import module
import { fileURLToPath } from 'node:url'
import { dirname } from 'node:path'
import pJWT from './index.js'

const dir = dirname(fileURLToPath(import.meta.url))

// instantiate the module
const jwt = new pJWT('RS256')

// add key files asynchronous (absolute path only)
jwt.addKeys(dir + '/test.key', dir + '/test.key.pub', () => {
console.log('loaded')

const rawHeader = {
kid: 'custom-header-key-id'
}
const rawPayload = {
iss: 'pico',
aud: 'world'
}

// create jwt with payload
const token = jwt.create(rawPayload, rawHeader)

// get header of jwt
const header = jwt.header(token) // or pJWT.prototype.header(token)
console.log('jwt header extract:', rawHeader.kid === header.kid)

// get payload of jwt
const payload = jwt.payload(token) // or pJWT.prototype.payload(token)
console.log('jwt payload extract:', rawPayload.aud === payload.aud)

// verify jwt
console.log('jwt validation:', jwt.verify(token))
})
30 changes: 30 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/// <reference types="node" />

declare module 'pico-jwt' {
import { KeyObject } from 'crypto';

type Callback = (err: Error | null, key?: string | Buffer | undefined) => void;
type Algorithm = 'HS256' | 'HS384' | 'HS512' | 'RS256' | 'RS384' | 'RS512' | 'ES256' | 'ES384' | 'ES512' | 'PS256' | 'PS384' | 'PS512';

interface JWTHeader {
typ?: string;
alg: Algorithm;
[key: string]: any;
}

interface JWTPayload {
[key: string]: any;
}

class JWT {
constructor(algo: Algorithm, secret: string | Buffer, key?: string | Buffer);
addKeys(secret: string | Buffer, key?: string | Buffer, cb?: Callback): void;
create(payload: JWTPayload, header?: Partial<JWTHeader>): string;
header(jwt: string): JWTHeader | undefined;
payload(jwt: string): JWTPayload | undefined;
verify(jwt: string): boolean;
}

export = JWT;
}