[keycloak-user] Getting username for Logout Event

Marek Posolda mposolda at redhat.com
Wed Feb 24 06:00:44 EST 2016


On 24/02/16 10:57, Maciek Dawidowicz wrote:
> Hello,
>
> I am trying to log information about successful login and logouts in 
> my application. I've written a simple event listener to pass data to 
> my application audit logger in correct format. In case of Login event 
> there are following details available:
> auth_method: openid-connect
> auth_type: code
> redirect_uri: 
> http://localhost:8080/auth/admin/master/console/?redirect_fragment=%2Frealms%2Fmaster%2Fevents
> consent: no_consent_required
> code_id: 28e74ada-cb0e-4901-91bb-2915f1a3b8e0
> *username: admin
> *
> however in logout event details there is only:
> redirect_uri: 
> http://localhost:8080/auth/admin/master/console/#/realms/master/events
>
> This means all i get in this event related to User is his id:
> *User: a680de68-1c9a-40dd-a642-c56d5912b7b6
>
> *
> Is there a simple way for my event listener to get username based on 
> User Id? Or perhaps a way to enable putting username in logout event 
> details?
Feel free to create JIRA for put username in logout event details.

The possibility to retrieve username in logout events is alreadt doable 
though, but it's a bit complicated. You will need to pass 
KeycloakSession to your EventListener implementation (KeycloakSession is 
available in EventListenerProviderFactory.create() ). Then once you have 
KeycloakSession you can use it to lookup realm and then username based 
on userId. Something like this:
RealmModel realm = session.getContext().getRealm();
UserModel user = session.users().getUserById(userId, realm);
String username = user.getUsername();

Marek
>
> thanks,
> Maciej Dawidowicz
>
>
> _______________________________________________
> keycloak-user mailing list
> keycloak-user at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/keycloak-user

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/keycloak-user/attachments/20160224/f6659670/attachment-0001.html 


More information about the keycloak-user mailing list