Skip to content

Commit

Permalink
feat: types (#17)
Browse files Browse the repository at this point in the history
Co-authored-by: Edward Stone <[email protected]>
  • Loading branch information
meghein and eadmundo authored Jan 29, 2024
1 parent 28d3142 commit ca8ff94
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 16 deletions.
35 changes: 19 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# fastify-timestamp-signer

A [Fastify](https://www.fastify.io/) plugin to timestamp/cryptographically sign strings and validate that signed strings have not been tampered with.
A [Fastify](https://www.fastify.io/) plugin to timestamp/cryptographically sign strings and validate that signed strings have not been tampered with.

### Usage
## Usage

```sh
npm i @autotelic/fastify-timestamp-signer
```

#### Examples
## Examples

### Plugin Usage

##### Plugin Usage
```js
// index.js
const signer = require('@autotelic/fastify-timestamp-singer')
Expand All @@ -36,36 +37,38 @@ fastify.post('/validate', async (req, reply) => {
})
```

#### Example App
### Example App

see [/example.js](./example.js) for a working example app.

### API
## API

#### Configuration
### Configuration

The plugin accepts the the following configuration properties:
- **`secret`** : `string` - The secret used to sign/ validate a string (required).

- **`algorithm`** : `string` - The algorithm used to sign/ validate a string (defaults to `sha512`).
- **`secret`** : `string` - The secret used to sign/ validate a string (required).

- **`algorithm`** : `string` - The algorithm used to sign/ validate a string (defaults to `sha512`).

- **`delimiter`**: `string` - The delimiter used to separate the raw string, timestamp and signature (defaults to `:`).
- **`delimiter`**: `string` - The delimiter used to separate the raw string, timestamp and signature (defaults to `:`).

- **`encoding`**: `string` - The encoding type used to encode the signature. (defaults to `base64`)
- **`encoding`**: `string` - The encoding type used to encode the signature. (defaults to `base64`)

#### Decorators
### Decorators

This plugin decorates fastify with the following methods:

- **`sign`**: `function` - Generates a timestamp and cryptographically signed string.
- Accepts the following arguments:
- **`sign`**: `function` - Generates a timestamp and cryptographically signed string.
- Accepts the following arguments:
- **`string`**: `string` - The raw string to be signed (required).
- **`options`**: `object` - Accepts the following parameters:
- **`salt`**: `string` - The salt used to hash the signature (required).
- **`timestamp`**: `number` - A unix timestamp used to timestamp the string (defaults to current time).
- Returns: `string` - A timestamped and cryptographically signed string.

- **`validate`**: `function` - Validates a signed string has a valid signature and has not expired.
- Accepts the following arguments:
- **`validate`**: `function` - Validates a signed string has a valid signature and has not expired.
- Accepts the following arguments:
- **`signedString`**: `string` - The signed string to be validated (required).
- **`maxAge`**: `number` - The max allowable age in minutes to validate a signed string. (defaults to 5)
- **`options`**: `object` - Accepts the following parameters:
Expand Down
39 changes: 39 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { FastifyPluginCallback } from 'fastify';

type FastifyTimestampSigner = FastifyPluginCallback<fastifyTimestampSigner.TimestampSignerOptions>;

declare module 'fastify' {
interface FastifyInstance {
sign(string: string, options?: fastifyTimestampSigner.SignOptions): Promise<string>;
validate(signedString: string, maxAge?: number, options?: fastifyTimestampSigner.ValidateOptions): Promise<boolean>;
}
}

declare namespace fastifyTimestampSigner {
export interface SignOptions {
salt: string;
timestamp?: number;
}

export interface ValidateOptions {
salt: string;
validateTime?: number;
}

export interface TimestampSignerOptions {
secret: string;
algorithm?: string;
delimiter?: string;
encoding?: string;
}

export const fastifyTimestampSigner: FastifyTimestampSigner;
export { fastifyTimestampSigner as default };
}

declare function fastifyTimestampSigner(
...params: Parameters<FastifyTimestampSigner>
): ReturnType<FastifyTimestampSigner>;

export = fastifyTimestampSigner;

0 comments on commit ca8ff94

Please sign in to comment.