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