0.13.0 • Published 2 days ago

@sidewinder/token v0.13.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 days ago

Overview

Sidewinder Token is a type safe Json Web Token library used to sign and verify claims exchanged between services over a network. This library is built upon the jsonwebtoken package but provides additional type checking for claims data for both signer and verifier. This package supports asymmetric signing only.

Licence MIT

Contents

Example

The following shows general usage

import { Generate, TokenEncoder, TokenDecoder } from '@sidewinder/token'
import { Type } from '@sidewinder/type'

// ----------------------------------------------------------------------
// Generate private and public key pair
// ----------------------------------------------------------------------

const [privateKey, publicKey] = Generate.KeyPair()

// ----------------------------------------------------------------------
// Create create a token type schematic
// ----------------------------------------------------------------------

const Token = Type.Object({
  username: Type.String(),
  roles: Type.Array(Type.String()),
})

// ----------------------------------------------------------------------
// Create a TokenEncoder and encode token
// ----------------------------------------------------------------------

const encoder = new TokenEncoder(Token, privateKey)

const token = encoder.encode({ username: 'dave', roles: ['admin', 'moderator'] })

// ----------------------------------------------------------------------
// Create a TokenDecoder and decode token
// ----------------------------------------------------------------------

const decoder = new TokenDecoder(Token, public)

const claims = decoder.decode(token)

TokenEncoder

The TokenEncoder is responsible for encoding a claims object intended to be sent to a remote system over the network. The TokenEncoder accepts the type of the claims as it's first constructor argument, followed by a valid RSA private key kept secret to the encoding process. The TokenEncoder will throw an error if the data being encoded is not of the correct type.

import { TokenEncoder } from '@sidewinder/token'
import { Type } from '@sidewinder/type'

const privateKey = '....' // Note: Private Keys are of type string.

const encoder = new TokenEncoder(
  Type.Object({
    username: Type.String(),
    roles: Type.Array(Type.String()),
  }),
  privateKey,
)

const encoded = encoder.encode({
  // Note: Data must conform to the structure
  username: 'dave', //       given on the encoders constructor
  roles: ['admin', 'moderator'], //       or an error is thrown.
})

// send 'encoded' to remote process

TokenDecoder

The TokenEncoder is responsible for decoding a claims object received from a remote system over the network. The TokenEncoder accepts the type of the claims as it's first constructor argument, followed by a valid RSA publicKey given to the decoding process via some user defined mechanism. The TokenEncoder will throw an error if the data being decoded is not of the correct type.

import { TokenDecoder } from '@sidewinder/token'
import { Type } from '@sidewinder/type'

const token = '...' // Note: Token is received via some network mechanism

const publicKey = '...' // Note: Public Keys are of type string.

const decoder = new TokenDecoder(
  Type.Object({
    username: Type.String(),
    roles: Type.Array(Type.String()),
  }),
  publicKey,
)

const token = decoder.decode(encoded) // Note: The decode() function will throw
//       if the given encoded data cannot
//       be verified with the configured
//       publicKey, or if the type of the
//       claims data does not match that
//       of the configured schema.

Generate Keys

The Sidewinder Token library uses asymmetric RS256 exclusively for token sign and verify. You can generate private and public keys either via command line or programmatically.

# Generate private and public keys
$ ssh-keygen -t rsa -b 4096 -m PEM -f private.key

# Convert public key to PEM format
$ openssl rsa -in private.key -pubout -outform PEM -out public.key
import { Generate } from '@sidewinder/token'

const [privateKey, publicKey] = Generate.KeyPair(4096)
0.13.0

2 days ago

0.12.10

10 months ago

0.12.11

10 months ago

0.12.12

10 months ago

0.12.14

10 months ago

0.12.15

10 months ago

0.12.8

11 months ago

0.12.9

11 months ago

0.12.7

11 months ago

0.12.6

11 months ago

0.12.5

1 year ago

0.11.0

2 years ago

0.10.14

2 years ago

0.11.1

2 years ago

0.10.15

2 years ago

0.11.2

2 years ago

0.11.3

2 years ago

0.11.4

2 years ago

0.10.10

2 years ago

0.11.5

2 years ago

0.10.11

2 years ago

0.11.6

1 year ago

0.10.12

2 years ago

0.10.13

2 years ago

0.10.9

2 years ago

0.10.1

2 years ago

0.12.0

1 year ago

0.10.2

2 years ago

0.12.1

1 year ago

0.10.3

2 years ago

0.12.2

1 year ago

0.10.4

2 years ago

0.12.3

1 year ago

0.10.5

2 years ago

0.12.4

1 year ago

0.10.6

2 years ago

0.10.7

2 years ago

0.10.8

2 years ago

0.10.0

2 years ago

0.9.2

2 years ago

0.9.1

2 years ago

0.8.85

2 years ago

0.8.84

2 years ago

0.8.81

2 years ago

0.8.80

2 years ago

0.8.83

2 years ago

0.8.82

2 years ago

0.8.78

2 years ago

0.8.79

2 years ago

0.9.0

2 years ago

0.8.67

2 years ago

0.8.66

2 years ago

0.8.69

2 years ago

0.8.68

2 years ago

0.8.63

2 years ago

0.8.62

2 years ago

0.8.65

2 years ago

0.8.64

2 years ago

0.8.61

2 years ago

0.8.77

2 years ago

0.8.74

2 years ago

0.8.73

2 years ago

0.8.76

2 years ago

0.8.75

2 years ago

0.8.70

2 years ago

0.8.72

2 years ago

0.8.71

2 years ago

0.8.60

2 years ago

0.8.59

2 years ago

0.8.56

2 years ago

0.8.58

2 years ago

0.8.57

2 years ago

0.8.45

2 years ago

0.8.44

2 years ago

0.8.47

2 years ago

0.8.46

2 years ago

0.8.49

2 years ago

0.8.48

2 years ago

0.8.55

2 years ago

0.8.52

2 years ago

0.8.51

2 years ago

0.8.54

2 years ago

0.8.53

2 years ago

0.8.50

2 years ago

0.8.43

2 years ago

0.8.42

2 years ago

0.8.41

2 years ago

0.8.40

2 years ago

0.8.39

2 years ago

0.8.38

2 years ago

0.8.37

2 years ago

0.8.36

2 years ago

0.8.35

2 years ago

0.8.34

2 years ago

0.8.33

2 years ago

0.8.32

2 years ago