Skip to content

Commit

Permalink
Bump version 0.0.6
Browse files Browse the repository at this point in the history
  • Loading branch information
signalhub committed May 5, 2024
1 parent 52f6389 commit 45c8b1b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
68 changes: 35 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,48 +1,50 @@
# Tokenized auth
# Crypto auth data

## Install

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}`);

````
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "crypto-auth-data",
"version": "0.0.5",
"version": "0.0.6",
"license": "MIT",
"scripts": {
"build": "nx build",
Expand Down
2 changes: 1 addition & 1 deletion src/utils/generate-key.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 45c8b1b

Please sign in to comment.