<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="https://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Custom principal is not propagated to ejb session context (resteasy3 + oauth)
</h3>
<span style="margin-bottom: 10px;">
    created by <a href="https://community.jboss.org/people/marcel.rovira">Marcel Rovira</a> in <i>EJB3</i> - <a href="https://community.jboss.org/message/826545#826545">View the full discussion</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><p>Hello,</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>I'm using resteasy 3.0.1 Final with oauth in JBoss 6.1 EAP and my custom principal class is not propagated to sessioncontext in an EJB3.<br/>Oauth is configured as BearerTokenAuthenticator</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>My login-module configuration in standalone.xml to use extended login module</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>&lt;login-module code="es.gc.epsilon.secure.api.shared.resources.MyDatabaseServerLoginModule" flag="required"&gt;<br/> &lt;module-option name="dsJndiName" value="java:jboss/datasources/EpsilonXADS"/&gt;<br/> &lt;module-option name="principalsQuery" value="select PASSWORD from EP_USER where name=?"/&gt;<br/> &lt;module-option name="rolesQuery" value="select ROLE_NAME, 'Roles' from EP_USER_ROLE where USER_NAME = ?"/&gt;<br/> &lt;module-option name="hashAlgorithm" value="MD5"/&gt;<br/> &lt;module-option name="hashEncoding" value="base64"/&gt;<br/> &lt;module-option name="unauthenticatedIdentity" value="guest"/&gt;<br/>&lt;/login-module&gt;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>My DatabaseServerLoginModule:</p><p><br/>public class MyDatabaseServerLoginModule extends DatabaseServerLoginModule {</p><p>&#160; @Override<br/>&#160; protected java.security.Principal createIdentity(String username) throws Exception {</p><p>&#160;&#160;&#160; System.out.println("createIdentity BEGIN");</p><p>&#160;&#160;&#160; MyCustomPrincipal p = null;<br/>&#160;&#160;&#160; if (principalClassName == null) {<br/>&#160;&#160;&#160;&#160;&#160; p = new MyCustomPrincipal(username);<br/>&#160;&#160;&#160; } else {<br/>&#160;&#160;&#160;&#160;&#160; p = (MyCustomPrincipal) super.createIdentity(username);<br/>&#160;&#160;&#160; }</p><p>&#160;&#160;&#160; return p;<br/>&#160; }<br/>...</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>My custom principal</p><p><br/>public class MyCustomPrincipal extends SimplePrincipal implements Serializable {&#160; </p><p>&#160; private static final long serialVersionUID = 1L;</p><p>&#160; private String tenant;</p><p>&#160; public MyCustomPrincipal(String name) {<br/>&#160;&#160;&#160; super(name);<br/>&#160;&#160;&#160; // TODO Auto-generated constructor stub<br/>&#160; }<br/>...<br/>&#160; <br/>My oauth server configuration:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>jboss-web.xml</strong><br/>&lt;jboss-web&gt;<br/>&#160;&#160;&#160; &lt;security-domain&gt;java:/jaas/jaasEpsilon&lt;/security-domain&gt;<br/>&#160;&#160;&#160; &lt;valve&gt;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;class-name&gt;org.jboss.resteasy.skeleton.key.as7.OAuthAuthenticationServerValve&lt;/class-name&gt;<br/>&#160;&#160;&#160; &lt;/valve&gt;<br/>&lt;/jboss-web&gt;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>My api rest configuration project:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>web.xml</strong></p><p> &lt;login-config&gt;<br/>&#160; &lt;auth-method&gt;BASIC&lt;/auth-method&gt;<br/>&#160; &lt;realm-name&gt;jaasEpsilon&lt;/realm-name&gt;<br/> &lt;/login-config&gt;</p><p>&#160; &lt;security-constraint&gt;<br/>&#160; &lt;web-resource-collection&gt;<br/>&#160;&#160; &lt;web-resource-name&gt;All resources&lt;/web-resource-name&gt;<br/>&#160;&#160; &lt;description&gt;Protects all resources&lt;/description&gt;<br/>&#160;&#160; &lt;url-pattern&gt;/api/secure/*&lt;/url-pattern&gt;<br/>&#160;&#160; &lt;http-method&gt;GET&lt;/http-method&gt;<br/>&#160;&#160; &lt;http-method&gt;POST&lt;/http-method&gt;<br/>&#160; &lt;/web-resource-collection&gt;<br/>&#160; &lt;auth-constraint&gt;<br/>&#160;&#160; &lt;role-name&gt;admin&lt;/role-name&gt;<br/>&#160;&#160; &lt;role-name&gt;employee&lt;/role-name&gt;<br/>&#160; &lt;/auth-constraint&gt;<br/> &lt;/security-constraint&gt;<br/> <br/>&#160;&#160;&#160; &lt;context-param&gt;<br/>&#160;&#160;&#160;&#160;&#160; &lt;param-name&gt;resteasy.role.based.security&lt;/param-name&gt;<br/>&#160;&#160;&#160;&#160;&#160; &lt;param-value&gt;true&lt;/param-value&gt;<br/>&#160;&#160; &lt;/context-param&gt;<br/>&#160;&#160; <br/><strong>jboss-deployment-structure</strong></p><p><br/>&lt;jboss-deployment-structure&gt;<br/>&#160;&#160;&#160; &lt;deployment&gt;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;dependencies&gt;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;module name="org.jboss.resteasy.resteasy-jaxrs" services="import"/&gt;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;module name="org.jboss.resteasy.resteasy-jackson-provider" services="import"/&gt;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;module name="org.jboss.resteasy.skeleton-key"/&gt;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;/dependencies&gt;<br/>&#160;&#160;&#160; &lt;/deployment&gt;<br/>&lt;/jboss-deployment-structure&gt;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p><strong>jboss-web.xml</strong><br/>&lt;jboss-web&gt;<br/>&#160;&#160;&#160; &lt;valve&gt;<br/>&#160;&#160;&#160;&#160;&#160;&#160;&#160; &lt;class-name&gt;org.jboss.resteasy.skeleton.key.as7.BearerTokenAuthenticatorValve&lt;/class-name&gt;<br/>&#160;&#160;&#160; &lt;/valve&gt;<br/>&lt;/jboss-web&gt;</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>From an EJB I extract principal info as</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>@Resource(name = "sessionContext")<br/>private SessionContext sctx;<br/>...&#160; <br/>Principal principal = sctx.getCallerPrincipal();<br/> <br/>if (!(principal instanceof MyCustomPrincipal)) {<br/>&#160; System.out.println("I expected a " + MyCustomPrincipal.class.getName() + " but got a "<br/>&#160;&#160;&#160; + principal.getClass().getName() + " instead !!!!!!"); </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>and the result is:<br/> <br/>I expected a es.gc.epsilon.secure.api.shared.resources.MyCustomPrincipal but got a org.jboss.resteasy.skeleton.key.SkeletonKeyPrincipal instead </p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Is this a bug, is there another way to retrieve the caller principal, is there any wrong configuration?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Thanks.</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Reply to this message by <a href="https://community.jboss.org/message/826545#826545">going to Community</a></p>
        <p style="margin: 0;">Start a new discussion in EJB3 at <a href="https://community.jboss.org/choose-container!input.jspa?contentType=1&containerType=14&container=2029">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>