0.0.16 • Published 2 years ago

@typescript-auth/server-utils v0.0.16

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

@typescript-auth/server-utils 🛡

npm version main codecov Known Vulnerabilities

The main propose of this package, is to provide general utilities for authorization & authentication.

Table of Contents

Installation

npm install @typescript-auth/server-utils --save

Usage

KeyPair

Create a private pkcs8 key and spki public key. The useKeyPair method will automatically create, a key pair in the specified directory if it doesn't already exist.

import path from 'path';
import {
    createKeyPair,
    useKeyPair,
    KeyPairOptions
} from "@typescript-auth/server";

const keyPairOptions: KeyPairOptions = {
    directory: path.join(__dirname, 'writable')
}

(async () => {
    await createKeyPair(keyPairOptions);

    const keyPair = await useKeyPair(keyPairOptions);
    
    console.log(keyPair);
    // {privateKey: 'xxx', publicKey: 'xxx'}
})();

Sign & Verify

The signToken and verifyToken provide a simple way to sign and verify a token (default: RS256). A private- & public-key will be automatically generated if none already exists.

import path from 'path';
import {
    KeyPairOptions,
    signToken,
    verifyToken
} from "@typescript-auth/server-utils";


const keyPairOptions: KeyPairOptions = {
    directory: path.join(__dirname, 'writable')
}

(async () => {
    const token : Record<string, any> = {foo: 'bar'};
    const tokenSigned = await signToken(token, {
        options: {
            expiresIn: 3600
        },
        keyPairOptions
    });
    
    const tokenVerified = await verifyToken(tokenSigned);
    
    console.log(tokenVerified);
    // {iat: 1642942322, exp: 1642945922, foo: 'bar', ... }
})();

Hash

To simply hash and verify a password with the password hashing-function bcrypt based on the Blowfish cipher, use the methods hash & compare.

import {
    hash,
    compare
} from "@typescript-auth/server-utils";


(async () => {
    const hashed = await hash('start123', 10); // 10 rounds
    let isValid = await compare('start123', hashed);
    console.log(isValid);
    // true
    
    isValid = await compare('star1234', hashed);
    console.log(isValid);
    // false
})();
0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.13

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago