@originator-profile/securing-mechanism 0.4.0 → 0.5.0-beta.2

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 CHANGED
@@ -3,8 +3,6 @@
3
3
  var jose = require('jose');
4
4
  var cryptography = require('@originator-profile/cryptography');
5
5
  var errors = require('jose/errors');
6
- var Ajv = require('ajv');
7
- var addFormats = require('ajv-formats');
8
6
 
9
7
  class VcVerifyFailed extends Error {
10
8
  constructor(message, result) {
@@ -152,19 +150,26 @@ function JwtVcVerifier(keys, issuer, validator) {
152
150
  return verify;
153
151
  }
154
152
 
155
- function VcValidator(jsonSchema) {
156
- const ajv = new Ajv();
157
- addFormats(ajv);
158
- const validateVcPayload = ajv.compile(jsonSchema);
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) {
159
162
  function validate(vc) {
160
- if (!validateVcPayload(vc.doc)) {
161
- return new VcValidateFailed(
162
- ajv.errorsText(validateVcPayload.errors),
163
- {
164
- ...vc,
165
- error: new Ajv.ValidationError(validateVcPayload.errors ?? [])
166
- }
167
- );
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
+ });
168
173
  }
169
174
  return vc;
170
175
  }
@@ -173,6 +178,7 @@ function VcValidator(jsonSchema) {
173
178
 
174
179
  exports.JwtVcDecoder = JwtVcDecoder;
175
180
  exports.JwtVcVerifier = JwtVcVerifier;
181
+ exports.SchemaValidationError = SchemaValidationError;
176
182
  exports.VcDecodeFailed = VcDecodeFailed;
177
183
  exports.VcValidateFailed = VcValidateFailed;
178
184
  exports.VcValidator = VcValidator;
package/dist/index.d.cts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { OpVc, Jwk } from '@originator-profile/model';
2
- import { ValidationError, AnySchema } from 'ajv';
3
2
  import { JWTInvalid, JOSEError } from 'jose/errors';
4
3
  import { JWTPayload, ProtectedHeaderParameters } from 'jose';
5
4
  import { Keys } from '@originator-profile/cryptography';
5
+ import { ZodType } from 'zod';
6
6
 
7
7
  /** 未復号 VC */
8
8
  type UndecodedVc = {
@@ -32,10 +32,20 @@ type VerifiedVc<T extends OpVc = OpVc> = UnverifiedVc<T> & {
32
32
  type WithError<V extends object, E extends Error = Error> = V & {
33
33
  error: E;
34
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
+ }
35
45
  /** VC 復号失敗 */
36
46
  type VcDecodingFailure<V extends UndecodedVc = UndecodedVc, E extends Error = Error> = WithError<V, E>;
37
47
  /** VC 妥当性確認失敗 */
38
- type VcValidationFailure<V extends UnverifiedVc = UnverifiedVc> = WithError<V, ValidationError>;
48
+ type VcValidationFailure<V extends UnverifiedVc = UnverifiedVc> = WithError<V, SchemaValidationError>;
39
49
  /** VC 検証失敗 */
40
50
  type VcVerificationFailure<V extends UndecodedVc = UndecodedVc, E extends Error = Error> = WithError<V, E>;
41
51
  /** VC 復号結果 */
@@ -104,20 +114,26 @@ type JwtVcDecoder<T extends OpVc> = ReturnType<typeof JwtVcDecoder<T>>;
104
114
  declare function toUnverifiedJwtVc<T extends OpVc>(payload: JWTPayload, protectedHeader: ProtectedHeaderParameters, jwt: string): UnverifiedJwtVc<T>;
105
115
  declare function toVerifiedJwtVc<T extends OpVc>(payload: JWTPayload, protectedHeader: ProtectedHeaderParameters, jwt: string, key: Jwk): VerifiedJwtVc<T>;
106
116
 
117
+ type SignableVc = {
118
+ issuer: string;
119
+ credentialSubject: {
120
+ id: string;
121
+ };
122
+ };
107
123
  /**
108
124
  * VC への署名
109
- * @param cp CoreProfile オブジェクト
110
- * @param privateKey プライベート鍵
125
+ * @param vc VC オブジェクト
126
+ * @param privateKey プライベートキー
111
127
  * @return JWT でエンコードされた VC
112
128
  */
113
- declare function signJwtVc<T extends OpVc>(vc: T, privateKey: Jwk, options: {
129
+ declare function signJwtVc<T extends SignableVc>(vc: T, privateKey: Jwk, options: {
114
130
  alg?: string;
115
131
  issuedAt: Date;
116
132
  expiredAt: Date;
117
133
  }): Promise<string>;
118
134
 
119
135
  /** データモデルへの適合性確認のためのバリデーター */
120
- declare function VcValidator<V extends UnverifiedVc>(jsonSchema: AnySchema): (vc: V) => VcValidationResult<V, VcValidationFailure<V>>;
136
+ declare function VcValidator<V extends UnverifiedVc>(schema: ZodType): (vc: V) => VcValidationResult<V, VcValidationFailure<V>>;
121
137
  /** データモデルへの適合性確認のためのバリデーター */
122
138
  type VcValidator<V extends UnverifiedVc> = ReturnType<typeof VcValidator<V>>;
123
139
 
@@ -131,4 +147,4 @@ type VcValidator<V extends UnverifiedVc> = ReturnType<typeof VcValidator<V>>;
131
147
  declare function JwtVcVerifier<T extends OpVc>(keys: Keys, issuer: string | string[], validator?: VcValidator<VerifiedJwtVc<T>>): (jwt: string) => Promise<JwtVcVerificationResult<T>>;
132
148
  type JwtVcVerifier<T extends OpVc> = ReturnType<typeof JwtVcVerifier<T>>;
133
149
 
134
- export { JwtVcDecoder, type JwtVcDecodingFailure, type JwtVcDecodingResult, type JwtVcValidationResult, type JwtVcVerificationFailure, type JwtVcVerificationResult, JwtVcVerifier, type UndecodedJwtVc, type UndecodedVc, type UnverifiedJwtVc, type UnverifiedVc, VcDecodeFailed, type VcDecodingFailure, type VcDecodingResult, VcValidateFailed, type VcValidationFailure, type VcValidationResult, VcValidator, type VcVerificationFailure, type VcVerificationResult, VcVerifyFailed, type VerifiedJwtVc, type VerifiedVc, signJwtVc, toUnverifiedJwtVc, toVerifiedJwtVc };
150
+ export { JwtVcDecoder, type JwtVcDecodingFailure, type JwtVcDecodingResult, type JwtVcValidationResult, type JwtVcVerificationFailure, type JwtVcVerificationResult, JwtVcVerifier, type SchemaIssue, SchemaValidationError, type UndecodedJwtVc, type UndecodedVc, type UnverifiedJwtVc, type UnverifiedVc, VcDecodeFailed, type VcDecodingFailure, type VcDecodingResult, VcValidateFailed, type VcValidationFailure, type VcValidationResult, VcValidator, type VcVerificationFailure, type VcVerificationResult, VcVerifyFailed, type VerifiedJwtVc, type VerifiedVc, signJwtVc, toUnverifiedJwtVc, toVerifiedJwtVc };
package/dist/index.d.mts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { OpVc, Jwk } from '@originator-profile/model';
2
- import { ValidationError, AnySchema } from 'ajv';
3
2
  import { JWTInvalid, JOSEError } from 'jose/errors';
4
3
  import { JWTPayload, ProtectedHeaderParameters } from 'jose';
5
4
  import { Keys } from '@originator-profile/cryptography';
5
+ import { ZodType } from 'zod';
6
6
 
7
7
  /** 未復号 VC */
8
8
  type UndecodedVc = {
@@ -32,10 +32,20 @@ type VerifiedVc<T extends OpVc = OpVc> = UnverifiedVc<T> & {
32
32
  type WithError<V extends object, E extends Error = Error> = V & {
33
33
  error: E;
34
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
+ }
35
45
  /** VC 復号失敗 */
36
46
  type VcDecodingFailure<V extends UndecodedVc = UndecodedVc, E extends Error = Error> = WithError<V, E>;
37
47
  /** VC 妥当性確認失敗 */
38
- type VcValidationFailure<V extends UnverifiedVc = UnverifiedVc> = WithError<V, ValidationError>;
48
+ type VcValidationFailure<V extends UnverifiedVc = UnverifiedVc> = WithError<V, SchemaValidationError>;
39
49
  /** VC 検証失敗 */
40
50
  type VcVerificationFailure<V extends UndecodedVc = UndecodedVc, E extends Error = Error> = WithError<V, E>;
41
51
  /** VC 復号結果 */
@@ -104,20 +114,26 @@ type JwtVcDecoder<T extends OpVc> = ReturnType<typeof JwtVcDecoder<T>>;
104
114
  declare function toUnverifiedJwtVc<T extends OpVc>(payload: JWTPayload, protectedHeader: ProtectedHeaderParameters, jwt: string): UnverifiedJwtVc<T>;
105
115
  declare function toVerifiedJwtVc<T extends OpVc>(payload: JWTPayload, protectedHeader: ProtectedHeaderParameters, jwt: string, key: Jwk): VerifiedJwtVc<T>;
106
116
 
117
+ type SignableVc = {
118
+ issuer: string;
119
+ credentialSubject: {
120
+ id: string;
121
+ };
122
+ };
107
123
  /**
108
124
  * VC への署名
109
- * @param cp CoreProfile オブジェクト
110
- * @param privateKey プライベート鍵
125
+ * @param vc VC オブジェクト
126
+ * @param privateKey プライベートキー
111
127
  * @return JWT でエンコードされた VC
112
128
  */
113
- declare function signJwtVc<T extends OpVc>(vc: T, privateKey: Jwk, options: {
129
+ declare function signJwtVc<T extends SignableVc>(vc: T, privateKey: Jwk, options: {
114
130
  alg?: string;
115
131
  issuedAt: Date;
116
132
  expiredAt: Date;
117
133
  }): Promise<string>;
118
134
 
119
135
  /** データモデルへの適合性確認のためのバリデーター */
120
- declare function VcValidator<V extends UnverifiedVc>(jsonSchema: AnySchema): (vc: V) => VcValidationResult<V, VcValidationFailure<V>>;
136
+ declare function VcValidator<V extends UnverifiedVc>(schema: ZodType): (vc: V) => VcValidationResult<V, VcValidationFailure<V>>;
121
137
  /** データモデルへの適合性確認のためのバリデーター */
122
138
  type VcValidator<V extends UnverifiedVc> = ReturnType<typeof VcValidator<V>>;
123
139
 
@@ -131,4 +147,4 @@ type VcValidator<V extends UnverifiedVc> = ReturnType<typeof VcValidator<V>>;
131
147
  declare function JwtVcVerifier<T extends OpVc>(keys: Keys, issuer: string | string[], validator?: VcValidator<VerifiedJwtVc<T>>): (jwt: string) => Promise<JwtVcVerificationResult<T>>;
132
148
  type JwtVcVerifier<T extends OpVc> = ReturnType<typeof JwtVcVerifier<T>>;
133
149
 
134
- export { JwtVcDecoder, type JwtVcDecodingFailure, type JwtVcDecodingResult, type JwtVcValidationResult, type JwtVcVerificationFailure, type JwtVcVerificationResult, JwtVcVerifier, type UndecodedJwtVc, type UndecodedVc, type UnverifiedJwtVc, type UnverifiedVc, VcDecodeFailed, type VcDecodingFailure, type VcDecodingResult, VcValidateFailed, type VcValidationFailure, type VcValidationResult, VcValidator, type VcVerificationFailure, type VcVerificationResult, VcVerifyFailed, type VerifiedJwtVc, type VerifiedVc, signJwtVc, toUnverifiedJwtVc, toVerifiedJwtVc };
150
+ export { JwtVcDecoder, type JwtVcDecodingFailure, type JwtVcDecodingResult, type JwtVcValidationResult, type JwtVcVerificationFailure, type JwtVcVerificationResult, JwtVcVerifier, type SchemaIssue, SchemaValidationError, type UndecodedJwtVc, type UndecodedVc, type UnverifiedJwtVc, type UnverifiedVc, VcDecodeFailed, type VcDecodingFailure, type VcDecodingResult, VcValidateFailed, type VcValidationFailure, type VcValidationResult, VcValidator, type VcVerificationFailure, type VcVerificationResult, VcVerifyFailed, type VerifiedJwtVc, type VerifiedVc, signJwtVc, toUnverifiedJwtVc, toVerifiedJwtVc };
package/dist/index.mjs CHANGED
@@ -1,8 +1,6 @@
1
1
  import { decodeJwt, decodeProtectedHeader, importJWK, SignJWT, jwtVerify, exportJWK } from 'jose';
2
2
  import { createThumbprint } from '@originator-profile/cryptography';
3
3
  import { JOSEError } from 'jose/errors';
4
- import Ajv, { ValidationError } from 'ajv';
5
- import addFormats from 'ajv-formats';
6
4
 
7
5
  class VcVerifyFailed extends Error {
8
6
  constructor(message, result) {
@@ -150,23 +148,30 @@ function JwtVcVerifier(keys, issuer, validator) {
150
148
  return verify;
151
149
  }
152
150
 
153
- function VcValidator(jsonSchema) {
154
- const ajv = new Ajv();
155
- addFormats(ajv);
156
- const validateVcPayload = ajv.compile(jsonSchema);
151
+ class SchemaValidationError extends Error {
152
+ constructor(issues) {
153
+ super(issues.map((i) => `${i.path}: ${i.message}`).join("; "));
154
+ this.issues = issues;
155
+ this.name = "SchemaValidationError";
156
+ }
157
+ }
158
+
159
+ function VcValidator(schema) {
157
160
  function validate(vc) {
158
- if (!validateVcPayload(vc.doc)) {
159
- return new VcValidateFailed(
160
- ajv.errorsText(validateVcPayload.errors),
161
- {
162
- ...vc,
163
- error: new ValidationError(validateVcPayload.errors ?? [])
164
- }
165
- );
161
+ const result = schema.safeParse(vc.doc);
162
+ if (!result.success) {
163
+ const issues = result.error.issues.map((i) => ({
164
+ path: i.path.join("."),
165
+ message: i.message
166
+ }));
167
+ return new VcValidateFailed(result.error.message, {
168
+ ...vc,
169
+ error: new SchemaValidationError(issues)
170
+ });
166
171
  }
167
172
  return vc;
168
173
  }
169
174
  return validate;
170
175
  }
171
176
 
172
- export { JwtVcDecoder, JwtVcVerifier, VcDecodeFailed, VcValidateFailed, VcValidator, VcVerifyFailed, signJwtVc, toUnverifiedJwtVc, toVerifiedJwtVc };
177
+ export { JwtVcDecoder, JwtVcVerifier, SchemaValidationError, VcDecodeFailed, VcValidateFailed, VcValidator, VcVerifyFailed, signJwtVc, toUnverifiedJwtVc, toVerifiedJwtVc };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@originator-profile/securing-mechanism",
3
- "version": "0.4.0",
3
+ "version": "0.5.0-beta.2",
4
4
  "license": "Apache-2.0",
5
5
  "homepage": "https://docs.originator-profile.org",
6
6
  "repository": {
@@ -29,11 +29,10 @@
29
29
  "NOTICE"
30
30
  ],
31
31
  "dependencies": {
32
- "ajv": "^8.17.1",
33
- "ajv-formats": "^3.0.1",
34
32
  "jose": "^6.0.10",
35
- "@originator-profile/cryptography": "0.4.0",
36
- "@originator-profile/model": "0.4.0"
33
+ "zod": "^4.3.6",
34
+ "@originator-profile/cryptography": "0.5.0-beta.2",
35
+ "@originator-profile/model": "0.5.0-beta.2"
37
36
  },
38
37
  "devDependencies": {
39
38
  "date-fns": "^4.1.0",
@@ -41,8 +40,8 @@
41
40
  "pkgroll": "^2.12.2",
42
41
  "typescript": "^5.8.3",
43
42
  "vitest": "^4.0.0",
44
- "@originator-profile/tsconfig": "0.4.0",
45
- "eslint-config-originator-profile": "0.4.0"
43
+ "@originator-profile/tsconfig": "0.5.0-beta.2",
44
+ "eslint-config-originator-profile": "0.5.0-beta.2"
46
45
  },
47
46
  "scripts": {
48
47
  "build": "pkgroll --clean-dist --target=node20",