Skip to content

Commit

Permalink
feat: add browser support (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamtxena authored Mar 31, 2021
1 parent cd02f16 commit e3b47d5
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 37 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
coverage
dist
dist.browser
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist
dist.browser
node_modules
coverage
.env
3 changes: 2 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
coverage
dist
dist
dist.browser
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
"dist",
"dist.browser"
],
"scripts": {
"audit": "audit-ci --moderate",
"prepare": "npm run build",
"build": "npm run clean && tsc -p tsconfig.build.json",
"clean": "rm -rf ./dist ./coverage",
"build": "npm run clean && tsc -p tsconfig.build.json && tsc -p tsconfig.browser.json",
"clean": "rm -rf ./dist ./dist.browser ./coverage",
"compile": "tsc",
"compile:dev": "tsc -w",
"coverage": "jest --coverage",
Expand Down
18 changes: 12 additions & 6 deletions src/SiopDidAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,19 @@ const verifyDidAuthRequest = async (
);
if (!verificationMethod)
throw new Error(DidAuthErrors.VERIFICATION_METHOD_NOT_MATCHES);
try {
// Verify the SIOP Request according to the verification method above.
const verification = await util.verifySignatureFromVerificationMethod(
jwt,
verificationMethod
);
if (!verification) throw Error(DidAuthErrors.ERROR_VERIFYING_SIGNATURE);
} catch (error) {
throw Error(
DidAuthErrors.ERROR_VERIFYING_SIGNATURE + (error as Error).message
);
}

// Verify the SIOP Request according to the verification method above.
const verification = await util.verifySignatureFromVerificationMethod(
jwt,
verificationMethod
);
if (!verification) throw Error(DidAuthErrors.ERROR_VERIFYING_SIGNATURE);
// Additionally performs a complete token validation via vidVerifyJwt
return verifyDidAuth(jwt, opts);
};
Expand Down
46 changes: 19 additions & 27 deletions src/util/Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,40 +192,32 @@ const verifyES256K = (
jwt: string,
verificationMethod: VerificationMethod
): boolean => {
try {
const publicKey = extractPublicKeyBytes(verificationMethod);
const secp256k1 = new EC("secp256k1");
const { data, signature } = decodeJwt(jwt);
const hash = SHA("sha256").update(data).digest();
const sigObj = toSignatureObject(signature);
return secp256k1.keyFromPublic(publicKey, "hex").verify(hash, sigObj);
} catch (err) {
return false;
}
const publicKey = extractPublicKeyBytes(verificationMethod);
const secp256k1 = new EC("secp256k1");
const { data, signature } = decodeJwt(jwt);
const hash = SHA("sha256").update(data).digest();
const sigObj = toSignatureObject(signature);
return secp256k1.keyFromPublic(publicKey, "hex").verify(hash, sigObj);
};

const verifyEDDSA = async (
jwt: string,
verificationMethod: VerificationMethod
): Promise<boolean> => {
try {
let publicKey: JWK;
if (verificationMethod.publicKeyBase58)
publicKey = keyUtils.publicKeyJwkFromPublicKeyBase58(
verificationMethod.publicKeyBase58
);
if (verificationMethod.publicKeyJwk)
publicKey = verificationMethod.publicKeyJwk;
const result = await jwtVerify(
jwt,
await parseJwk(publicKey, DidAuthKeyAlgorithm.EDDSA)
let publicKey: JWK;
if (verificationMethod.publicKeyBase58)
publicKey = keyUtils.publicKeyJwkFromPublicKeyBase58(
verificationMethod.publicKeyBase58
);
if (!result || !result.payload)
throw Error(DidAuthErrors.ERROR_VERIFYING_SIGNATURE);
return true;
} catch (err) {
return false;
}
if (verificationMethod.publicKeyJwk)
publicKey = verificationMethod.publicKeyJwk;
const result = await jwtVerify(
jwt,
await parseJwk(publicKey, DidAuthKeyAlgorithm.EDDSA)
);
if (!result || !result.payload)
throw Error(DidAuthErrors.ERROR_VERIFYING_SIGNATURE);
return true;
};

const verifySignatureFromVerificationMethod = async (
Expand Down
19 changes: 19 additions & 0 deletions tsconfig.browser.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"compilerOptions": {
"resolveJsonModule": true,
"allowSyntheticDefaultImports": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"declaration": true,
"sourceMap": true,
"incremental": true,
"strict": false,
"outDir": "./dist.browser",
"target": "es5",
"module": "es2015",
"moduleResolution": "node",
"lib": ["es2016", "dom", "es5"]
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}

0 comments on commit e3b47d5

Please sign in to comment.