json-proof-token 0.4.1

A library implementation based on the new JOSE Working Group draft specifications: JSON Web Proof (JWP), JSON Proof Token (JPT) and JSON Proof Algorithm (JPA).
Documentation
<!--
Copyright 2023 Fondazione LINKS

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JPT Decoder</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        textarea {
            width: 100%;
            height: 100px;
        }
        pre {
            white-space: pre-wrap;
        }
    </style>
</head>
<body>
    <h1>JPT Decoder</h1>
    <label for="jptInput">Enter JPT:</label>
    <textarea id="jptInput" placeholder="Paste your JPT here..."></textarea>
    <button onclick="decodeJPT()">Decode JPT</button>

    <h2>Decoded Result:</h2>
    <div id="decodedResult">
        <section id="issuerHeaderSection">
            <h3>Issuer Protected Header:</h3>
            <pre id="decodedIssuerHeader"></pre>
        </section>
		<section id="presentationProtectedHeaderSection">
            <h3>Presentation Protected Header:</h3>
            <pre id="decodedPresentationProtectedHeader"></pre>
        </section>
        <section id="payloadsSection">
            <h3>Payloads:</h3>
            <pre id="decodedPayloads"></pre>
        </section>
        <section id="proofSection">
            <h3>Proof (encoded):</h3>
            <pre id="encodedProof"></pre>
        </section>
    </div>

    <script>
        function decodeJPT() {
            const jptInput = document.getElementById('jptInput').value.trim();

            try {
				const [issuer_header, ...restSections] = jptInput.split('.');
				
				
				let decodedPresentationProtectedHeader = '';
                let encodedPayloads;
                let proof;
				console.log(restSections);

                if (restSections.length === 2) {
                    [encodedPayloads, proof] = restSections;
                } else if (restSections.length === 3) {
                    [presentation_header, encodedPayloads, proof] = restSections;
					decodedPresentationProtectedHeader = JSON.stringify(JSON.parse(atob(presentation_header)), null, 2);
                }


				const decodedIssuerHeader = JSON.stringify(JSON.parse(atob(issuer_header)), null, 2);
                
                
                const payloads = encodedPayloads.split('~').map((payload, index) => {
                    try {
                        const indent = '    '; 
                        const indentedPayload = JSON.stringify(JSON.parse(atob(payload)), null, 2).split('\n').join(`\n${indent}`);
                        return `${indent}${indentedPayload}`;
                    } catch (error) {
                        return '    Null';
                    }
                });

                document.getElementById('decodedIssuerHeader').textContent = decodedIssuerHeader;
				document.getElementById('decodedPresentationProtectedHeader').textContent = decodedPresentationProtectedHeader;
                document.getElementById('decodedPayloads').textContent = '[\n' + payloads.join(',\n') + '\n]';
                document.getElementById('encodedProof').textContent = proof;
            } catch (error) {
				console.log(error);
                document.getElementById('decodedIssuerHeader').textContent = 'Invalid JWT';
				document.getElementById('decodedPresentationProtectedHeader').textContent = '';
                document.getElementById('decodedPayloads').textContent = '';
                document.getElementById('encodedProof').textContent = '';
            }
        }
    </script>
</body>
</html>