Skip to content

Commit

Permalink
Share common code in test pages
Browse files Browse the repository at this point in the history
Signed-off-by: John Gomersall <[email protected]>
  • Loading branch information
john-gom committed Jan 8, 2025
1 parent ead23a8 commit 1a76134
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 26 deletions.
16 changes: 2 additions & 14 deletions tests/client/code.html
Original file line number Diff line number Diff line change
@@ -1,23 +1,11 @@
<script>
(async () => {
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get("code");
const clientId = urlParams.get("clientId");
const clientSecret = urlParams.get("clientSecret");
const keycloak = decodeURIComponent(urlParams.get("keycloak"));
const lang = urlParams.get("lang");
const {code, clientId, clientSecret, keycloak, lang, error, login} = await import("./common.js");

if (!code) {
const error = urlParams.get("error");
// Try again if we get a temporarily unavailable error
if (error === "temporarily_unavailable") {
const nonce = crypto.getRandomValues(new BigUint64Array(1))[0];
const url = `${keycloak}/protocol/openid-connect/auth?response_type=code&client_id=${clientId}&redirect_uri=${encodeURIComponent(
`http://localhost:5604/code.html?clientId=${clientId}&clientSecret=${clientSecret}&lang=${lang}&keycloak=${encodeURIComponent(
keycloak
)}`
)}&scope=openid+profile+offline_access&state=${nonce}&ui_locales=${lang}`;
window.location = url;
login();
}
}

Expand Down
16 changes: 16 additions & 0 deletions tests/client/common.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const urlParams = new URLSearchParams(window.location.search);

export const clientId = urlParams.get('clientId');
export const clientSecret = urlParams.get('clientSecret');
export const lang = urlParams.get('lang');
export const keycloak = decodeURIComponent(urlParams.get('keycloak'));
export const code = urlParams.get("code");
export const error = urlParams.get("error");

export function login() {
const nonce = crypto.getRandomValues(new BigUint64Array(1))[0];
const url = `${keycloak}/protocol/openid-connect/auth?response_type=code&client_id=${clientId}&redirect_uri=${
encodeURIComponent(`http://localhost:5604/code.html?clientId=${clientId}&clientSecret=${clientSecret}&lang=${lang}&keycloak=${encodeURIComponent(keycloak)}`)
}&scope=openid+profile+offline_access&state=${nonce}&ui_locales=${lang}`;
window.location = url;
}
15 changes: 3 additions & 12 deletions tests/client/index.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
<script>
const urlParams = new URLSearchParams(window.location.search);

const clientId = urlParams.get('clientId');
const clientSecret = urlParams.get('clientSecret');
const lang = urlParams.get('lang');
const keycloak = decodeURIComponent(urlParams.get('keycloak'));
const nonce = crypto.getRandomValues(new BigUint64Array(1))[0];
function login() {
const url = `${keycloak}/protocol/openid-connect/auth?response_type=code&client_id=${clientId}&redirect_uri=${
encodeURIComponent(`http://localhost:5604/code.html?clientId=${clientId}&clientSecret=${clientSecret}&lang=${lang}&keycloak=${encodeURIComponent(keycloak)}`)
}&scope=openid+profile+offline_access&state=${nonce}&ui_locales=${lang}`;
window.location = url;
async function login() {
const common = await import("./common.js");
common.login();
}
</script>
<body>
Expand Down

0 comments on commit 1a76134

Please sign in to comment.