-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
37 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
|
||
```` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters