Skip to content

Commit

Permalink
[MIG] auth_jwt_demo from 14 to 16
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul committed Jun 7, 2023
1 parent c2d4b4e commit 845ffac
Show file tree
Hide file tree
Showing 7 changed files with 3,100 additions and 3,302 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# ignore demo js code
auth_jwt_demo/tests/spa/js/*.js
2 changes: 1 addition & 1 deletion auth_jwt_demo/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Auth JWT Test",
"summary": """
Test/demo module for auth_jwt.""",
"version": "14.0.1.2.0",
"version": "16.0.1.0.0",
"license": "LGPL-3",
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"maintainers": ["sbidoul"],
Expand Down
5 changes: 2 additions & 3 deletions auth_jwt_demo/tests/spa/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ <h2>SPA OIDC Authentication Sample</h2>
document.getElementById("btn-login").onclick = login;
document.getElementById("btn-logout").onclick = logout;
document.getElementById("btn-whoami").onclick = whoami;
document.getElementById(
"btn-whoami-public-or-jwt"
).onclick = whoami_public_or_jwt;
document.getElementById("btn-whoami-public-or-jwt").onclick =
whoami_public_or_jwt;
</script>
</body>
</html>
22 changes: 11 additions & 11 deletions auth_jwt_demo/tests/spa/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class InMemoryWebStorageStateStore {
}

get(key) {
let item = this._data[key];
const item = this._data[key];
return Promise.resolve(item);
}

remove(key) {
let item = this._data[key];
const item = this._data[key];
delete this._data[key];
return Promise.resolve(item);
}
Expand All @@ -30,20 +30,20 @@ class InMemoryWebStorageStateStore {
}

async function onload() {
let settings_response = await fetch("/auth_settings.json");
let settings = await settings_response.json();
const settings_response = await fetch("/auth_settings.json");
const settings = await settings_response.json();
settings.redirect_uri = window.location.href;
settings.post_logout_redirect_uri = window.location.href;
// avoid storing JWT tokens in session storage
// Avoid storing JWT tokens in session storage
settings.userStore = new InMemoryWebStorageStateStore();
client = new Oidc.UserManager(settings);
client.events.addAccessTokenExpiring(refresh);

const query = window.location.search;
if (query.includes("code=") && query.includes("state=")) {
// Process the redirect callback from the identity provider
let user = await client.signinCallback();
console.log(user); // don't do this IRL!
const user = await client.signinCallback();
console.log(user); // Don't do this IRL!
// Use replaceState to redirect the user away and remove the querystring parameters
window.history.replaceState({}, document.title, "/");
}
Expand All @@ -52,7 +52,7 @@ async function onload() {
}

async function updateUI() {
let user = await client.getUser();
const user = await client.getUser();
const isAuthenticated = Boolean(user);

document.getElementById("btn-login").disabled = isAuthenticated;
Expand All @@ -77,17 +77,17 @@ async function refresh() {
}

async function _whoami(endpoint) {
let user = await client.getUser();
const user = await client.getUser();
try {
let response = await fetch(
const response = await fetch(
"http://localhost:8069/auth_jwt_demo/keycloak" + endpoint,
{
headers: {
...(user && {Authorization: `Bearer ${user.access_token}`}),
},
}
);
let data = await response.json();
const data = await response.json();
alert(JSON.stringify(data));
} catch (error) {
alert(error);
Expand Down
Loading

0 comments on commit 845ffac

Please sign in to comment.