1+ package org .javaee7 .jaspic .programmaticauthentication .sam ;
2+
3+ import static javax .security .auth .message .AuthStatus .SEND_SUCCESS ;
4+ import static javax .security .auth .message .AuthStatus .SUCCESS ;
5+
6+ import java .io .IOException ;
7+ import java .security .Principal ;
8+ import java .util .Map ;
9+
10+ import javax .security .auth .Subject ;
11+ import javax .security .auth .callback .Callback ;
12+ import javax .security .auth .callback .CallbackHandler ;
13+ import javax .security .auth .callback .UnsupportedCallbackException ;
14+ import javax .security .auth .message .AuthException ;
15+ import javax .security .auth .message .AuthStatus ;
16+ import javax .security .auth .message .MessageInfo ;
17+ import javax .security .auth .message .MessagePolicy ;
18+ import javax .security .auth .message .callback .CallerPrincipalCallback ;
19+ import javax .security .auth .message .callback .GroupPrincipalCallback ;
20+ import javax .security .auth .message .module .ServerAuthModule ;
21+ import javax .servlet .http .HttpServletRequest ;
22+ import javax .servlet .http .HttpServletResponse ;
23+
24+ /**
25+ * Very basic SAM that returns a single hardcoded user named "test" with role "architect" when the request *attribute*
26+ * <code>doLogin</code> is present.
27+ *
28+ * @author Arjan Tijms
29+ *
30+ */
31+ public class TestServerAuthModule implements ServerAuthModule {
32+
33+ private CallbackHandler handler ;
34+ private Class <?>[] supportedMessageTypes = new Class [] { HttpServletRequest .class , HttpServletResponse .class };
35+
36+ @ Override
37+ public void initialize (MessagePolicy requestPolicy , MessagePolicy responsePolicy , CallbackHandler handler ,
38+ @ SuppressWarnings ("rawtypes" ) Map options ) throws AuthException {
39+ this .handler = handler ;
40+ }
41+
42+ @ Override
43+ public AuthStatus validateRequest (MessageInfo messageInfo , Subject clientSubject , Subject serviceSubject )
44+ throws AuthException {
45+
46+ HttpServletRequest request = (HttpServletRequest ) messageInfo .getRequestMessage ();
47+
48+ Callback [] callbacks ;
49+
50+ if (request .getAttribute ("doLogin" ) != null ) { // notice "getAttribute" here, this is set by the Servlet
51+
52+ // For the test perform a login by directly "returning" the details of the authenticated user.
53+ // Normally credentials would be checked and the details fetched from some repository
54+
55+ callbacks = new Callback [] {
56+ // The name of the authenticated user
57+ new CallerPrincipalCallback (clientSubject , "test" ),
58+ // the roles of the authenticated user
59+ new GroupPrincipalCallback (clientSubject , new String [] { "architect" })
60+ };
61+ } else {
62+
63+ // The JASPIC protocol for "do nothing"
64+ callbacks = new Callback [] { new CallerPrincipalCallback (clientSubject , (Principal ) null ) };
65+ }
66+
67+ try {
68+
69+ // Communicate the details of the authenticated user to the container. In many
70+ // cases the handler will just store the details and the container will actually handle
71+ // the login after we return from this method.
72+ handler .handle (callbacks );
73+
74+ } catch (IOException | UnsupportedCallbackException e ) {
75+ throw (AuthException ) new AuthException ().initCause (e );
76+ }
77+
78+ return SUCCESS ;
79+ }
80+
81+ @ Override
82+ public Class <?>[] getSupportedMessageTypes () {
83+ return supportedMessageTypes ;
84+ }
85+
86+ @ Override
87+ public AuthStatus secureResponse (MessageInfo messageInfo , Subject serviceSubject ) throws AuthException {
88+ return SEND_SUCCESS ;
89+ }
90+
91+ @ Override
92+ public void cleanSubject (MessageInfo messageInfo , Subject subject ) throws AuthException {
93+
94+ }
95+ }
0 commit comments