Skip to content

Commit

Permalink
Update publish info
Browse files Browse the repository at this point in the history
  • Loading branch information
signalhub committed May 4, 2024
1 parent 039748b commit 9750074
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 61 deletions.
90 changes: 40 additions & 50 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,48 @@
# TokenizedAuth
# Tokenized auth

<a alt="Nx logo" href="https://nx.dev" target="_blank" rel="noreferrer"><img src="https://raw.githubusercontent.com/nrwl/nx/master/images/nx-logo.png" width="45"></a>
## Install

**This workspace has been generated by [Nx, Smart Monorepos · Fast CI.](https://nx.dev)**
Run `npm i tokenized-auth` to install the library.

## Integrate with editors
## How to use

Enhance your Nx experience by installing [Nx Console](https://nx.dev/nx-console) for your favorite editor. Nx Console
provides an interactive UI to view your projects, run tasks, generate code, and more! Available for VSCode, IntelliJ and
comes with a LSP for Vim users.

## Build the library

Run `npx nx build` to build the library. The build artifacts are stored in the output directory (i.e. `dist/`), ready to be published.

## Running tasks

To execute tasks with Nx use the following syntax:

```
npx nx <target> <project> <...options>
```

You can also run multiple targets:

```
npx nx run-many -t <target1> <target2>
```

..or add `-p` to filter specific projects
### Generate key and encrypt it like a secret

```typescript
const key = await cryptoAuthUtils.encryptKey();
```
npx nx run-many -t <target1> <target2> -p <proj1> <proj2>
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);
}

generateEnv().then();
````
and update your `package.json` file:
```json
"scripts": {
"build": "node generate-env.js && next build",
// ...
}
```

Targets can be defined in the `package.json` or `projects.json`. Learn more [in the docs](https://nx.dev/features/run-tasks).

## Set up CI!

Nx comes with local caching already built-in (check your `nx.json`). On CI you might want to go a step further.

- [Set up remote caching](https://nx.dev/features/share-your-cache)
- [Set up task distribution across multiple machines](https://nx.dev/nx-cloud/features/distribute-task-execution)
- [Learn more how to setup CI](https://nx.dev/recipes/ci)

## Explore the project graph

Run `npx nx graph` to show the graph of the workspace.
It will show tasks that you can run with Nx.

- [Learn more about Exploring the Project Graph](https://nx.dev/core-features/explore-graph)

## Connect with us!

- [Join the community](https://nx.dev/community)
- [Subscribe to the Nx Youtube Channel](https://www.youtube.com/@nxdevtools)
- [Follow us on Twitter](https://twitter.com/nxdevtools)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@tokenized-auth/source",
"name": "tokenized-auth",
"version": "0.0.1",
"license": "MIT",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './lib/tokenized-auth';
export { cryptoAuthUtils } from './lib/tokenized-auth';
18 changes: 9 additions & 9 deletions src/lib/tokenized-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const sha256 = async (message: string) => {
const hash = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hash));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
};

/**
* Calculates SHA-256 hash of the input string
Expand All @@ -22,7 +22,7 @@ const sha256 = async (message: string) => {
const saveJWTHashToStorage = async (jwt: string, storageKey = "accessTokenHash") => {
const jwtHash = await sha256(jwt);
localStorage.setItem(storageKey, jwtHash);
}
};

/**
* Verifies if the JWT hash stored in the storage is equal to the hash of the input JWT
Expand All @@ -37,7 +37,7 @@ const verifyJWTFromStorage = async (jwt: string, storageKey = "accessTokenHash"
return jwtHash === storedJWTHash;
}
return false;
}
};

/**
* Generates an encryption key
Expand All @@ -49,7 +49,7 @@ const generateEncryptionKey = async (): Promise<CryptoKey> => {
true,
['encrypt', 'decrypt']
);
}
};

/**
* Generates an encryption key and returns it as a base64 string
Expand All @@ -60,7 +60,7 @@ const encryptKey = async (): Promise<string> => {
const exportedKey = await crypto.subtle.exportKey('raw', key);
const exportedKeyBuffer = Buffer.from(exportedKey);
return exportedKeyBuffer.toString('base64');
}
};

/**
* Decrypts the input key
Expand All @@ -76,7 +76,7 @@ const decryptKey = async (encryptedKey: string): Promise<CryptoKey> => {
true,
['encrypt', 'decrypt']
);
}
};

/**
* Encrypts the input JWT with the input key
Expand All @@ -97,7 +97,7 @@ const encryptJWT = async (jwt: string, key: CryptoKey): Promise<string> => {
const base64IV = Buffer.from(iv).toString('base64');
const base64EncryptedJWT = Buffer.from(encryptedJWT, 'binary').toString('base64');
return `${base64IV}.${base64EncryptedJWT}`;
}
};

/**
* Decrypts the input JWT with the input key
Expand All @@ -117,12 +117,12 @@ const decryptJWT = async (encryptedJWT: string, key: CryptoKey): Promise<string>
);
const decoder = new TextDecoder();
return decoder.decode(decryptedData);
}
};

export const cryptoAuthUtils = {
generateEncryptionKey,
encryptKey,
decryptKey,
encryptJWT,
decryptJWT,
}
};

0 comments on commit 9750074

Please sign in to comment.