<html>
  <head>
    <meta content="text/html; charset=windows-1252"
      http-equiv="Content-Type">
  </head>
  <body bgcolor="#FFFFFF" text="#000000">
    Hi,<br>
    <br>
    i suppose you've to set cookies from response and keycloak
    automagically made the things for you.<br>
    <br>
    I resolved the problem to call impersonation API from our
    back-office panel applying the following modifications to
    keycloak.js file. <br>
    <br>
    After <i>processInit </i>function definition add the following
    lines of code:<br>
    <br>
    <pre>            /**
             * Append methods to keycloak object
             */
            
            adapter.impersonate = function(options) {
                var url = kc.createImpersonationUrl(options);                        
                var req = new XMLHttpRequest();
                req.open('POST', url, true);
                req.setRequestHeader('Accept', 'application/json');
                req.setRequestHeader('Authorization', 'bearer ' + kc.token);
                req.withCredentials = true;                                               
                
                var promise = createPromise();

                req.onreadystatechange = function () {
                    if (req.readyState == 4) {
                        if (req.status == 200) {
                            promise.setSuccess();
                        } else {
                            promise.setError();
                        }
                    }
                }

                req.send(null);

                return promise.promise;
            };
            
            kc.impersonate = function(options) {
                    return adapter.impersonate(options);
            };

            kc.createImpersonationUrl = function(user) {
                return getRealmUrl().replace('/auth/', '/auth/admin/') + '/users/' + user + '/impersonation';
            };
</pre>
    <br>
    Then define your impersonate method that calls keycloak.impersonate
    where you manage local session refresh, like that:<br>
    <br>
    <pre>        function impersonate(user) {</pre>
    <pre>            var deferred = $.Deferred();</pre>
    <pre>            keycloak.impersonate(user).success(function() {</pre>
    <pre>                console.log('user ' + user + ' impersonated');

                // clear local session user informations
</pre>
    <pre>                clearSession();
                
                // refresh logged user
</pre>
    <pre>                keycloak.login();</pre>
    <pre>                deferred.resolve();</pre>
    <pre>            });</pre>
    <pre>            return deferred.promise();</pre>
    <pre>        }</pre>
    <br>
    Hope this helps! ;)<br>
    <br>
    Daniele Bonetto<br>
    <br>
    <br>
    <br>
    Il 12/05/2016 00:29, Anshul Malpani ha scritto:<br>
    <blockquote
      cite="mid:4E607652-258B-4527-A07F-FB87114F703B@dnbcloud.com"
      type="cite">
      <meta http-equiv="Content-Type" content="text/html;
        charset=windows-1252">
      Hi,
      <div class=""><br class="">
      </div>
      <div class="">I am trying to use impersonate feature using my java
        client. When I call impersonate api using admin access grant. I
        get back the cookies. How can I get the access token for the
        impersonate user.</div>
      <div class=""><br class="">
      </div>
      <div class=""><br class="">
      </div>
      <div class=""><br class="">
      </div>
      <div class="">
        <div class="" style="margin: 0px; line-height: normal;
          font-family: Monaco;">HttpPost <span class="" style="color:
            rgb(126, 80, 79);">post</span> = <span class=""
            style="color: rgb(147, 26, 104);">new</span> HttpPost(</div>
        <div class="" style="margin: 0px; line-height: normal;"><font
            class="" face="Monaco">                   
            KeycloakUriBuilder.fromUri(</font><span class=""
            style="font-family: Monaco; color: rgb(126, 80, 79);">authServerUrl</span><font
            class="" face="Monaco">).path(</font><font class=""
            face="Monaco" color="#0326cc">“</font><span class=""
            style="color: rgb(57, 51, 255); font-family: Monaco;">/admin/realms/{realm}/users/{id}/impersonation</span><font
            class="" face="Monaco" color="#0326cc">"</font><font
            class="" face="Monaco">).build(</font><span class=""
            style="font-family: Monaco; color: rgb(126, 80, 79);">realm</span><font
            class="" face="Monaco">, </font><span class=""
            style="font-family: Monaco; color: rgb(126, 80, 79);">accountKeycloakId</span><font
            class="" face="Monaco">));</font></div>
      </div>
      <div class="" style="margin: 0px; line-height: normal;"><font
          class="" face="Monaco"><br class="">
        </font></div>
      <div class="" style="margin: 0px; line-height: normal;"><font
          class="" face="Monaco">This is returning me cookies. In
          next step I would like to get the access token of impersonate
          user.</font></div>
      <div class="" style="margin: 0px; line-height: normal;"><font
          class="" face="Monaco"><br class="">
        </font></div>
      <div class="" style="margin: 0px; line-height: normal;"><font
          class="" face="Monaco">Thanks</font></div>
      <div class="" style="margin: 0px; line-height: normal;"><font
          class="" face="Monaco">A</font></div>
      <br>
      <fieldset class="mimeAttachmentHeader"></fieldset>
      <br>
      <pre wrap="">_______________________________________________
keycloak-user mailing list
<a class="moz-txt-link-abbreviated" href="mailto:keycloak-user@lists.jboss.org">keycloak-user@lists.jboss.org</a>
<a class="moz-txt-link-freetext" href="https://lists.jboss.org/mailman/listinfo/keycloak-user">https://lists.jboss.org/mailman/listinfo/keycloak-user</a></pre>
    </blockquote>
    <br>
  </body>
</html>