Skip to content

Commit 33a8ae7

Browse files
committed
#2 - Add example of how to use JWT authentication
1 parent 59b4876 commit 33a8ae7

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

jwt_auth/jwt_embed.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Dashboard embed with JWT auth</title>
6+
</head>
7+
<script type="module" src="piJWT/auth.js"></script>
8+
<body>
9+
<iframe id="dashboardIframe" style="width: 1024px; height: 768px"></iframe>
10+
</body>
11+
</html>

jwt_auth/keys/createKeys.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
ssh-keygen -t rsa -b 4096 -E SHA512 -m PEM -N "" -f signing_key.pkcs1
3+
openssl rsa -in signing_key.pkcs1 -pubout -outform PEM -out signing_key.pub
4+
rm signing_key.pkcs1.pub
5+
openssl pkey -in signing_key.pkcs1 -out signing_key.pkcs8
6+
rm signing_key.pkcs1

jwt_auth/piJWT/auth.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+

jwt_auth/readme.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Login with JWT
2+
3+
## Keys
4+
To login with JWT you need a private/public key pair. If you sign the JWT with your private key and provide
5+
the matching public key to the Dashboard the trust is established and you can issue login tokens. You need to set
6+
one of the supported claims:
7+
pi:dashboard_username, pi:dashboard_email and pi:dashboard_userid
8+
9+
For this demo if you haven't generated keys use keys/createKeys.sh on linux to generate test keys.
10+
11+
## Demo
12+
13+
### Prerequisites to run this demo
14+
- Setup trusted hosts in your Dashboard to allow embedding from http://localhost:8081
15+
- Add the public key from signing_key.pub to the Dashboard on the admin screen (strip the header and footer)
16+
- Check that auth.js contains valid dashboard and loginAs values
17+
18+
### Running the demo
19+
To run the demo use run.sh, it uses npx to launch a webserver and you can open localhost:8081 in a browser.
20+
If all the setup is correct you should be logged in to the Dashboard as that user.
21+
22+
## Important Notes
23+
- Don't expose your private key in production, it needs to be kept secret!
24+
- The jwt creation code would typically be run server side, this is just an example!

jwt_auth/run.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
npx http-server

0 commit comments

Comments
 (0)