Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the way tokens and logged in users are shown in Keycloak Dev UI #19028

Merged
merged 1 commit into from
Jul 28, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{#include main fluid=true}
{#title}Keycloak{/title}

{#script}

{#if info:oidcApplicationType is 'service'}
{#if info:oidcGrantType is 'implicit' || info:oidcGrantType is 'code'}
var accessToken;
var idToken;
var loggedIn = false;
var userName;

$( document ).ready(function() {

Expand All @@ -18,8 +18,8 @@
var hash = window.location.hash;
accessToken = hash.match(/access_token=([^&]+)/)[1];
idToken = hash.match(/id_token=([^&]+)/)[1];
$('#accessTokenArea').text(accessToken);
$('#idTokenArea').text(idToken);
$('#accessTokenArea').text(decodeToken(accessToken));
$('#idTokenArea').text(decodeToken(idToken));
}else if(codeInUrl()){
loggedIn === true;
$('.implicitLoggedOut').hide();
Expand All @@ -33,6 +33,7 @@
$('.implicitLoggedIn').hide();
accessToken = null;
idToken = null;
userName = null;
$('#accessTokenArea').text('');
$('#idTokenArea').text('');

Expand Down Expand Up @@ -151,11 +152,33 @@
var tokens = JSON.parse(data);
accessToken = tokens.access_token
idToken = tokens.id_token
$('#accessTokenArea').text(accessToken);
$('#idTokenArea').text(idToken);
$('#accessTokenArea').text(decodeToken(accessToken));
$('#idTokenArea').text(decodeToken(idToken));
});
}

function decodeToken(token) {
var parts = token.split(".");
if (parts.length == 3) {
var headers = window.atob(parts[0]);
var payload = window.atob(parts[1]);
var jsonPayload = JSON.parse(payload);
if (!userName) {
if (jsonPayload.upn) {
userName = jsonPayload.upn;
} else if (jsonPayload.preferred_username) {
userName = jsonPayload.preferred_username;
}
if (userName) {
$('#userName').append("<span class='text-primary px-1'>" + "as " + userName + "</span>");
}
}
return JSON.stringify(JSON.parse(headers), null, '\t') + "\r\n.\r\n" + JSON.stringify(jsonPayload,null,'\t') + "\r\n.\r\n" + parts[2];
} else {
return token;
}
}

{/if}
{/if}

Expand Down Expand Up @@ -242,7 +265,7 @@
</div>
<div class="float-right">
<a onclick="logout();" class="btn btn-link" title="Click to logout and start again">
<i class="fas fa-lock fa-xs"></i> Logged in
<i class="fas fa-lock fa-xs"></i> Logged in<div class="bg-light" id="userName"></div>
</a>
</div>
</div>
Expand All @@ -252,9 +275,9 @@
</a>
<div class="collapse" id="collapseAccessToken">
<div class="card card-body bg-dark">
<p id="accessTokenArea" class="text-light text-monospace user-select-all">
<pre id="accessTokenArea" class="text-light text-monospace user-select-all">

</p>
</pre>
<a class="btn btn-link shadow-none text-right text-secondary" title="Copy to clipboard" onclick="accessTokenToClipboard();">
<i class="fas fa-clipboard"></i>
</a>
Expand All @@ -268,9 +291,9 @@
</a>
<div class="collapse" id="collapseIdToken">
<div class="card card-body bg-dark">
<p id="idTokenArea" class="text-light text-monospace user-select-all">
<pre id="idTokenArea" class="text-light text-monospace user-select-all">

</p>
</pre>

<a class="btn btn-link shadow-none text-right text-secondary" title="Copy to clipboard" onclick="idTokenToClipboard();">
<i class="fas fa-clipboard"></i>
Expand Down