@signature-kit/a1 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/README.md +26 -0
- package/dist/config.d.ts +35 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +32 -0
- package/dist/signer.d.ts +35 -0
- package/dist/signer.d.ts.map +1 -0
- package/dist/signer.js +193 -0
- package/package.json +55 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0
|
|
4
|
+
|
|
5
|
+
- Initial npm-ready release for `@signature-kit/a1`.
|
|
6
|
+
- Published package metadata, MIT license, README, and package-local changelog.
|
|
7
|
+
- Ships local A1 / PKCS#12 signer adapter that exposes signing power through the core SignerAdapter contract.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 SignatureKit contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# @signature-kit/a1
|
|
2
|
+
|
|
3
|
+
Local A1 / PKCS#12 signer adapter that exposes signing power through the core SignerAdapter contract.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
bun add @signature-kit/a1 effect
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Exports
|
|
12
|
+
|
|
13
|
+
- `@signature-kit/a1/config`
|
|
14
|
+
- `@signature-kit/a1/signer`
|
|
15
|
+
|
|
16
|
+
## Runtime model
|
|
17
|
+
|
|
18
|
+
SignatureKit packages are Effect-native. Public APIs return typed `Effect.Effect` values; recoverable faults stay in the typed error channel; callers provide required services and layers explicitly at the application boundary.
|
|
19
|
+
|
|
20
|
+
## Version
|
|
21
|
+
|
|
22
|
+
Current npm release line: `0.1.0`.
|
|
23
|
+
|
|
24
|
+
## License
|
|
25
|
+
|
|
26
|
+
MIT. See `LICENSE`.
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A1 signer adapter options.
|
|
3
|
+
*/
|
|
4
|
+
import { Schema } from "effect";
|
|
5
|
+
export declare const A1SignerOptionsSchema: Schema.Struct<{
|
|
6
|
+
readonly pfx: Schema.Uint8Array;
|
|
7
|
+
readonly password: Schema.ConstraintDecoder<import("effect/Redacted").Redacted<string>, never>;
|
|
8
|
+
}>;
|
|
9
|
+
export type A1SignerOptions = (typeof A1SignerOptionsSchema)["Type"];
|
|
10
|
+
export declare const A1RemoteFetchSchema: Schema.Struct<{
|
|
11
|
+
/** The (presigned) URL the PKCS#12 bytes are fetched from with a GET. */
|
|
12
|
+
url: Schema.NonEmptyString;
|
|
13
|
+
/** Extra request headers (for auth not already baked into the URL). */
|
|
14
|
+
headers: Schema.optional<Schema.$Record<Schema.String, Schema.String>>;
|
|
15
|
+
}>;
|
|
16
|
+
export type A1RemoteFetch = (typeof A1RemoteFetchSchema)["Type"];
|
|
17
|
+
export declare const A1RemoteSourceSchema: Schema.Struct<{
|
|
18
|
+
readonly password: Schema.ConstraintDecoder<import("effect/Redacted").Redacted<string>, never>;
|
|
19
|
+
readonly url: Schema.NonEmptyString;
|
|
20
|
+
readonly headers: Schema.optional<Schema.$Record<Schema.String, Schema.String>>;
|
|
21
|
+
}>;
|
|
22
|
+
export type A1RemoteSource = (typeof A1RemoteSourceSchema)["Type"];
|
|
23
|
+
export declare const A1CertificateProfileSchema: Schema.Struct<{
|
|
24
|
+
readonly document: Schema.NonEmptyString;
|
|
25
|
+
readonly subject: Schema.NonEmptyString;
|
|
26
|
+
readonly organization: Schema.NullOr<Schema.String>;
|
|
27
|
+
readonly issuer: Schema.NonEmptyString;
|
|
28
|
+
readonly serialNumber: Schema.NonEmptyString;
|
|
29
|
+
readonly fingerprint: Schema.NonEmptyString;
|
|
30
|
+
readonly validFrom: Schema.Date;
|
|
31
|
+
readonly validTo: Schema.Date;
|
|
32
|
+
readonly daysUntilExpiry: Schema.Number;
|
|
33
|
+
}>;
|
|
34
|
+
export type A1CertificateProfile = (typeof A1CertificateProfileSchema)["Type"];
|
|
35
|
+
//# sourceMappingURL=config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,eAAO,MAAM,qBAAqB;;;EAGhC,CAAC;AACH,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,CAAC,CAAC;AASrE,eAAO,MAAM,mBAAmB;IAN9B,yEAAyE;;IAEzE,uEAAuE;;EAID,CAAC;AACzE,MAAM,MAAM,aAAa,GAAG,CAAC,OAAO,mBAAmB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEjE,eAAO,MAAM,oBAAoB;;;;EAG/B,CAAC;AACH,MAAM,MAAM,cAAc,GAAG,CAAC,OAAO,oBAAoB,CAAC,CAAC,MAAM,CAAC,CAAC;AAEnE,eAAO,MAAM,0BAA0B;;;;;;;;;;EAUrC,CAAC;AACH,MAAM,MAAM,oBAAoB,GAAG,CAAC,OAAO,0BAA0B,CAAC,CAAC,MAAM,CAAC,CAAC"}
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A1 signer adapter options.
|
|
3
|
+
*/
|
|
4
|
+
import { redactedStringSchema } from "@signature-kit/core/config";
|
|
5
|
+
import { SignatureHttpHeadersSchema } from "@signature-kit/core/http";
|
|
6
|
+
import { Schema } from "effect";
|
|
7
|
+
export const A1SignerOptionsSchema = Schema.Struct({
|
|
8
|
+
pfx: Schema.Uint8Array,
|
|
9
|
+
password: redactedStringSchema,
|
|
10
|
+
});
|
|
11
|
+
const A1RemoteLocationFields = {
|
|
12
|
+
/** The (presigned) URL the PKCS#12 bytes are fetched from with a GET. */
|
|
13
|
+
url: Schema.NonEmptyString,
|
|
14
|
+
/** Extra request headers (for auth not already baked into the URL). */
|
|
15
|
+
headers: Schema.optional(SignatureHttpHeadersSchema),
|
|
16
|
+
};
|
|
17
|
+
export const A1RemoteFetchSchema = Schema.Struct(A1RemoteLocationFields);
|
|
18
|
+
export const A1RemoteSourceSchema = Schema.Struct({
|
|
19
|
+
...A1RemoteLocationFields,
|
|
20
|
+
password: redactedStringSchema,
|
|
21
|
+
});
|
|
22
|
+
export const A1CertificateProfileSchema = Schema.Struct({
|
|
23
|
+
document: Schema.NonEmptyString,
|
|
24
|
+
subject: Schema.NonEmptyString,
|
|
25
|
+
organization: Schema.NullOr(Schema.String),
|
|
26
|
+
issuer: Schema.NonEmptyString,
|
|
27
|
+
serialNumber: Schema.NonEmptyString,
|
|
28
|
+
fingerprint: Schema.NonEmptyString,
|
|
29
|
+
validFrom: Schema.Date,
|
|
30
|
+
validTo: Schema.Date,
|
|
31
|
+
daysUntilExpiry: Schema.Number,
|
|
32
|
+
});
|
package/dist/signer.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The first e-signature adapter: A1 / PKCS#12.
|
|
3
|
+
*/
|
|
4
|
+
import { Signatures } from "@signature-kit/core/signatures";
|
|
5
|
+
import { SignatureHttpClient } from "@signature-kit/core/http";
|
|
6
|
+
import type { Certificate, SignerAdapter } from "@signature-kit/core/config";
|
|
7
|
+
import { SignatureKitError } from "@signature-kit/core/config";
|
|
8
|
+
import { Context, Effect, Layer } from "effect";
|
|
9
|
+
import { type A1CertificateProfile, type A1RemoteFetch, type A1RemoteSource, type A1SignerOptions } from "./config";
|
|
10
|
+
export type A1SignerMaterial = {
|
|
11
|
+
readonly certificate: Certificate;
|
|
12
|
+
readonly profile: A1CertificateProfile;
|
|
13
|
+
readonly signer: SignerAdapter;
|
|
14
|
+
};
|
|
15
|
+
declare const A1Signer_base: Context.ServiceClass<A1Signer, "@signature-kit/a1/Signer", A1SignerMaterial>;
|
|
16
|
+
export declare class A1Signer extends A1Signer_base {
|
|
17
|
+
}
|
|
18
|
+
/** Build an A1 signer adapter from an already-parsed core {@link Certificate}. */
|
|
19
|
+
export declare const createA1SignerAdapter: (certificate: Certificate) => SignerAdapter;
|
|
20
|
+
/** Load an A1 container and expose its parsed certificate, profile, and signer. */
|
|
21
|
+
export declare const a1SignerLayer: (options: A1SignerOptions) => Layer.Layer<A1Signer, SignatureKitError>;
|
|
22
|
+
/** Load an A1 container and build the adapter in one Effect. */
|
|
23
|
+
export declare const loadA1SignerAdapter: (options: A1SignerOptions) => Effect.Effect<SignerAdapter, SignatureKitError>;
|
|
24
|
+
/** Parse and validate the certificate metadata most app integrations store. */
|
|
25
|
+
export declare const parseA1CertificateProfile: (options: A1SignerOptions) => Effect.Effect<A1CertificateProfile, SignatureKitError>;
|
|
26
|
+
/** Load an A1 container and provide the agnostic core Signatures service. */
|
|
27
|
+
export declare const a1SignaturesLayer: (options: A1SignerOptions) => Layer.Layer<Signatures, SignatureKitError>;
|
|
28
|
+
/** Fetch the A1 PKCS#12 (.pfx) bytes from a (presigned) URL via a GET. */
|
|
29
|
+
export declare const fetchA1Pkcs12: (source: A1RemoteFetch) => Effect.Effect<Uint8Array, SignatureKitError, SignatureHttpClient>;
|
|
30
|
+
/** Provide the agnostic core Signatures service from a remote (URL) A1 container. */
|
|
31
|
+
export declare const a1SignaturesLayerFromUrl: (source: A1RemoteSource) => Layer.Layer<Signatures, SignatureKitError, SignatureHttpClient>;
|
|
32
|
+
/** Parse and validate the A1 certificate metadata from a remote (URL) container. */
|
|
33
|
+
export declare const parseA1CertificateProfileFromUrl: (source: A1RemoteSource) => Effect.Effect<A1CertificateProfile, SignatureKitError, SignatureHttpClient>;
|
|
34
|
+
export {};
|
|
35
|
+
//# sourceMappingURL=signer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"signer.d.ts","sourceRoot":"","sources":["../src/signer.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,UAAU,EAAE,MAAM,gCAAgC,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,KAAK,EAAE,WAAW,EAAsB,aAAa,EAAE,MAAM,4BAA4B,CAAC;AACjG,OAAO,EACL,iBAAiB,EAMlB,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,KAAK,EAAoB,MAAM,QAAQ,CAAC;AAClE,OAAO,EAIL,KAAK,oBAAoB,EACzB,KAAK,aAAa,EAClB,KAAK,cAAc,EACnB,KAAK,eAAe,EACrB,MAAM,UAAU,CAAC;AA6FlB,MAAM,MAAM,gBAAgB,GAAG;IAC7B,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC;IAClC,QAAQ,CAAC,OAAO,EAAE,oBAAoB,CAAC;IACvC,QAAQ,CAAC,MAAM,EAAE,aAAa,CAAC;CAChC,CAAC;;AAEF,qBAAa,QAAS,SAAQ,aAE7B;CAAG;AAoEJ,kFAAkF;AAClF,eAAO,MAAM,qBAAqB,GAAI,aAAa,WAAW,KAAG,aA4DhE,CAAC;AAiBF,mFAAmF;AACnF,eAAO,MAAM,aAAa,GAAI,SAAS,eAAe,KAAG,KAAK,CAAC,KAAK,CAAC,QAAQ,EAAE,iBAAiB,CACzC,CAAC;AAExD,gEAAgE;AAChE,eAAO,MAAM,mBAAmB,GAC9B,SAAS,eAAe,KACvB,MAAM,CAAC,MAAM,CAAC,aAAa,EAAE,iBAAiB,CACmB,CAAC;AAErE,+EAA+E;AAC/E,eAAO,MAAM,yBAAyB,GACpC,SAAS,eAAe,KACvB,MAAM,CAAC,MAAM,CAAC,oBAAoB,EAAE,iBAAiB,CACa,CAAC;AAEtE,6EAA6E;AAC7E,eAAO,MAAM,iBAAiB,GAC5B,SAAS,eAAe,KACvB,KAAK,CAAC,KAAK,CAAC,UAAU,EAAE,iBAAiB,CACkD,CAAC;AAc/F,0EAA0E;AAC1E,eAAO,MAAM,aAAa,GACxB,QAAQ,aAAa,KACpB,MAAM,CAAC,MAAM,CAAC,UAAU,EAAE,iBAAiB,EAAE,mBAAmB,CAmChE,CAAC;AAEJ,qFAAqF;AACrF,eAAO,MAAM,wBAAwB,GACnC,QAAQ,cAAc,KACrB,KAAK,CAAC,KAAK,CAAC,UAAU,EAAE,iBAAiB,EAAE,mBAAmB,CAqB9D,CAAC;AAEJ,oFAAoF;AACpF,eAAO,MAAM,gCAAgC,GAC3C,QAAQ,cAAc,KACrB,MAAM,CAAC,MAAM,CAAC,oBAAoB,EAAE,iBAAiB,EAAE,mBAAmB,CAiB1E,CAAC"}
|
package/dist/signer.js
ADDED
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The first e-signature adapter: A1 / PKCS#12.
|
|
3
|
+
*/
|
|
4
|
+
import { Signatures } from "@signature-kit/core/signatures";
|
|
5
|
+
import { SignatureHttpClient } from "@signature-kit/core/http";
|
|
6
|
+
import { SignatureKitError, SignatureKitErrorCodeValue, SignatureKitOperationValue, SignatureKitSchemaNameValue, SignInputSchema, VerifyInputSchema, } from "@signature-kit/core/config";
|
|
7
|
+
import { daysUntilExpiry, parseCertificate, toSignerIdentity } from "@signature-kit/certificates";
|
|
8
|
+
import { pemToDer } from "@signature-kit/crypto/pem";
|
|
9
|
+
import { Context, Effect, Layer, Redacted, Schema } from "effect";
|
|
10
|
+
import { A1RemoteFetchSchema, A1RemoteSourceSchema, A1SignerOptionsSchema, } from "./config";
|
|
11
|
+
const RSA_ALGORITHM_NAME = "RSASSA-PKCS1-v1_5";
|
|
12
|
+
const rsaAlgorithm = (algorithm) => ({
|
|
13
|
+
name: RSA_ALGORITHM_NAME,
|
|
14
|
+
hash: algorithm === "rsa-sha1" ? "SHA-1" : algorithm === "rsa-sha512" ? "SHA-512" : "SHA-256",
|
|
15
|
+
});
|
|
16
|
+
/** Copy into a fresh ArrayBuffer-backed view so it satisfies `BufferSource`. */
|
|
17
|
+
const toBufferSource = (data) => {
|
|
18
|
+
const copy = new Uint8Array(data.byteLength);
|
|
19
|
+
copy.set(data);
|
|
20
|
+
return copy;
|
|
21
|
+
};
|
|
22
|
+
const importKey = (format, keyData, algorithm, usage) => Effect.tryPromise({
|
|
23
|
+
try: () => crypto.subtle.importKey(format, toBufferSource(keyData), rsaAlgorithm(algorithm), false, [
|
|
24
|
+
usage,
|
|
25
|
+
]),
|
|
26
|
+
catch: () => new SignatureKitError({
|
|
27
|
+
code: SignatureKitErrorCodeValue.keyImportFailed,
|
|
28
|
+
retryable: false,
|
|
29
|
+
reason: `Failed to import ${format} key for ${algorithm}.`,
|
|
30
|
+
operation: SignatureKitOperationValue.cryptoImport,
|
|
31
|
+
}),
|
|
32
|
+
});
|
|
33
|
+
const importPrivateKey = (privateKey, algorithm) => importKey("pkcs8", pemToDer(Redacted.value(privateKey)), algorithm, "sign");
|
|
34
|
+
const importPublicKey = (publicKeyDer, algorithm) => importKey("spki", publicKeyDer, algorithm, "verify");
|
|
35
|
+
const signWithKey = (key, algorithm, content) => Effect.gen(function* () {
|
|
36
|
+
const signature = yield* Effect.tryPromise({
|
|
37
|
+
try: () => crypto.subtle.sign(rsaAlgorithm(algorithm).name, key, toBufferSource(content)),
|
|
38
|
+
catch: () => new SignatureKitError({
|
|
39
|
+
code: SignatureKitErrorCodeValue.signFailed,
|
|
40
|
+
retryable: false,
|
|
41
|
+
operation: SignatureKitOperationValue.cryptoSign,
|
|
42
|
+
}),
|
|
43
|
+
});
|
|
44
|
+
return new Uint8Array(signature);
|
|
45
|
+
});
|
|
46
|
+
const verifyWithKey = (key, algorithm, signature, content) => Effect.tryPromise({
|
|
47
|
+
try: () => crypto.subtle.verify(rsaAlgorithm(algorithm).name, key, toBufferSource(signature), toBufferSource(content)),
|
|
48
|
+
catch: () => new SignatureKitError({
|
|
49
|
+
code: SignatureKitErrorCodeValue.verifyFailed,
|
|
50
|
+
retryable: false,
|
|
51
|
+
operation: SignatureKitOperationValue.cryptoVerify,
|
|
52
|
+
}),
|
|
53
|
+
});
|
|
54
|
+
export class A1Signer extends Context.Service()("@signature-kit/a1/Signer") {
|
|
55
|
+
}
|
|
56
|
+
/** Cache WebCrypto imports per adapter and algorithm. */
|
|
57
|
+
const cachedKey = (cache, algorithm, load) => {
|
|
58
|
+
const cached = cache.get(algorithm);
|
|
59
|
+
if (cached !== undefined)
|
|
60
|
+
return Effect.succeed(cached);
|
|
61
|
+
return load(algorithm).pipe(Effect.tap((key) => Effect.sync(() => cache.set(algorithm, key))));
|
|
62
|
+
};
|
|
63
|
+
const loadA1Certificate = (options) => Schema.decodeUnknownEffect(A1SignerOptionsSchema)(options).pipe(Effect.mapError((issue) => {
|
|
64
|
+
return new SignatureKitError({
|
|
65
|
+
code: SignatureKitErrorCodeValue.invalidInput,
|
|
66
|
+
retryable: false,
|
|
67
|
+
reason: "Invalid A1 signer options.",
|
|
68
|
+
operation: SignatureKitOperationValue.schemaDecode,
|
|
69
|
+
schemaName: SignatureKitSchemaNameValue.a1SignerOptions,
|
|
70
|
+
issueMessage: String(issue),
|
|
71
|
+
});
|
|
72
|
+
}), Effect.flatMap((valid) => parseCertificate(valid.pfx, valid.password)));
|
|
73
|
+
const certificateProfile = (certificate) => Effect.gen(function* () {
|
|
74
|
+
if (!certificate.isValid) {
|
|
75
|
+
return yield* Effect.fail(new SignatureKitError({
|
|
76
|
+
code: SignatureKitErrorCodeValue.invalidInput,
|
|
77
|
+
retryable: false,
|
|
78
|
+
reason: `A1 certificate is not valid on the current date. Validity: ${certificate.validity.notBefore.toISOString()} to ${certificate.validity.notAfter.toISOString()}.`,
|
|
79
|
+
}));
|
|
80
|
+
}
|
|
81
|
+
const document = certificate.brazilian.cnpj ?? certificate.brazilian.cpf;
|
|
82
|
+
if (document === null) {
|
|
83
|
+
return yield* Effect.fail(new SignatureKitError({
|
|
84
|
+
code: SignatureKitErrorCodeValue.invalidInput,
|
|
85
|
+
retryable: false,
|
|
86
|
+
reason: "A1 certificate does not contain a Brazilian CPF or CNPJ.",
|
|
87
|
+
}));
|
|
88
|
+
}
|
|
89
|
+
return {
|
|
90
|
+
document,
|
|
91
|
+
subject: certificate.subject.commonName ?? certificate.subject.raw,
|
|
92
|
+
organization: certificate.subject.organization,
|
|
93
|
+
issuer: certificate.issuer.commonName ?? certificate.issuer.raw,
|
|
94
|
+
serialNumber: certificate.serialNumber,
|
|
95
|
+
fingerprint: certificate.fingerprint,
|
|
96
|
+
validFrom: certificate.validity.notBefore,
|
|
97
|
+
validTo: certificate.validity.notAfter,
|
|
98
|
+
daysUntilExpiry: daysUntilExpiry(certificate),
|
|
99
|
+
};
|
|
100
|
+
});
|
|
101
|
+
/** Build an A1 signer adapter from an already-parsed core {@link Certificate}. */
|
|
102
|
+
export const createA1SignerAdapter = (certificate) => {
|
|
103
|
+
const privateKeys = new Map();
|
|
104
|
+
const publicKeys = new Map();
|
|
105
|
+
const signingKey = (algorithm) => cachedKey(privateKeys, algorithm, (validAlgorithm) => importPrivateKey(certificate.privateKeyPem, validAlgorithm));
|
|
106
|
+
const verificationKey = (algorithm) => cachedKey(publicKeys, algorithm, (validAlgorithm) => importPublicKey(certificate.publicKeyDer, validAlgorithm));
|
|
107
|
+
return {
|
|
108
|
+
id: "a1",
|
|
109
|
+
inspect: () => Effect.succeed(toSignerIdentity(certificate)),
|
|
110
|
+
certificate: () => Effect.succeed(certificate),
|
|
111
|
+
importSigningKey: signingKey,
|
|
112
|
+
sign: (input) => Schema.decodeUnknownEffect(SignInputSchema)(input).pipe(Effect.mapError((issue) => {
|
|
113
|
+
return new SignatureKitError({
|
|
114
|
+
code: SignatureKitErrorCodeValue.invalidInput,
|
|
115
|
+
retryable: false,
|
|
116
|
+
reason: "Invalid sign input.",
|
|
117
|
+
operation: SignatureKitOperationValue.schemaDecode,
|
|
118
|
+
schemaName: SignatureKitSchemaNameValue.signInput,
|
|
119
|
+
issueMessage: String(issue),
|
|
120
|
+
});
|
|
121
|
+
}), Effect.flatMap((valid) => signingKey(valid.algorithm).pipe(Effect.flatMap((key) => signWithKey(key, valid.algorithm, valid.content)), Effect.map((signature) => ({ algorithm: valid.algorithm, signature }))))),
|
|
122
|
+
verify: (input) => Schema.decodeUnknownEffect(VerifyInputSchema)(input).pipe(Effect.mapError((issue) => {
|
|
123
|
+
return new SignatureKitError({
|
|
124
|
+
code: SignatureKitErrorCodeValue.invalidInput,
|
|
125
|
+
retryable: false,
|
|
126
|
+
reason: "Invalid verify input.",
|
|
127
|
+
operation: SignatureKitOperationValue.schemaDecode,
|
|
128
|
+
schemaName: SignatureKitSchemaNameValue.verifyInput,
|
|
129
|
+
issueMessage: String(issue),
|
|
130
|
+
});
|
|
131
|
+
}), Effect.flatMap((valid) => verificationKey(valid.algorithm).pipe(Effect.flatMap((key) => verifyWithKey(key, valid.algorithm, valid.signature, valid.content)), Effect.map((val32) => ({ valid: val32, algorithm: valid.algorithm }))))),
|
|
132
|
+
};
|
|
133
|
+
};
|
|
134
|
+
const loadA1SignerMaterial = (options) => loadA1Certificate(options).pipe(Effect.flatMap((certificate) => certificateProfile(certificate).pipe(Effect.map((profile) => ({
|
|
135
|
+
certificate,
|
|
136
|
+
profile,
|
|
137
|
+
signer: createA1SignerAdapter(certificate),
|
|
138
|
+
})))));
|
|
139
|
+
/** Load an A1 container and expose its parsed certificate, profile, and signer. */
|
|
140
|
+
export const a1SignerLayer = (options) => Layer.effect(A1Signer, loadA1SignerMaterial(options));
|
|
141
|
+
/** Load an A1 container and build the adapter in one Effect. */
|
|
142
|
+
export const loadA1SignerAdapter = (options) => loadA1Certificate(options).pipe(Effect.map(createA1SignerAdapter));
|
|
143
|
+
/** Parse and validate the certificate metadata most app integrations store. */
|
|
144
|
+
export const parseA1CertificateProfile = (options) => loadA1Certificate(options).pipe(Effect.flatMap(certificateProfile));
|
|
145
|
+
/** Load an A1 container and provide the agnostic core Signatures service. */
|
|
146
|
+
export const a1SignaturesLayer = (options) => Layer.effect(Signatures, loadA1Certificate(options).pipe(Effect.map(createA1SignerAdapter)));
|
|
147
|
+
// ---------------------------------------------------------------------------
|
|
148
|
+
// Remote A1 material — fetch the PKCS#12 (.pfx) from a (presigned) URL.
|
|
149
|
+
//
|
|
150
|
+
// The A1 container usually lives in object storage (e.g. S3) and is handed to
|
|
151
|
+
// the signer as a short-lived presigned URL. Fetching is the ONLY thing that
|
|
152
|
+
// changes versus the local-bytes path: the fetched bytes flow into exactly the
|
|
153
|
+
// same loadA1Certificate -> createA1SignerAdapter pipeline, so the signer, the
|
|
154
|
+
// profile, and the Signatures layer behave identically. The private key never
|
|
155
|
+
// leaves this process — only the encrypted PKCS#12 is fetched, then decrypted
|
|
156
|
+
// locally with the Redacted password.
|
|
157
|
+
// ---------------------------------------------------------------------------
|
|
158
|
+
/** Fetch the A1 PKCS#12 (.pfx) bytes from a (presigned) URL via a GET. */
|
|
159
|
+
export const fetchA1Pkcs12 = (source) => Schema.decodeUnknownEffect(A1RemoteFetchSchema)(source).pipe(Effect.mapError((issue) => new SignatureKitError({
|
|
160
|
+
code: SignatureKitErrorCodeValue.invalidInput,
|
|
161
|
+
retryable: false,
|
|
162
|
+
operation: SignatureKitOperationValue.schemaDecode,
|
|
163
|
+
schemaName: SignatureKitSchemaNameValue.a1RemoteFetch,
|
|
164
|
+
issueMessage: String(issue),
|
|
165
|
+
})), Effect.flatMap((valid) => SignatureHttpClient.use((http) => http
|
|
166
|
+
.requestBytes({
|
|
167
|
+
method: "GET",
|
|
168
|
+
url: valid.url,
|
|
169
|
+
...(valid.headers === undefined ? {} : { headers: valid.headers }),
|
|
170
|
+
})
|
|
171
|
+
.pipe(Effect.flatMap((bytes) => bytes.byteLength === 0
|
|
172
|
+
? Effect.fail(new SignatureKitError({
|
|
173
|
+
code: SignatureKitErrorCodeValue.emptyFile,
|
|
174
|
+
retryable: false,
|
|
175
|
+
reason: "The fetched A1 certificate is empty.",
|
|
176
|
+
}))
|
|
177
|
+
: Effect.succeed(bytes))))));
|
|
178
|
+
/** Provide the agnostic core Signatures service from a remote (URL) A1 container. */
|
|
179
|
+
export const a1SignaturesLayerFromUrl = (source) => Layer.effect(Signatures, Schema.decodeUnknownEffect(A1RemoteSourceSchema)(source).pipe(Effect.mapError((issue) => new SignatureKitError({
|
|
180
|
+
code: SignatureKitErrorCodeValue.invalidInput,
|
|
181
|
+
retryable: false,
|
|
182
|
+
operation: SignatureKitOperationValue.schemaDecode,
|
|
183
|
+
schemaName: SignatureKitSchemaNameValue.a1RemoteSource,
|
|
184
|
+
issueMessage: String(issue),
|
|
185
|
+
})), Effect.flatMap((valid) => fetchA1Pkcs12(valid).pipe(Effect.flatMap((pfx) => loadA1Certificate({ pfx, password: valid.password })), Effect.map(createA1SignerAdapter)))));
|
|
186
|
+
/** Parse and validate the A1 certificate metadata from a remote (URL) container. */
|
|
187
|
+
export const parseA1CertificateProfileFromUrl = (source) => Schema.decodeUnknownEffect(A1RemoteSourceSchema)(source).pipe(Effect.mapError((issue) => new SignatureKitError({
|
|
188
|
+
code: SignatureKitErrorCodeValue.invalidInput,
|
|
189
|
+
retryable: false,
|
|
190
|
+
operation: SignatureKitOperationValue.schemaDecode,
|
|
191
|
+
schemaName: SignatureKitSchemaNameValue.a1RemoteSource,
|
|
192
|
+
issueMessage: String(issue),
|
|
193
|
+
})), Effect.flatMap((valid) => fetchA1Pkcs12(valid).pipe(Effect.flatMap((pfx) => parseA1CertificateProfile({ pfx, password: valid.password })))));
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@signature-kit/a1",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"license": "MIT",
|
|
5
|
+
"files": [
|
|
6
|
+
"dist",
|
|
7
|
+
"README.md",
|
|
8
|
+
"LICENSE",
|
|
9
|
+
"CHANGELOG.md"
|
|
10
|
+
],
|
|
11
|
+
"type": "module",
|
|
12
|
+
"sideEffects": false,
|
|
13
|
+
"exports": {
|
|
14
|
+
"./config": {
|
|
15
|
+
"types": "./dist/config.d.ts",
|
|
16
|
+
"import": "./dist/config.js"
|
|
17
|
+
},
|
|
18
|
+
"./signer": {
|
|
19
|
+
"types": "./dist/signer.d.ts",
|
|
20
|
+
"import": "./dist/signer.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"publishConfig": {
|
|
24
|
+
"access": "public"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "tsc -b tsconfig.json",
|
|
28
|
+
"typecheck": "tsc -p tsconfig.json --noEmit",
|
|
29
|
+
"test": "vitest run"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@signature-kit/certificates": "^0.1.0",
|
|
33
|
+
"@signature-kit/core": "^0.1.0",
|
|
34
|
+
"@signature-kit/crypto": "^0.1.0",
|
|
35
|
+
"effect": "4.0.0-beta.86"
|
|
36
|
+
},
|
|
37
|
+
"nx": {
|
|
38
|
+
"targets": {
|
|
39
|
+
"npm-pack": {
|
|
40
|
+
"executor": "nx:run-commands",
|
|
41
|
+
"options": {
|
|
42
|
+
"command": "npm pack --dry-run --json",
|
|
43
|
+
"cwd": "{projectRoot}"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"npm-publish": {
|
|
47
|
+
"executor": "nx:run-commands",
|
|
48
|
+
"options": {
|
|
49
|
+
"command": "npm publish --access public",
|
|
50
|
+
"cwd": "{projectRoot}"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
}
|