1+ package org .javaee7 .jaspic .registersession ;
2+
3+ import static org .junit .Assert .assertFalse ;
4+ import static org .junit .Assert .assertTrue ;
5+
6+ import java .io .IOException ;
7+
8+ import org .javaee7 .jaspic .common .ArquillianBase ;
9+ import org .jboss .arquillian .container .test .api .Deployment ;
10+ import org .jboss .arquillian .junit .Arquillian ;
11+ import org .jboss .shrinkwrap .api .Archive ;
12+ import org .junit .Test ;
13+ import org .junit .runner .RunWith ;
14+ import org .xml .sax .SAXException ;
15+
16+ /**
17+ * Variant of the {@link RegisterSessionCustomPrincipalTest}, where it's tested
18+ * if the authenticated identity restored by the runtime correctly propagates
19+ * to EJB.
20+ *
21+ * @author Arjan Tijms
22+ *
23+ */
24+ @ RunWith (Arquillian .class )
25+ public class RegisterSessionCustomPrincipalEJBPropagationTest extends ArquillianBase {
26+
27+ @ Deployment (testable = false )
28+ public static Archive <?> createDeployment () {
29+ return defaultArchive ();
30+ }
31+
32+ @ Test
33+ public void testRemembersSession () throws IOException , SAXException {
34+
35+ // -------------------- Request 1 ---------------------------
36+
37+ // Accessing protected page without login
38+ String response = getFromServerPath ("protected/servlet" );
39+
40+ // Not logged-in thus should not be accessible.
41+ assertFalse (response .contains ("This is a protected servlet" ));
42+
43+
44+ // -------------------- Request 2 ---------------------------
45+
46+ // We access the protected page again and now login
47+
48+ response = getFromServerPath ("protected/servlet?doLogin=true&customPrincipal=true" );
49+
50+ // Now has to be logged-in so page is accessible
51+ assertTrue (
52+ "Could not access protected page, but should be able to. " +
53+ "Did the container remember the previously set 'unauthenticated identity'?" ,
54+ response .contains ("This is a protected servlet" )
55+ );
56+
57+ // Check principal has right name and right type and roles are available
58+ checkAuthenticatedIdentity (response );
59+
60+
61+ // -------------------- Request 3 ---------------------------
62+
63+ // JASPIC is normally stateless, but for this test the SAM uses the register session feature so now
64+ // we should be logged-in when doing a call without explicitly logging in again.
65+
66+ response = getFromServerPath ("protected/servlet?continueSession=true" );
67+
68+ // Logged-in thus should be accessible.
69+ assertTrue (
70+ "Could not access protected page, but should be able to. " +
71+ "Did the container not remember the authenticated identity via 'javax.servlet.http.registerSession'?" ,
72+ response .contains ("This is a protected servlet" )
73+ );
74+
75+ // Both the user name and roles/groups have to be restored
76+
77+ // *** NOTE ***: The JASPIC 1.1 spec is NOT clear about remembering roles, but spec lead Ron Monzillo clarified that
78+ // this should indeed be the case. The next JASPIC revision of the spec will have to mention this explicitly.
79+ // Intuitively it should make sense though that the authenticated identity is fully restored and not partially,
80+ // but again the spec should make this clear to avoid ambiguity.
81+
82+ checkAuthenticatedIdentity (response );
83+
84+
85+ // -------------------- Request 4 ---------------------------
86+
87+ // The session should also be remembered and propagated to a public EJB
88+
89+ response = getFromServerPath ("public/servlet-public-ejb?continueSession=true" );
90+
91+ // Both the web (HttpServletRequest) and EJB (EJBContext) should see the same
92+ // user name.
93+ assertTrue (
94+ "User should have been authenticated in the web layer and given name \" test\" , " +
95+ " but does not appear to have this name" ,
96+ response .contains ("web username: test" )
97+ );
98+ assertTrue (
99+ "Web has user principal set, but EJB not." ,
100+ response .contains ("EJB username: test" )
101+ );
102+
103+
104+ // -------------------- Request 5 ---------------------------
105+
106+ // The session should also be remembered and propagated to a protected EJB
107+
108+ response = getFromServerPath ("public/servlet-protected-ejb?continueSession=true" );
109+
110+ // Both the web (HttpServletRequest) and EJB (EJBContext) should see the same
111+ // user name.
112+ assertTrue (
113+ "User should have been authenticated in the web layer and given name \" test\" , " +
114+ " but does not appear to have this name" ,
115+ response .contains ("web username: test" )
116+ );
117+ assertTrue (
118+ "Web has user principal set, but EJB not." ,
119+ response .contains ("EJB username: test" )
120+ );
121+
122+ // Both the web (HttpServletRequest) and EJB (EJBContext) should see that the
123+ // user has the role "architect".
124+ assertTrue (response .contains ("web user has role \" architect\" : true" ));
125+ assertTrue ("Web user principal has role \" architect\" , but one in EJB doesn't." ,
126+ response .contains ("EJB user has role \" architect\" : true" ));
127+
128+ }
129+
130+ private void checkAuthenticatedIdentity ( String response ) {
131+
132+ // Has to be logged-in with the right principal
133+ assertTrue (
134+ "Authenticated but username is not the expected one 'test'" ,
135+ response .contains ("web username: test" )
136+ );
137+ assertTrue (
138+ "Authentication succeeded and username is correct, but the expected role 'architect' is not present." ,
139+ response .contains ("web user has role \" architect\" : true" ));
140+
141+ assertTrue (
142+ "Authentication succeeded and username and roles are correct, but principal type is not the expected custom type." ,
143+ response .contains ("isCustomPrincipal: true" )
144+ );
145+ }
146+
147+
148+
149+ }
0 commit comments