Skip to content

Commit

Permalink
Add custom signer (#3)
Browse files Browse the repository at this point in the history
* Add ability to specify a custom signer during the `sign` call

* Add changeset

* Add release script

* Install pnpm
  • Loading branch information
Dave Kozma committed Jun 28, 2023
1 parent 17f6552 commit f9c21f4
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/slimy-guests-swim.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'c2pa-node': patch
---

Add ability to specify a custom signer during the `sign` call
13 changes: 10 additions & 3 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: "16.x"
- run: npm install
- run: npm run build
- run: npm run ci

- uses: pnpm/action-setup@v2
name: Install pnpm
id: pnpm-install
with:
version: 8

- run: pnpm install
- run: pnpm run build
- run: pnpm run ci
39 changes: 39 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Release

on:
push:
branches:
- main

concurrency: ${{ github.workflow }}-${{ github.ref }}

jobs:
release:
name: Release
runs-on: ubuntu-latest
steps:
- name: Checkout Repo
uses: actions/checkout@v3

- name: Setup Node.js 16.x
uses: actions/setup-node@v3
with:
node-version: 16.x

- uses: pnpm/action-setup@v2
name: Install pnpm
id: pnpm-install
with:
version: 8

- name: Install Dependencies
run: pnpm install

- name: Create Release Pull Request or Publish to npm
id: changesets
uses: changesets/action@v1
with:
publish: pnpm release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
12 changes: 10 additions & 2 deletions js-src/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,15 @@ export interface SignOptions {
}

export interface SignProps {
// The asset to sign
asset: Asset;
// The manifest to sign and optionally embed
manifest: ManifestBuilder;
// Allows you to pass in a thumbnail to be used instead of generating one, or `false` to prevent thumbnail generation
thumbnail?: Asset | false;
// Allows you to pass in a custom signer for this operation instead of using the global signer (if passed)
signer?: Signer;
// Options for this operation
options?: SignOptions;
}

Expand All @@ -195,11 +201,13 @@ export function createSign(globalOptions: C2paOptions) {
asset,
manifest,
thumbnail,
signer: customSigner,
options,
}: SignProps): Promise<SignOutput> {
const signOptions = Object.assign({}, defaultSignOptions, options);
const signer = customSigner ?? globalOptions.signer;

if (!globalOptions.signer) {
if (!signer) {
throw new MissingSignerError();
}
if (!signOptions.embed && !signOptions.remoteManifestUrl) {
Expand All @@ -210,7 +218,7 @@ export function createSign(globalOptions: C2paOptions) {
const { mimeType, buffer } = asset;
const signOpts = {
format: mimeType,
signer: globalOptions.signer,
signer,
embed: signOptions.embed,
remoteManifestUrl: signOptions.remoteManifestUrl,
};
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"build:docs": "typedoc --plugin typedoc-plugin-markdown --out docs js-src/index.ts",
"build": "run-s build:rust build:ts build:assets build:docs",
"ci": "cargo test --release && jest",
"publish": "changeset publish",
"release": "run-s build publish",
"test": "cargo test && jest",
"test:watch": "cargo test && jest --watch --runInBand --detectOpenHandles --forceExit",
"clean": "rimraf dist generated target"
Expand Down
29 changes: 28 additions & 1 deletion tests/sign.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ describe('sign()', () => {
let mockRemoteService: Scope;

afterEach(async () => {
mockRemoteService?.done();
nock.restore();
});

Expand Down Expand Up @@ -280,6 +279,34 @@ describe('sign()', () => {
);

expect(validation_status.length).toEqual(0);

// Calls should be made to the mock service
expect(mockRemoteService.isDone()).toBeTruthy();
});

test('should be able to override the signer during the sign call', async () => {
mockRemoteService = createSuccessRemoteServiceMock();
const signer = createRemoteSigner();
const c2pa = createC2pa({
signer,
});
const fixture = await readFile('tests/fixtures/A.jpg');
const asset: Asset = { mimeType: 'image/jpeg', buffer: fixture };
const manifest = new ManifestBuilder({
claim_generator: 'my-app/1.0.0',
format: 'image/jpeg',
title: 'node_test_local_signer.jpg',
});
const { signedAsset } = await c2pa.sign({
asset,
manifest,
signer: await createTestSigner(),
});

await c2pa.read(signedAsset);

// Calls should not be made to the mock service
expect(mockRemoteService.isDone()).toBeFalsy();
});
});
});

0 comments on commit f9c21f4

Please sign in to comment.