Added details to log view in acct mngmt

This commit is contained in:
Stian Thorgersen 2014-04-04 11:01:18 +01:00
parent 42975f0edf
commit 8caf3fa83a
4 changed files with 71 additions and 8 deletions

View file

@ -5,6 +5,7 @@ import org.keycloak.audit.Event;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
@ -37,7 +38,7 @@ public class LogBean {
}
public String getEvent() {
return event.getEvent().replace('_', ' ');
return event.getEvent();
}
public String getClient() {
@ -48,6 +49,32 @@ public class LogBean {
return event.getIpAddress();
}
public List<DetailBean> getDetails() {
List<DetailBean> details = new LinkedList<DetailBean>();
for (Map.Entry<String, String> e : event.getDetails().entrySet()) {
details.add(new DetailBean(e));
}
return details;
}
}
public static class DetailBean {
private Map.Entry<String, String> entry;
public DetailBean(Map.Entry<String, String> entry) {
this.entry = entry;
}
public String getKey() {
return entry.getKey();
}
public String getValue() {
return entry.getValue();
}
}
}

View file

@ -14,6 +14,7 @@
<td>Event</td>
<td>IP</td>
<td>Client</td>
<td>Details</td>
</tr>
</thead>
@ -23,10 +24,12 @@
<td>${event.date?datetime}</td>
<td>${event.event}</td>
<td>${event.ipAddress}</td>
<td>${event.client}</td
<td>${event.client}</td>
<td><#list event.details as detail>${detail.key} = ${detail.value} <#if detail_has_next>, </#if></#list></td>
</tr>
</#list>
</tbody>
</table>
</@layout.mainLayout>

View file

@ -78,7 +78,13 @@ import javax.ws.rs.core.UriBuilder;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Variant;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
/**
@ -88,6 +94,20 @@ public class AccountService {
private static final Logger logger = Logger.getLogger(AccountService.class);
private static final String[] AUDIT_EVENTS = {Events.LOGIN, Events.LOGOUT, Events.REGISTER, Events.REMOVE_SOCIAL_LINK, Events.REMOVE_TOTP, Events.SEND_RESET_PASSWORD,
Events.SEND_VERIFY_EMAIL, Events.SOCIAL_LINK, Events.UPDATE_EMAIL, Events.UPDATE_PASSWORD, Events.UPDATE_PASSWORD, Events.UPDATE_TOTP, Events.VERIFY_EMAIL};
private static final Set<String> AUDIT_DETAILS = new HashSet<String>();
static {
AUDIT_DETAILS.add(Details.UPDATED_EMAIL);
AUDIT_DETAILS.add(Details.EMAIL);
AUDIT_DETAILS.add(Details.PREVIOUS_EMAIL);
AUDIT_DETAILS.add(Details.USERNAME);
AUDIT_DETAILS.add(Details.REMEMBER_ME);
AUDIT_DETAILS.add(Details.REGISTER_METHOD);
AUDIT_DETAILS.add(Details.AUTH_METHOD);
}
public static final String KEYCLOAK_ACCOUNT_IDENTITY_COOKIE = "KEYCLOAK_ACCOUNT_IDENTITY";
private RealmModel realm;
@ -116,7 +136,7 @@ public class AccountService {
this.realm = realm;
this.application = application;
this.audit = audit;
this.authManager = new AppAuthManager(KEYCLOAK_ACCOUNT_IDENTITY_COOKIE, tokenManager);
this.authManager = new AppAuthManager(KEYCLOAK_ACCOUNT_IDENTITY_COOKIE, tokenManager);
this.socialRequestManager = socialRequestManager;
}
@ -198,7 +218,20 @@ public class AccountService {
@GET
public Response logPage() {
if (auth != null) {
List<Event> events = auditProvider.createQuery().user(auth.getUser().getId()).maxResults(20).getResultList();
List<Event> events = auditProvider.createQuery().event(AUDIT_EVENTS).user(auth.getUser().getId()).maxResults(30).getResultList();
for (Event e : events) {
e.setEvent(e.getEvent().replace('_', ' '));
Map<String, String> details = new HashMap<String, String>();
Iterator<String> itr = e.getDetails().keySet().iterator();
for (Map.Entry<String, String> d : e.getDetails().entrySet()) {
if (AUDIT_DETAILS.contains(d.getKey())) {
details.put(d.getKey().replace('_', ' '), d.getValue());
}
}
e.setDetails(details);
}
account.setEvents(events);
}
return forwardToPage("log", AccountPages.LOG);
@ -475,7 +508,7 @@ public class AccountService {
}
if (referrerUri != null) {
return new String[] { referrer, referrerUri };
return new String[]{referrer, referrerUri};
}
} else if (referrerUri != null) {
ClientModel client = realm.getOAuthClient(referrer);
@ -483,7 +516,7 @@ public class AccountService {
referrerUri = TokenService.verifyRedirectUri(referrerUri, application);
if (referrerUri != null) {
return new String[] { referrer, referrerUri };
return new String[]{referrer, referrerUri};
}
}
}

View file

@ -132,7 +132,7 @@ public class SocialResource {
Audit audit = createAudit(realm)
.event(Events.LOGIN)
.detail(Details.RESPONSE_TYPE, "code")
.detail(Details.AUTH_METHOD, "social");
.detail(Details.AUTH_METHOD, "social@" + provider.getId());
OAuthFlows oauth = Flows.oauth(realm, request, uriInfo, authManager, tokenManager);
@ -272,7 +272,7 @@ public class SocialResource {
.event(Events.LOGIN).client(clientId)
.detail(Details.REDIRECT_URI, redirectUri)
.detail(Details.RESPONSE_TYPE, "code")
.detail(Details.AUTH_METHOD, "social");
.detail(Details.AUTH_METHOD, "social@" + providerId);
SocialProvider provider = SocialLoader.load(providerId);
if (provider == null) {