@originator-profile/securing-mechanism 0.5.2 → 0.5.3

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/dist/index.cjs ADDED
@@ -0,0 +1,188 @@
1
+ 'use strict';
2
+
3
+ var jose = require('jose');
4
+ var cryptography = require('@originator-profile/cryptography');
5
+ var errors = require('jose/errors');
6
+
7
+ class VcVerifyFailed extends Error {
8
+ constructor(message, result) {
9
+ super(message);
10
+ this.result = result;
11
+ }
12
+ static get code() {
13
+ return "ERR_VC_VERIFY_FAILED";
14
+ }
15
+ code = VcVerifyFailed.code;
16
+ }
17
+ class VcDecodeFailed extends Error {
18
+ constructor(message, result) {
19
+ super(message);
20
+ this.result = result;
21
+ }
22
+ static get code() {
23
+ return "ERR_VC_DECODE_FAILED";
24
+ }
25
+ code = VcDecodeFailed.code;
26
+ }
27
+ class VcValidateFailed extends Error {
28
+ constructor(message, result) {
29
+ super(message);
30
+ this.result = result;
31
+ }
32
+ static get code() {
33
+ return "ERR_VC_VALIDATION_FAILED";
34
+ }
35
+ code = VcValidateFailed.code;
36
+ }
37
+
38
+ const constructFromSymbol = Symbol.for("constructDateFrom");
39
+
40
+ function constructFrom(date, value) {
41
+ if (typeof date === "function") return date(value);
42
+ if (date && typeof date === "object" && constructFromSymbol in date)
43
+ return date[constructFromSymbol](value);
44
+ if (date instanceof Date) return new date.constructor(value);
45
+ return new Date(value);
46
+ }
47
+
48
+ function toDate(argument, context) {
49
+ return constructFrom(argument, argument);
50
+ }
51
+
52
+ function fromUnixTime(unixTime, options) {
53
+ return toDate(unixTime * 1e3);
54
+ }
55
+
56
+ function getUnixTime(date) {
57
+ return Math.trunc(+toDate(date) / 1e3);
58
+ }
59
+
60
+ function toUnverifiedJwtVc(payload, protectedHeader, jwt) {
61
+ const {
62
+ aud: _aud,
63
+ exp,
64
+ iat,
65
+ iss: _iss,
66
+ jti: _jti,
67
+ nbf: _nbf,
68
+ sub: _sub,
69
+ ...rest
70
+ } = payload;
71
+ const { alg, typ } = protectedHeader;
72
+ const doc = rest;
73
+ return {
74
+ doc,
75
+ issuedAt: iat ? fromUnixTime(iat) : void 0,
76
+ expiredAt: exp ? fromUnixTime(exp) : void 0,
77
+ mediaType: typ && `application/${typ}`,
78
+ algorithm: alg,
79
+ source: jwt
80
+ };
81
+ }
82
+ function toVerifiedJwtVc(payload, protectedHeader, jwt, key) {
83
+ const unverified = toUnverifiedJwtVc(payload, protectedHeader, jwt);
84
+ const verificationKey = {
85
+ ...key,
86
+ kid: protectedHeader.kid
87
+ };
88
+ return { ...unverified, verificationKey, validated: false };
89
+ }
90
+
91
+ function JwtVcDecoder() {
92
+ function decode(jwt) {
93
+ let payload;
94
+ let protectedHeader;
95
+ try {
96
+ payload = jose.decodeJwt(jwt);
97
+ protectedHeader = jose.decodeProtectedHeader(jwt);
98
+ } catch (e) {
99
+ const error = e;
100
+ return new VcDecodeFailed(
101
+ "JWT VC Decoding Failure",
102
+ {
103
+ source: jwt,
104
+ error
105
+ }
106
+ );
107
+ }
108
+ return toUnverifiedJwtVc(payload, protectedHeader, jwt);
109
+ }
110
+ return decode;
111
+ }
112
+
113
+ async function signJwtVc(vc, privateKey, options) {
114
+ const payload = vc;
115
+ const { alg = "ES256", issuedAt, expiredAt } = options;
116
+ const header = {
117
+ alg,
118
+ kid: privateKey.kid ?? await cryptography.createThumbprint(privateKey, alg),
119
+ typ: "vc+jwt",
120
+ cty: "vc"
121
+ };
122
+ const privateKeyImported = await jose.importJWK(privateKey, alg);
123
+ const jwt = await new jose.SignJWT(payload).setProtectedHeader(header).setIssuer(vc.issuer).setSubject(vc.credentialSubject.id).setIssuedAt(getUnixTime(issuedAt)).setExpirationTime(getUnixTime(expiredAt)).sign(privateKeyImported);
124
+ return jwt;
125
+ }
126
+
127
+ function JwtVcVerifier(keys, issuer, validator) {
128
+ async function verify(jwt) {
129
+ const verified = await jose.jwtVerify(jwt, keys, { issuer }).catch(
130
+ (e) => e
131
+ );
132
+ if (verified instanceof errors.JOSEError) {
133
+ return new VcVerifyFailed(
134
+ "JWT VC Verification Failure",
135
+ {
136
+ source: jwt,
137
+ error: verified
138
+ }
139
+ );
140
+ }
141
+ const { payload, protectedHeader, key } = verified;
142
+ const jwk = await jose.exportJWK(key);
143
+ const vc = toVerifiedJwtVc(payload, protectedHeader, jwt, jwk);
144
+ if (!validator) return vc;
145
+ const validated = validator(vc);
146
+ if (validated instanceof VcValidateFailed) return validated;
147
+ validated.validated = true;
148
+ return validated;
149
+ }
150
+ return verify;
151
+ }
152
+
153
+ class SchemaValidationError extends Error {
154
+ constructor(issues) {
155
+ super(issues.map((i) => `${i.path}: ${i.message}`).join("; "));
156
+ this.issues = issues;
157
+ this.name = "SchemaValidationError";
158
+ }
159
+ }
160
+
161
+ function VcValidator(schema) {
162
+ function validate(vc) {
163
+ const result = schema.safeParse(vc.doc);
164
+ if (!result.success) {
165
+ const issues = result.error.issues.map((i) => ({
166
+ path: i.path.join("."),
167
+ message: i.message
168
+ }));
169
+ return new VcValidateFailed(result.error.message, {
170
+ ...vc,
171
+ error: new SchemaValidationError(issues)
172
+ });
173
+ }
174
+ return vc;
175
+ }
176
+ return validate;
177
+ }
178
+
179
+ exports.JwtVcDecoder = JwtVcDecoder;
180
+ exports.JwtVcVerifier = JwtVcVerifier;
181
+ exports.SchemaValidationError = SchemaValidationError;
182
+ exports.VcDecodeFailed = VcDecodeFailed;
183
+ exports.VcValidateFailed = VcValidateFailed;
184
+ exports.VcValidator = VcValidator;
185
+ exports.VcVerifyFailed = VcVerifyFailed;
186
+ exports.signJwtVc = signJwtVc;
187
+ exports.toUnverifiedJwtVc = toUnverifiedJwtVc;
188
+ exports.toVerifiedJwtVc = toVerifiedJwtVc;
@@ -0,0 +1,151 @@
1
+ import { OpVc, Jwk } from '@originator-profile/model';
2
+ import { JWTInvalid, JOSEError } from 'jose/errors';
3
+ import { JWTPayload, ProtectedHeaderParameters } from 'jose';
4
+ import { Keys } from '@originator-profile/cryptography';
5
+ import { ZodType } from 'zod';
6
+
7
+ /** 未復号 VC */
8
+ type UndecodedVc = {
9
+ /** ソース */
10
+ source: unknown;
11
+ };
12
+ /** 未検証 VC */
13
+ type UnverifiedVc<T extends OpVc = OpVc> = UndecodedVc & {
14
+ /** VC DM 2.0 文書 */
15
+ doc: T;
16
+ /** 発行日 */
17
+ issuedAt?: Date;
18
+ /** 有効期限 */
19
+ expiredAt?: Date;
20
+ /** メディアタイプ */
21
+ mediaType?: string;
22
+ /** 暗号アルゴリズム */
23
+ algorithm?: string;
24
+ };
25
+ /** 検証済み VC */
26
+ type VerifiedVc<T extends OpVc = OpVc> = UnverifiedVc<T> & {
27
+ /** 検証鍵 */
28
+ verificationKey: Jwk;
29
+ /** 妥当性確認済みか否か */
30
+ validated: boolean;
31
+ };
32
+ type WithError<V extends object, E extends Error = Error> = V & {
33
+ error: E;
34
+ };
35
+ /** スキーマ検証エラーの issue */
36
+ type SchemaIssue = {
37
+ path: string;
38
+ message: string;
39
+ };
40
+ /** スキーマ検証エラー */
41
+ declare class SchemaValidationError extends Error {
42
+ issues: SchemaIssue[];
43
+ constructor(issues: SchemaIssue[]);
44
+ }
45
+ /** VC 復号失敗 */
46
+ type VcDecodingFailure<V extends UndecodedVc = UndecodedVc, E extends Error = Error> = WithError<V, E>;
47
+ /** VC 妥当性確認失敗 */
48
+ type VcValidationFailure<V extends UnverifiedVc = UnverifiedVc> = WithError<V, SchemaValidationError>;
49
+ /** VC 検証失敗 */
50
+ type VcVerificationFailure<V extends UndecodedVc = UndecodedVc, E extends Error = Error> = WithError<V, E>;
51
+ /** VC 復号結果 */
52
+ type VcDecodingResult<Success extends UnverifiedVc, Failure extends VcDecodingFailure> = Success | VcDecodeFailed<Failure>;
53
+ /** VC 妥当性確認結果 */
54
+ type VcValidationResult<Success extends UnverifiedVc, Failure extends VcValidationFailure> = Success | VcValidateFailed<VcValidationFailure<Failure>>;
55
+ /** VC 検証結果 */
56
+ type VcVerificationResult<Success extends VerifiedVc, Failure extends VcVerificationFailure> = Success | VcVerifyFailed<VcVerificationFailure<Failure>>;
57
+
58
+ /** VC 検証失敗 */
59
+ declare class VcVerifyFailed<T extends VcVerificationFailure> extends Error {
60
+ result: T;
61
+ static get code(): string;
62
+ readonly code: string;
63
+ constructor(message: string, result: T);
64
+ }
65
+ /** VC 復号失敗 */
66
+ declare class VcDecodeFailed<T extends VcDecodingFailure> extends Error {
67
+ result: T;
68
+ static get code(): string;
69
+ readonly code: string;
70
+ constructor(message: string, result: T);
71
+ }
72
+ /**
73
+ * VC 妥当性確認失敗
74
+ *
75
+ * VC の妥当性確認に失敗しました。次の原因で使用されます。
76
+ *
77
+ * - データモデルへの適合性確認に失敗した
78
+ *
79
+ * */
80
+ declare class VcValidateFailed<T extends VcValidationFailure> extends Error {
81
+ result: T;
82
+ static get code(): string;
83
+ readonly code: string;
84
+ constructor(message: string, result: T);
85
+ }
86
+
87
+ /** 未復号 VC */
88
+ type UndecodedJwtVc = {
89
+ /** JWT */
90
+ source: string;
91
+ };
92
+ /** 未検証 JWT VC */
93
+ type UnverifiedJwtVc<T extends OpVc> = UndecodedJwtVc & UnverifiedVc<T>;
94
+ /** 検証済み JWT VC */
95
+ type VerifiedJwtVc<T extends OpVc> = UnverifiedJwtVc<T> & VerifiedVc<T>;
96
+ /** JWT VC 復号失敗 */
97
+ type JwtVcDecodingFailure = VcDecodingFailure<UndecodedJwtVc, JWTInvalid | TypeError>;
98
+ /** JWT VC 検証失敗 */
99
+ type JwtVcVerificationFailure = VcVerificationFailure<UndecodedJwtVc, JOSEError>;
100
+ /** JWT VC 妥当性確認結果 */
101
+ type JwtVcValidationResult<T extends OpVc> = VcValidationResult<VerifiedJwtVc<T>, VcValidationFailure<VerifiedJwtVc<T>>>;
102
+ /** JWT VC 復号結果 */
103
+ type JwtVcDecodingResult<T extends OpVc> = VcDecodingResult<UnverifiedJwtVc<T>, JwtVcDecodingFailure>;
104
+ /** JWT VC 検証結果 */
105
+ type JwtVcVerificationResult<T extends OpVc> = VcVerificationResult<VerifiedJwtVc<T>, JwtVcVerificationFailure> | JwtVcValidationResult<T>;
106
+
107
+ /**
108
+ * データモデルの復号器の生成
109
+ * @return 復号器
110
+ */
111
+ declare function JwtVcDecoder<T extends OpVc>(): (jwt: string) => JwtVcDecodingResult<T>;
112
+ type JwtVcDecoder<T extends OpVc> = ReturnType<typeof JwtVcDecoder<T>>;
113
+
114
+ declare function toUnverifiedJwtVc<T extends OpVc>(payload: JWTPayload, protectedHeader: ProtectedHeaderParameters, jwt: string): UnverifiedJwtVc<T>;
115
+ declare function toVerifiedJwtVc<T extends OpVc>(payload: JWTPayload, protectedHeader: ProtectedHeaderParameters, jwt: string, key: Jwk): VerifiedJwtVc<T>;
116
+
117
+ type SignableVc = {
118
+ issuer: string;
119
+ credentialSubject: {
120
+ id: string;
121
+ };
122
+ };
123
+ /**
124
+ * VC への署名
125
+ * @param vc VC オブジェクト
126
+ * @param privateKey プライベートキー
127
+ * @return JWT でエンコードされた VC
128
+ */
129
+ declare function signJwtVc<T extends SignableVc>(vc: T, privateKey: Jwk, options: {
130
+ alg?: string;
131
+ issuedAt: Date;
132
+ expiredAt: Date;
133
+ }): Promise<string>;
134
+
135
+ /** データモデルへの適合性確認のためのバリデーター */
136
+ declare function VcValidator<V extends UnverifiedVc>(schema: ZodType): (vc: V) => VcValidationResult<V, VcValidationFailure<V>>;
137
+ /** データモデルへの適合性確認のためのバリデーター */
138
+ type VcValidator<V extends UnverifiedVc> = ReturnType<typeof VcValidator<V>>;
139
+
140
+ /**
141
+ * JWT VC の検証者の作成
142
+ * @param keys 公開鍵
143
+ * @param issuer 公開鍵の有効な発行者
144
+ * @param validator バリデーター
145
+ * @return 検証者
146
+ */
147
+ declare function JwtVcVerifier<T extends OpVc>(keys: Keys, issuer: string | string[], validator?: VcValidator<VerifiedJwtVc<T>>): (jwt: string) => Promise<JwtVcVerificationResult<T>>;
148
+ type JwtVcVerifier<T extends OpVc> = ReturnType<typeof JwtVcVerifier<T>>;
149
+
150
+ export { JwtVcDecoder, JwtVcVerifier, SchemaValidationError, VcDecodeFailed, VcValidateFailed, VcValidator, VcVerifyFailed, signJwtVc, toUnverifiedJwtVc, toVerifiedJwtVc };
151
+ export type { JwtVcDecodingFailure, JwtVcDecodingResult, JwtVcValidationResult, JwtVcVerificationFailure, JwtVcVerificationResult, SchemaIssue, UndecodedJwtVc, UndecodedVc, UnverifiedJwtVc, UnverifiedVc, VcDecodingFailure, VcDecodingResult, VcValidationFailure, VcValidationResult, VcVerificationFailure, VcVerificationResult, VerifiedJwtVc, VerifiedVc };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@originator-profile/securing-mechanism",
3
- "version": "0.5.2",
3
+ "version": "0.5.3",
4
4
  "license": "Apache-2.0",
5
5
  "homepage": "https://docs.originator-profile.org",
6
6
  "repository": {
@@ -13,8 +13,14 @@
13
13
  },
14
14
  "type": "module",
15
15
  "exports": {
16
- "types": "./dist/index.d.ts",
17
- "default": "./dist/index.js"
16
+ "require": {
17
+ "types": "./dist/index.d.cts",
18
+ "default": "./dist/index.cjs"
19
+ },
20
+ "import": {
21
+ "types": "./dist/index.d.mts",
22
+ "default": "./dist/index.mjs"
23
+ }
18
24
  },
19
25
  "files": [
20
26
  "dist",
@@ -25,20 +31,20 @@
25
31
  "dependencies": {
26
32
  "jose": "^6.2.2",
27
33
  "zod": "^4.3.6",
28
- "@originator-profile/cryptography": "0.5.2",
29
- "@originator-profile/model": "0.5.2"
34
+ "@originator-profile/model": "0.5.3",
35
+ "@originator-profile/cryptography": "0.5.3"
30
36
  },
31
37
  "devDependencies": {
32
- "date-fns": "4.1.0",
33
- "eslint": "10.2.0",
34
- "pkgroll": "2.27.0",
35
- "typescript": "6.0.2",
36
- "vitest": "4.1.4",
37
- "eslint-config-originator-profile": "0.5.2",
38
- "@originator-profile/tsconfig": "0.5.2"
38
+ "date-fns": "^4.1.0",
39
+ "eslint": "^10.1.0",
40
+ "pkgroll": "^2.27.0",
41
+ "typescript": "^6.0.2",
42
+ "vitest": "^4.1.2",
43
+ "@originator-profile/tsconfig": "0.5.3",
44
+ "eslint-config-originator-profile": "0.5.3"
39
45
  },
40
46
  "scripts": {
41
- "build": "pkgroll --clean-dist",
47
+ "build": "pkgroll --clean-dist --target=node20",
42
48
  "test": "vitest run",
43
49
  "lint": "eslint --fix .",
44
50
  "type-check": "tsc --noEmit"
File without changes
File without changes