|
| 1 | +import { SignJWT, importPKCS8 } from "https://cdnjs.cloudflare.com/ajax/libs/jose/4.14.4/index.bundle.min.js"; |
| 2 | + |
| 3 | +const dashboard = 'http://localhost:8224'; |
| 4 | +const loginAs = 'admin'; |
| 5 | +const algorithm = 'RS512' // or RS384 or RS256 currently supported |
| 6 | + |
| 7 | +async function loginWithJWTAndIframe(){ |
| 8 | + await grabPrivateKeyString(async function (privateKeyString) { |
| 9 | + let privateKey = await importPKCS8(privateKeyString, algorithm); |
| 10 | + const jwt = await createJWT(privateKey, algorithm); |
| 11 | + await initIframe(jwt); |
| 12 | + }) |
| 13 | +} |
| 14 | + |
| 15 | +async function grabPrivateKeyString(onLoad) { |
| 16 | + const keyFile = new XMLHttpRequest(); |
| 17 | + keyFile.open("GET", "keys/signing_key.pkcs8", true); |
| 18 | + keyFile.onreadystatechange = async function() { |
| 19 | + if (keyFile.readyState === 4 && keyFile.status === 200) { |
| 20 | + const keyText = keyFile.responseText; |
| 21 | + onLoad(keyText); |
| 22 | + } |
| 23 | + } |
| 24 | + await keyFile.send(); |
| 25 | +} |
| 26 | + |
| 27 | +async function createJWT(privateKey, algorithm){ |
| 28 | + /* Supported claims are |
| 29 | + pi:dashboard_username, pi:dashboard_email and pi:dashboard_userid */ |
| 30 | + return new SignJWT({'pi:dashboard_username': loginAs}) |
| 31 | + .setProtectedHeader({alg: algorithm}) |
| 32 | + .setIssuedAt() |
| 33 | + .setExpirationTime('1h') |
| 34 | + .sign(privateKey); |
| 35 | +} |
| 36 | + |
| 37 | +async function initIframe(jwt){ |
| 38 | + document.getElementById("dashboardIframe").src = `${dashboard}/pi/?jwt=${jwt}` |
| 39 | +} |
| 40 | + |
| 41 | +loginWithJWTAndIframe(); |
| 42 | + |
| 43 | + |
0 commit comments