daf-did-jwt
Advanced tools
Comparing version 0.9.0 to 0.10.0
@@ -1,1 +0,1 @@ | ||
export { MessageValidator, DidJwtPayloadValidator } from './message-validator'; | ||
export { MessageValidator } from './message-validator'; |
@@ -1,13 +0,4 @@ | ||
import { Core, AbstractMessageValidator, Types, Resolver } from 'daf-core'; | ||
export interface DidJwtPayloadValidator { | ||
validate: (verifiedJwt: any, didResolver: Resolver) => Promise<Types.PreValidatedMessage>; | ||
} | ||
interface Options { | ||
payloadValidators: DidJwtPayloadValidator[]; | ||
} | ||
import { Core, AbstractMessageValidator, Message } from 'daf-core'; | ||
export declare class MessageValidator extends AbstractMessageValidator { | ||
private payloadValidators; | ||
constructor(options: Options); | ||
validate(rawMessage: Types.RawMessage, core: Core): Promise<Types.PreValidatedMessage | null>; | ||
validate(message: Message, core: Core): Promise<Message>; | ||
} | ||
export {}; |
@@ -15,13 +15,2 @@ "use strict"; | ||
})(); | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -63,13 +52,2 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
}; | ||
var __values = (this && this.__values) || function(o) { | ||
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; | ||
if (m) return m.call(o); | ||
if (o && typeof o.length === "number") return { | ||
next: function () { | ||
if (o && i >= o.length) o = void 0; | ||
return { value: o && o[i++], done: !o }; | ||
} | ||
}; | ||
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -85,69 +63,29 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
__extends(MessageValidator, _super); | ||
function MessageValidator(options) { | ||
var _this = _super.call(this) || this; | ||
_this.payloadValidators = options.payloadValidators; | ||
return _this; | ||
function MessageValidator() { | ||
return _super !== null && _super.apply(this, arguments) || this; | ||
} | ||
MessageValidator.prototype.validate = function (rawMessage, core) { | ||
MessageValidator.prototype.validate = function (message, core) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var jwt, json, verified, decoded, audience, _a, _b, payloadValidator, validMessage, e_1, e_2_1, e_3; | ||
var e_2, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
var decoded, audience, verified, e_1; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
jwt = rawMessage.raw; | ||
try { | ||
json = JSON.parse(rawMessage.raw); | ||
if (json['@type'] === 'JWT') { | ||
jwt = json.data; | ||
} | ||
} | ||
catch (e) { } | ||
_d.label = 1; | ||
_a.trys.push([0, 2, , 3]); | ||
decoded = did_jwt_1.decodeJWT(message.raw); | ||
audience = decoded.payload.aud; | ||
return [4 /*yield*/, did_jwt_1.verifyJWT(message.raw, { resolver: core.didResolver, audience: audience })]; | ||
case 1: | ||
_d.trys.push([1, 13, , 14]); | ||
decoded = did_jwt_1.decodeJWT(jwt); | ||
audience = decoded.payload.aud; | ||
return [4 /*yield*/, did_jwt_1.verifyJWT(jwt, { resolver: core.didResolver, audience: audience })]; | ||
verified = _a.sent(); | ||
debug('Valid JWT.'); | ||
message.transform({ | ||
raw: message.raw, | ||
data: verified.payload, | ||
meta: { type: decoded.header.typ, id: decoded.header.alg }, | ||
}); | ||
return [3 /*break*/, 3]; | ||
case 2: | ||
verified = _d.sent(); | ||
debug('Valid JWT.'); | ||
_d.label = 3; | ||
case 3: | ||
_d.trys.push([3, 10, 11, 12]); | ||
_a = __values(this.payloadValidators), _b = _a.next(); | ||
_d.label = 4; | ||
case 4: | ||
if (!!_b.done) return [3 /*break*/, 9]; | ||
payloadValidator = _b.value; | ||
_d.label = 5; | ||
case 5: | ||
_d.trys.push([5, 7, , 8]); | ||
return [4 /*yield*/, payloadValidator.validate(verified, core.didResolver)]; | ||
case 6: | ||
validMessage = _d.sent(); | ||
return [2 /*return*/, __assign(__assign({}, validMessage), { meta: rawMessage.meta })]; | ||
case 7: | ||
e_1 = _d.sent(); | ||
return [3 /*break*/, 8]; | ||
case 8: | ||
_b = _a.next(); | ||
return [3 /*break*/, 4]; | ||
case 9: return [3 /*break*/, 12]; | ||
case 10: | ||
e_2_1 = _d.sent(); | ||
e_2 = { error: e_2_1 }; | ||
return [3 /*break*/, 12]; | ||
case 11: | ||
try { | ||
if (_b && !_b.done && (_c = _a.return)) _c.call(_a); | ||
} | ||
finally { if (e_2) throw e_2.error; } | ||
return [7 /*endfinally*/]; | ||
case 12: return [3 /*break*/, 14]; | ||
case 13: | ||
e_3 = _d.sent(); | ||
debug(e_3.message); | ||
return [3 /*break*/, 14]; | ||
case 14: return [2 /*return*/, _super.prototype.validate.call(this, rawMessage, core)]; | ||
e_1 = _a.sent(); | ||
debug(e_1.message); | ||
return [3 /*break*/, 3]; | ||
case 3: return [2 /*return*/, _super.prototype.validate.call(this, message, core)]; | ||
} | ||
@@ -154,0 +92,0 @@ }); |
@@ -6,2 +6,13 @@ # Change Log | ||
# [0.10.0](https://github.com/uport-project/daf/compare/v0.9.0...v0.10.0) (2019-12-10) | ||
### Features | ||
* Message object with validation ([8bf6a9d](https://github.com/uport-project/daf/commit/8bf6a9d47e73d6e2be9003854718b67f59c636dd)) | ||
# [0.9.0](https://github.com/uport-project/daf/compare/v0.8.0...v0.9.0) (2019-12-05) | ||
@@ -8,0 +19,0 @@ |
{ | ||
"name": "daf-did-jwt", | ||
"description": "DAF did-jwt message validator", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"main": "build/index.js", | ||
@@ -11,3 +11,3 @@ "types": "build/index.d.ts", | ||
"dependencies": { | ||
"daf-core": "^0.9.0", | ||
"daf-core": "^0.10.0", | ||
"debug": "^4.1.1", | ||
@@ -31,3 +31,3 @@ "did-jwt": "^3.0.0", | ||
"keywords": [], | ||
"gitHead": "d185517608d45eaff66690b6be2128ded6946b1c" | ||
"gitHead": "a194a5cf6c9bbe98b033f3add2044175dcc68391" | ||
} |
@@ -1,1 +0,1 @@ | ||
export { MessageValidator, DidJwtPayloadValidator } from './message-validator' | ||
export { MessageValidator } from './message-validator' |
@@ -1,2 +0,2 @@ | ||
import { Core, AbstractMessageValidator, Types, Resolver } from 'daf-core' | ||
import { Core, AbstractMessageValidator, Message, Resolver } from 'daf-core' | ||
import { verifyJWT, decodeJWT } from 'did-jwt' | ||
@@ -6,54 +6,15 @@ import Debug from 'debug' | ||
export interface DidJwtPayloadValidator { | ||
validate: ( | ||
verifiedJwt: any, | ||
didResolver: Resolver, | ||
) => Promise<Types.PreValidatedMessage> | ||
} | ||
interface Options { | ||
payloadValidators: DidJwtPayloadValidator[] | ||
} | ||
export class MessageValidator extends AbstractMessageValidator { | ||
private payloadValidators: DidJwtPayloadValidator[] | ||
constructor(options: Options) { | ||
super() | ||
this.payloadValidators = options.payloadValidators | ||
} | ||
async validate( | ||
rawMessage: Types.RawMessage, | ||
core: Core, | ||
): Promise<Types.PreValidatedMessage | null> { | ||
let jwt = rawMessage.raw | ||
async validate(message: Message, core: Core): Promise<Message> { | ||
try { | ||
// This can be DIDComm message '{"@type": "JWT", "data": "..."}' | ||
const json = JSON.parse(rawMessage.raw) | ||
if (json['@type'] === 'JWT') { | ||
jwt = json.data | ||
} | ||
} catch (e) {} | ||
let verified | ||
try { | ||
const decoded = decodeJWT(jwt) | ||
const decoded = decodeJWT(message.raw) | ||
const audience = decoded.payload.aud | ||
verified = await verifyJWT(jwt, { resolver: core.didResolver, audience }) | ||
const verified = await verifyJWT(message.raw, { resolver: core.didResolver, audience }) | ||
debug('Valid JWT.') | ||
for (const payloadValidator of this.payloadValidators) { | ||
try { | ||
const validMessage = await payloadValidator.validate( | ||
verified, | ||
core.didResolver, | ||
) | ||
return { | ||
...validMessage, | ||
meta: rawMessage.meta, | ||
} | ||
} catch (e) {} | ||
} | ||
message.transform({ | ||
raw: message.raw, | ||
data: verified.payload, | ||
meta: { type: decoded.header.typ, id: decoded.header.alg }, | ||
}) | ||
} catch (e) { | ||
@@ -63,4 +24,4 @@ debug(e.message) | ||
return super.validate(rawMessage, core) | ||
return super.validate(message, core) | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22141
132
+ Addeddaf-core@0.10.3(transitive)
- Removeddaf-core@0.9.0(transitive)
Updateddaf-core@^0.10.0