From 45c8b1b9e3ae41d4e24e3a0a8d0ae92a53095425 Mon Sep 17 00:00:00 2001 From: signalhub Date: Sun, 5 May 2024 16:59:20 +0300 Subject: [PATCH] Bump version 0.0.6 --- README.md | 68 ++++++++++++++++++++------------------- package.json | 2 +- src/utils/generate-key.js | 2 +- 3 files changed, 37 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 169daf7..a869ec2 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Tokenized auth +# Crypto auth data ## Install @@ -6,43 +6,45 @@ Run `npm i crypto-auth-data` to install the library. ## How to use -### Generate key and encrypt it like a secret +### 1. Generate key and encrypt it like a secret + +Add script to your `package.json` file: +```json +"scripts": { + "generate-key": "node node_modules/crypto-auth-data/src/utils/generate-key.js -- --secret \"my-secret-phrase\" --output \"path/to/project/.env\"" +}, +``` +in the .env file you will have the following variables +```env +ENCRYPTION_KEY=eoEG4sJQxPHurfzgYSJ7Vmlwsk7poKXiHlq8MQxvjp4= +``` + +### 2. Decrypt generated key ```typescript -const key = await cryptoAuth.encryptKey(); +import { cryptoData } from 'crypto-auth-data'; + +const salt = new Uint8Array(16); +const key = process.env.ENCRYPTION_KEY +const decryptedKey = await cryptoData.decryptSecretKey(key, salt); + ``` -Store generated key in the .env file - -Or you can generate this key then you build your application just create a file `generate-env.js` in the root of your project with the following content: - -```javascript -const fs = require('fs'); -const crypto = require('crypto').webcrypto; - -async function generateEncryptionKey() { - const key = await crypto.subtle.generateKey( - { name: 'AES-GCM', length: 256 }, - true, - ['encrypt', 'decrypt'] - ); - const exportedKey = await crypto.subtle.exportKey('raw', key); - const exportedKeyBuffer = Buffer.from(exportedKey); - return exportedKeyBuffer.toString('base64'); -} -async function generateEnv() { - const secretKey = await generateEncryptionKey(); - const envContent = `\nENCRYPTION_KEY=${secretKey}\n`; - fs.appendFileSync('.env', envContent); -} +### 3. Encrypt JWT data -generateEnv().then(); -```` -and update your `package.json` file: -```json -"scripts": { - "build": "node generate-env.js && next build", - // ... +```typescript +if (decryptedKey) { + const encryptJWT = await cryptoData.encryptJWT(response.token.accessToken, decryptedKey); // save it to localStorage } + ``` +### 4. Decrypt JWT data for every request + +Get encryptJWT from localStorage and decrypt it + +```typescript +const accessToken = await cryptoData.decryptJWT(encryptJWT, decryptedKey); +headers.set("Authorization", `Bearer ${accessToken}`); + +```` diff --git a/package.json b/package.json index 37ae096..3f1b9e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "crypto-auth-data", - "version": "0.0.5", + "version": "0.0.6", "license": "MIT", "scripts": { "build": "nx build", diff --git a/src/utils/generate-key.js b/src/utils/generate-key.js index 4e05241..c9b40fe 100644 --- a/src/utils/generate-key.js +++ b/src/utils/generate-key.js @@ -34,7 +34,7 @@ const encryptSecretKey = async (secret, salt) => { }; const generateEnvFile = async (secret, envFilePath) => { - const salt = crypto.getRandomValues(new Uint8Array(16)); + const salt = new Uint8Array(16); const encryptionKey = await encryptSecretKey(secret, salt);