Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose signer #193

Merged
merged 14 commits into from
Jul 15, 2022
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,46 @@ fastify.get('/', (req, rep) => {
})
```

### Other cases of manual signing / unsigning cookies

Sometimes the service under test should only accept requests with signed cookies, but it does not generate them itself.

**Example:**

```js
import { signerFactory } from "@fastify/cookie";

const signerFactory = require('@fastify/cookie/signer');
const secret = 'a secret';
const signer = signerFactory(secret);
...

test('Request requires signed cookie', async () => {
const response = await app.inject({
method: 'GET',
url: '/',
headers: {
cookies : {
'sid': signer.sign(sidValue)
}
},
});

expect(response.statusCode).toBe(200);
});
```

For cases when it is necessary to use different keys for signing/unsigning

```js
import { sign, unsign } from "@fastify/cookie";

const sid = "sid-123456";
const secret = "a secret";

const signedCookie = sign(sid, secret);
const unsignedCookie = unsign(signedCookie, secret);
```

## License

Expand Down
7 changes: 7 additions & 0 deletions cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

'use strict'

const { sign, unsign } = require('cookie-signature')
const { signerFactory } = require('./signer')

/**
* Module exports.
* @public
Expand All @@ -36,6 +39,10 @@
exports.parse = parse
exports.serialize = serialize

exports.signerFactory = signerFactory
exports.sign = sign
exports.unsign = unsign
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I landed #194 and now this conflicts. Coul you move these exports to the plugin.js file instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ок

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How then should the import of utilities look like?

const cookie = require('@fastify/cookie');
const { signerFactory , sign, unsign } = cookie;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that works

Copy link
Contributor Author

@budarin budarin Jul 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I understand correctly that exporting to plugin.js should it look like this?

/**
 * These export configurations enable JS and TS developers
 * to consume fastify-cookie in whatever way best suits their needs.
 * Some examples of supported import syntax includes:
 * - `const fastifyCookie = require('fastify-cookie')`
 * - `const { fastifyCookie } = require('fastify-cookie')`
 * - `import * as fastifyCookie from 'fastify-cookie'`
 * - `import { fastifyCookie } from 'fastify-cookie'`
 * - `import fastifyCookie from 'fastify-cookie'`
 */
fastifyCookie.fastifyCookie = fastifyCookie
fastifyCookie.default = fastifyCookie
module.exports = fastifyCookie

fastifyCookie.fastifyCookie.signerFactory = signerFactory;
fastifyCookie.fastifyCookie.sign = sign;
fastifyCookie.fastifyCookie.unsign = unsign;

module.exports.signerFactory = signerFactory;
module.exports.sign = sign;
module.exports.unsign = unsign;

Copy link
Member

@climba03003 climba03003 Jul 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either

fastifyCookie.signerFactory = signerFactory;
fastifyCookie.sign = sign;
fastifyCookie.unsign = unsign;

or

plugin.signerFactory = signerFactory;
plugin.sign = sign;
plugin.unsign = unsign;


/**
* Module variables.
* @private
Expand Down
6 changes: 5 additions & 1 deletion plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ interface Signer {
};
}

declare const signerFactory: Signer;
declare const sign: (value: string, secret: string) => string;
declare const unsign: (input: string, secret: string) => string | false;

export interface FastifyCookieOptions {
secret?: string | string[] | Signer;
parseOptions?: CookieSerializeOptions;
Expand All @@ -113,4 +117,4 @@ export interface FastifyCookieOptions {
declare const fastifyCookie: FastifyPluginCallback<NonNullable<FastifyCookieOptions>>;

export default fastifyCookie;
export { fastifyCookie };
export { fastifyCookie, signerFactory, sign, unsign };