<div dir="ltr">Are you identifying you user mainly by the email?<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jul 10, 2015 at 6:24 PM, Scott Dunbar <span dir="ltr"><<a href="mailto:scott@xigole.com" target="_blank">scott@xigole.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000">
It is injected into the bean - sorry, might not have been enough
code before. A small example:<br>
<pre>import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;</pre>
<pre>import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;</pre><span class="">
<pre>import org.keycloak.KeycloakPrincipal;
import org.keycloak.KeycloakSecurityContext;
import org.keycloak.representations.IDToken;</pre>
<pre></pre>
</span><pre>@Path("/user")
@Stateless
public class UserService {
private static final Log log = LogFactory.getLog(UserService.class);</pre>
<pre> @Resource
private SessionContext sessionContext;
@Path("/getCurrentUserInfo")
@Produces({ MediaType.APPLICATION_JSON })
@GET
@RolesAllowed({"someRole"})
public Response getCurrentUser() {
        @SuppressWarnings("unchecked")
KeycloakPrincipal<KeycloakSecurityContext> kcPrincipal = (KeycloakPrincipal<KeycloakSecurityContext>)(sessionContext.getCallerPrincipal());
IDToken idToken = kcPrincipal.getKeycloakSecurityContext().getIdToken();
log.debug( "email from token is \"" + idToken.getEmail() + "\"" ); </pre>
<pre> // your return is likely something more useful
return Response.ok().build();
}
}</pre>
<br>
Your use case might be different but this is how it is working for
me. Again, there may be a better way.<div><div class="h5"><br>
<br>
<br>
<br>
<div>On 07/10/2015 05:01 PM, Juan Diego
wrote:<br>
</div>
<blockquote type="cite">
<div dir="ltr">Where do you get sessionContext from?<br>
</div>
<div class="gmail_extra"><br>
<div class="gmail_quote">On Fri, Jul 10, 2015 at 5:54 PM, Scott
Dunbar <span dir="ltr"><<a href="mailto:scott@xigole.com" target="_blank">scott@xigole.com</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"> I use something like:<br>
<br>
<pre>import org.keycloak.KeycloakPrincipal;
import org.keycloak.KeycloakSecurityContext;
import org.keycloak.representations.IDToken;
...
@Resource
private SessionContext sessionContext;
...
@SuppressWarnings("unchecked")
KeycloakPrincipal<KeycloakSecurityContext> kcPrincipal = (KeycloakPrincipal<KeycloakSecurityContext>)(sessionContext.getCallerPrincipal());
IDToken idToken = kcPrincipal.getKeycloakSecurityContext().getIdToken();
log.debug( "email from token is \"" + idToken.getEmail() + "\"" );</pre>
<br>
Not sure if that's the recommended way but it works well.
<br>
<div>
<div> <br>
<br>
<div>On 07/10/2015 04:48 PM, Juan Diego wrote:<br>
</div>
</div>
</div>
<blockquote type="cite">
<div>
<div>
<div dir="ltr">
<div>
<div>
<div>
<div>Hi<br>
<br>
</div>
I want to be able to update the user
password and some preferences from my web
app, in order to update some of the user
info from my portal i can see in the rest
api that you need the user ID. <br>
</div>
I have a backend with java that should connect
to my keycloak server once it gets the token<br>
<br>
KeycloakSecurityContext securityContext =
(KeycloakSecurityContext) httpRequest<br>
.getAttribute(KeycloakSecurityContext.class.getName());<br>
<br>
AccessToken accessToken =
securityContext.getToken();<br>
<br>
</div>
I dont know how to get info from the accesToken,
or does the access token class already has
methods to do that. I know this is more of a
question of design. This part is not really
clear for me.<br>
<br>
</div>
Thanks<br>
<br>
</div>
<br>
<fieldset></fieldset>
<br>
</div>
</div>
<pre>_______________________________________________
keycloak-user mailing list
<a href="mailto:keycloak-user@lists.jboss.org" target="_blank">keycloak-user@lists.jboss.org</a>
<a href="https://lists.jboss.org/mailman/listinfo/keycloak-user" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-user</a></pre>
<span><font color="#888888"> </font></span></blockquote>
<span><font color="#888888"> <br>
<div>-- <br>
<span style="font-weight:bold">Scott Dunbar</span><br>
<span>Xigole Systems, Inc.</span><br>
<span>Enterprise consulting, development, and
hosting</span><br>
<span>303·667·6343</span><br>
</div>
</font></span></div>
<br>
_______________________________________________<br>
keycloak-user mailing list<br>
<a href="mailto:keycloak-user@lists.jboss.org" target="_blank">keycloak-user@lists.jboss.org</a><br>
<a href="https://lists.jboss.org/mailman/listinfo/keycloak-user" rel="noreferrer" target="_blank">https://lists.jboss.org/mailman/listinfo/keycloak-user</a><br>
</blockquote>
</div>
<br>
</div>
</blockquote>
<br>
<div>-- <br>
<span style="font-weight:bold">Scott Dunbar</span><br>
<span>Xigole Systems, Inc.</span><br>
<span>Enterprise consulting, development, and hosting</span><br>
<span>303·667·6343</span><br>
</div>
</div></div></div>
</blockquote></div><br></div>