• dihutenosa@piefed.social
    link
    fedilink
    English
    arrow-up
    3
    ·
    edit-2
    2 days ago

    Care to try winget install jqlang.jq? Perhaps it works :) The Windows version of Git Bash provides base64 through its openssl package, along with cut. And maybe, just maybe you can present your case and get approval from IT to install needed tools for your job.

    If it doesn’t, I’m hopeful that Powershell is available? I don’t agree with certain design choices Powershell has, but it sure is powerful.

    If even bloody Powershell isn’t available, perhaps a web browser (optionally, with the dev tools) is? You can cook up a couple of lines of locally running JavaScript to do that. Basically a “website” that lives in a single file in your PC.

    Here’s a Powershell snippet (generated by a LLM) for that:

    $jwt = 'eyJ...........' # paste here
    $parts = $jwt.Split('.')
    if ($parts.Count -ne 3) { throw 'Expected a 3-part JWT' }
    
    function Decode-Base64Url($s) {
        $s = $s.Replace('-', '+').Replace('_', '/')
        [Convert]::FromBase64String($s + ('=' * ((4 - $s.Length % 4) % 4)))
    }
    
    [pscustomobject]@{
        Header       = [Text.Encoding]::UTF8.GetString((Decode-Base64Url $parts[0])) | ConvertFrom-Json
        Payload      = [Text.Encoding]::UTF8.GetString((Decode-Base64Url $parts[1])) | ConvertFrom-Json
        SignatureHex = [BitConverter]::ToString((Decode-Base64Url $parts[2]))
    }
    

    I took a sample JWT from https://www.jwt.io/, and fed it in, it’s readable enough:

    Header                Payload                                                      SignatureHex
    ------                -------                                                      ------------
    @{alg=HS256; typ=JWT} @{sub=1234567890; name=John Doe; admin=True; iat=1516239022} 28-C5-05-B0-80-D3-9C-59-B2-1B-79-CC…
    

    Here, I also had Luna slop up a webpage for the same:

    <!doctype html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>JWT Decoder</title>
    <style>
      body { max-width: 760px; margin: 2rem auto; padding: 0 1rem; font: 16px system-ui, sans-serif; }
      textarea { box-sizing: border-box; width: 100%; min-height: 7rem; }
      pre { overflow-wrap: anywhere; white-space: pre-wrap; background: #f3f3f3; padding: 1rem; }
      button { margin: .5rem 0; padding: .5rem 1rem; }
    </style>
    <h1>JWT Decoder</h1>
    <label for="token">Paste a JWT</label>
    <textarea id="token" placeholder="eyJ..."></textarea>
    <button id="decode">Decode</button>
    <p id="error" role="alert"></p>
    <div id="result" hidden>
      <h2>Header</h2><pre id="header"></pre>
      <h2>Payload</h2><pre id="payload"></pre>
      <h2>Signature (hex)</h2><pre id="signature"></pre>
    </div>
    <script>
      const decodePart = part => {
        const base64 = part.replace(/-/g, '+').replace(/_/g, '/');
        const bytes = Uint8Array.from(atob(base64 + '='.repeat((4 - base64.length % 4) % 4)), c => c.charCodeAt(0));
        return new TextDecoder().decode(bytes);
      };
    
      document.querySelector('#decode').addEventListener('click', () => {
        const parts = document.querySelector('#token').value.trim().split('.');
        const error = document.querySelector('#error');
        const result = document.querySelector('#result');
        error.textContent = '';
        result.hidden = true;
        try {
          if (parts.length !== 3) throw new Error('Expected a 3-part JWT.');
          const header = JSON.parse(decodePart(parts[0]));
          const payload = JSON.parse(decodePart(parts[1]));
          const signature = [...atob(parts[2].replace(/-/g, '+').replace(/_/g, '/') + '='.repeat((4 - parts[2].length % 4) % 4))]
            .map(c => c.charCodeAt(0).toString(16).padStart(2, '0')).join('');
          document.querySelector('#header').textContent = JSON.stringify(header, null, 2);
          document.querySelector('#payload').textContent = JSON.stringify(payload, null, 2);
          document.querySelector('#signature').textContent = signature;
          result.hidden = false;
        } catch (e) {
          error.textContent = `Could not decode JWT: ${e.message}`;
        }
      });
    </script>
    <p>Decoding does not verify the signature or validate the token.</p>