KEYCLOAK-365 Add IDToken properties to JS SDK
This commit is contained in:
parent
0214827492
commit
fbc39e82a1
3 changed files with 77 additions and 12 deletions
|
@ -1,25 +1,52 @@
|
|||
<html>
|
||||
<head>
|
||||
<title>Customer View Page</title>
|
||||
<script src="/auth/js/keycloak.js"></script>
|
||||
<script src="http://localhost:8081/auth/js/keycloak.js"></script>
|
||||
</head>
|
||||
<body bgcolor="#E3F6CE">
|
||||
|
||||
<p>Goto: <a href="#" onclick="keycloak.logout()">logout</a></p>
|
||||
User <b id="username"></b> made this request.
|
||||
User <b id="subject"></b> made this request.
|
||||
<p><b>User details (from <span id="profileType"></span>)</b></p>
|
||||
<p>Username: <span id="username"></span></p>
|
||||
<p>Email: <span id="email"></span></p>
|
||||
<p>Full Name: <span id="name"></span></p>
|
||||
<p>First: <span id="givenName"></span></p>
|
||||
<p>Last: <span id="familyName"></span></p>
|
||||
|
||||
<h2>Customer Listing</h2>
|
||||
<div id="customers"></div>
|
||||
|
||||
<script>
|
||||
var keycloak = Keycloak({
|
||||
clientId: 'customer-portal',
|
||||
clientSecret: 'password',
|
||||
realm: 'demo',
|
||||
onload: 'login-required'
|
||||
});
|
||||
|
||||
var loadData = function () {
|
||||
document.getElementById('username').innerText = keycloak.username;
|
||||
document.getElementById('subject').innerText = keycloak.subject;
|
||||
|
||||
console.debug(keycloak.idToken);
|
||||
if (keycloak.idToken) {
|
||||
document.getElementById('profileType').innerText = 'IDToken';
|
||||
document.getElementById('username').innerText = keycloak.idToken.preferred_username;
|
||||
document.getElementById('email').innerText = keycloak.idToken.email;
|
||||
document.getElementById('name').innerText = keycloak.idToken.name;
|
||||
document.getElementById('givenName').innerText = keycloak.idToken.given_name;
|
||||
document.getElementById('familyName').innerText = keycloak.idToken.family_name;
|
||||
} else {
|
||||
keycloak.loadUserProfile(function() {
|
||||
document.getElementById('profileType').innerText = 'Account Service';
|
||||
document.getElementById('username').innerText = keycloak.profile.username;
|
||||
document.getElementById('email').innerText = keycloak.profile.email;
|
||||
document.getElementById('name').innerText = keycloak.profile.firstName + ' ' + keycloak.profile.lastName;
|
||||
document.getElementById('givenName').innerText = keycloak.profile.firstName;
|
||||
document.getElementById('familyName').innerText = keycloak.profile.lastName;
|
||||
}, function() {
|
||||
document.getElementById('profileType').innerText = 'Failed to retrieve user details. Please enable claims or account role';
|
||||
});
|
||||
}
|
||||
|
||||
var url = 'http://localhost:8080/database/customers';
|
||||
|
||||
|
@ -50,12 +77,11 @@ User <b id="username"></b> made this request.
|
|||
|
||||
};
|
||||
|
||||
|
||||
|
||||
var reloadData = function () {
|
||||
keycloak.onValidAccessToken(loadData, loadFailure);
|
||||
}
|
||||
keycloak.init(loadData);
|
||||
|
||||
keycloak.init(loadData, loadFailure);
|
||||
|
||||
</script>
|
||||
|
||||
|
|
|
@ -194,13 +194,22 @@ var Keycloak = function (options) {
|
|||
if (token) {
|
||||
window.oauth.token = token;
|
||||
kc.token = token;
|
||||
|
||||
kc.tokenParsed = JSON.parse(atob(token.split('.')[1]));
|
||||
kc.authenticated = true;
|
||||
kc.subject = kc.tokenParsed.sub;
|
||||
kc.realmAccess = kc.tokenParsed.realm_access;
|
||||
kc.resourceAccess = kc.tokenParsed.resource_access;
|
||||
|
||||
for (var i = 0; i < idTokenProperties.length; i++) {
|
||||
var n = idTokenProperties[i];
|
||||
if (kc.tokenParsed[n]) {
|
||||
if (!kc.idToken) {
|
||||
kc.idToken = {};
|
||||
}
|
||||
kc.idToken[n] = kc.tokenParsed[n];
|
||||
}
|
||||
}
|
||||
|
||||
setTimeout(function() {
|
||||
successCallback && successCallback({ authenticated: kc.authenticated, subject: kc.subject });
|
||||
}, 0);
|
||||
|
@ -260,6 +269,35 @@ var Keycloak = function (options) {
|
|||
var uuid = s.join('');
|
||||
return uuid;
|
||||
}
|
||||
|
||||
var idTokenProperties = [
|
||||
"name",
|
||||
"given_name",
|
||||
"family_name",
|
||||
"middle_name",
|
||||
"nickname",
|
||||
"preferred_username",
|
||||
"profile",
|
||||
"picture",
|
||||
"website",
|
||||
"email",
|
||||
"email_verified",
|
||||
"gender",
|
||||
"birthdate",
|
||||
"zoneinfo",
|
||||
"locale",
|
||||
"phone_number",
|
||||
"phone_number_verified",
|
||||
"address",
|
||||
"updated_at",
|
||||
"formatted",
|
||||
"street_address",
|
||||
"locality",
|
||||
"region",
|
||||
"postal_code",
|
||||
"country",
|
||||
"claims_locales"
|
||||
]
|
||||
}
|
||||
|
||||
window.oauth = (function () {
|
||||
|
|
|
@ -202,11 +202,12 @@ public class KeycloakServer {
|
|||
try {
|
||||
RealmManager manager = new RealmManager(session);
|
||||
|
||||
if (rep.getId() == null) {
|
||||
throw new RuntimeException("Realm id not specified");
|
||||
if (rep.getId() != null && manager.getRealm(rep.getId()) != null) {
|
||||
info("Not importing realm " + rep.getRealm() + " realm already exists");
|
||||
return;
|
||||
}
|
||||
|
||||
if (manager.getRealm(rep.getId()) != null) {
|
||||
if (manager.getRealmByName(rep.getRealm()) != null) {
|
||||
info("Not importing realm " + rep.getRealm() + " realm already exists");
|
||||
return;
|
||||
}
|
||||
|
@ -268,7 +269,7 @@ public class KeycloakServer {
|
|||
|
||||
server.deploy(di);
|
||||
|
||||
factory = ((KeycloakApplication)deployment.getApplication()).getFactory();
|
||||
factory = ((KeycloakApplication) deployment.getApplication()).getFactory();
|
||||
|
||||
setupDevConfig();
|
||||
|
||||
|
|
Loading…
Reference in a new issue