Skip to content

Commit

Permalink
Merge pull request #1 from schlagtim/c2bo/example
Browse files Browse the repository at this point in the history
WIP: Highlight digests of disclosures on mouse events
  • Loading branch information
schlagtim authored Feb 13, 2024
2 parents 15a30a3 + 857f91b commit f11dc3a
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 7 deletions.
33 changes: 32 additions & 1 deletion src/lib/sd-jwt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SdJwt } from "@sd-jwt/core";
import { SdJwt, type HasherAndAlgorithm, HasherAlgorithm } from "@sd-jwt/core";

export function splitJwt(text: string): string[] {
const result = text.split(".");
Expand Down Expand Up @@ -57,3 +57,34 @@ export function decodeSdJwt(encodedJwt: string) {
return undefined;
}
}

export default crypto;
export function provideHasher(alg: string) {
let browserAlg: string = "";
switch (alg.toLowerCase()) {
case "sha-256":
browserAlg = "SHA-256";
break;
case "sha-384":
browserAlg = "SHA-384";
break;
case "sha-512":
browserAlg = "SHA-512";
break;
}
var enc = new TextEncoder();
const hasherAndAlgorithm: HasherAndAlgorithm = {
// TODO: how do you properly cast this?
hasher: (input: string) =>
crypto.subtle
.digest(browserAlg, enc.encode(input))
.then((val) => {
return new Uint8Array(val);
})
.catch((err) => {
return new Uint8Array(0);
}),
algorithm: alg,
};
return hasherAndAlgorithm;
}
18 changes: 13 additions & 5 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
<script lang="ts">
import { decodeSdJwt, formatJsonObject } from "$lib/sd-jwt";
import { Disclosure } from "@sd-jwt/core";
import { decodeSdJwt, formatJsonObject, provideHasher } from "$lib/sd-jwt";
import Disclosures from "./Disclosures.svelte";
import Editor from "./Editor.svelte";
import type { DisclosureWithDigest } from "@sd-jwt/core/build/sdJwt";
import { onMount } from "svelte";
let encodedJwt: string | undefined =
"eyJhbGciOiJFZERTQSIsInR5cCI6InNkLWp3dCJ9.eyJpYXQiOjE3MDc2NjcxNTM2MzQsImlzcyI6ImRpZDprZXk6c29tZ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Yitqd3QiLCJhbGciOiJFZERTQSJ9.eyJpYXQiOjE3MDc2NjcxNTM2MzMsImF1ZCI6ImRpZDpwZWVyOjQ6c29tZS12ZXJpZmllciIsIm5vbmNlIjoiaVloOXBoU3ZWWTFVcUpsX05pNklJUSIsIl9zZF9oYXNoIjoiRTYwNWJfbnJPallsdUlSbktfQThKNTNhemwwcG8wcThBbHBJczZrQm5JWSJ9.6ZAydMHRVByM02Z79zQSWuZU3ZfNIkmVrMXM2ZVR-nN92h_J9D5-2cB7gPZ3aDP3Z-BY1Wj2kp_cIakv5ji3Cw";
let jwtHeader = "";
let jwtPayload = "";
let jwtSignature = "";
let disclosures: Promise<DisclosureWithDigest[] | undefined> | undefined;
let alg: any;
let jwtPayloadSelection = "credential";
let disclosures: Disclosure[] = [];
$: sdJWt = encodedJwt ? decodeSdJwt(encodedJwt) : undefined;
$: jwtHeader = formatJsonObject(sdJWt?.header);
$: jwtPayload = formatJsonObject(sdJWt?.payload);
$: jwtSignature = sdJWt?.signature ? sdJWt?.signature.toLocaleString() : "";
$: disclosures = sdJWt?.disclosures ? sdJWt?.disclosures : [];
$: alg = sdJWt ? sdJWt?.payload["_sd_alg"] : "";
$: disclosures = sdJWt
? sdJWt?.withHasher(provideHasher(alg)).disclosuresWithDigest()
: undefined;
</script>

<svelte:head>
Expand All @@ -43,7 +47,11 @@
<Editor title="Signature" value={jwtSignature} emitChanges={false}></Editor>
</div>
<div class="column" style="flex: 1;">
<Disclosures {disclosures}></Disclosures>
{#await disclosures then disclosures}
<Disclosures bind:jwtPayloadSelection {disclosures}></Disclosures>
{:catch error}
<p style="color: red">{error.message}</p>
{/await}
</div>
</div>
</section>
Expand Down
Binary file added src/routes/.Disclosures.svelte.swp
Binary file not shown.
10 changes: 9 additions & 1 deletion src/routes/Disclosures.svelte
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
<script lang="ts">
import { page } from "$app/stores";
import type { Disclosure } from "@sd-jwt/core";
export let disclosures: Disclosure[] = [];
export let jwtPayloadSelection: string = "";
</script>

<div class="box">
<div class="stripes">
<h5>DISCLOSURES</h5>
</div>
{#each disclosures as disclosure}
<div class="stripes">
<div
class="stripes"
on:mouseover={() => {
jwtPayloadSelection = disclosure.digest ? disclosure.digest : "";
console.log("Hovering ", jwtPayloadSelection);
}}
>
<p style="text-align: center;"><small><b>{disclosure.key}</b></small></p>
<div style="background-color: whitesmoke;">
<p>
Expand Down
1 change: 1 addition & 0 deletions src/routes/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
}
$: if (selectText != null) {
selectedText ? selectText(selectedText) : undefined;
console.log("setting hover target", selectedText);
}
onDestroy(() => {
Expand Down

0 comments on commit f11dc3a

Please sign in to comment.