1
+ package org .javaee7 .jaspic .jaccpropagation .sam ;
2
+
3
+ import static javax .security .auth .message .AuthStatus .SUCCESS ;
4
+
5
+ import java .io .IOException ;
6
+ import java .security .Principal ;
7
+ import java .util .Map ;
8
+
9
+ import javax .security .auth .Subject ;
10
+ import javax .security .auth .callback .Callback ;
11
+ import javax .security .auth .callback .CallbackHandler ;
12
+ import javax .security .auth .callback .UnsupportedCallbackException ;
13
+ import javax .security .auth .message .AuthException ;
14
+ import javax .security .auth .message .AuthStatus ;
15
+ import javax .security .auth .message .MessageInfo ;
16
+ import javax .security .auth .message .MessagePolicy ;
17
+ import javax .security .auth .message .callback .CallerPrincipalCallback ;
18
+ import javax .security .auth .message .callback .GroupPrincipalCallback ;
19
+ import javax .security .auth .message .module .ServerAuthModule ;
20
+ import javax .servlet .http .HttpServletRequest ;
21
+ import javax .servlet .http .HttpServletResponse ;
22
+
23
+ /**
24
+ * Very basic SAM that returns a single hardcoded user named "test" with role "architect" when the request parameter
25
+ * <code>doLogin</code> is present.
26
+ *
27
+ * @author Arjan Tijms
28
+ *
29
+ */
30
+ public class TestServerAuthModule implements ServerAuthModule {
31
+
32
+ private CallbackHandler handler ;
33
+ private Class <?>[] supportedMessageTypes = new Class [] { HttpServletRequest .class , HttpServletResponse .class };
34
+
35
+ @ Override
36
+ public void initialize (MessagePolicy requestPolicy , MessagePolicy responsePolicy , CallbackHandler handler ,
37
+ @ SuppressWarnings ("rawtypes" ) Map options ) throws AuthException {
38
+ this .handler = handler ;
39
+ }
40
+
41
+ @ Override
42
+ public AuthStatus validateRequest (MessageInfo messageInfo , Subject clientSubject , Subject serviceSubject )
43
+ throws AuthException {
44
+
45
+ HttpServletRequest request = (HttpServletRequest ) messageInfo .getRequestMessage ();
46
+
47
+ Callback [] callbacks ;
48
+
49
+ if (request .getParameter ("doLogin" ) != null ) {
50
+
51
+ callbacks = new Callback [] { new CallerPrincipalCallback (clientSubject , "test" ),
52
+ new GroupPrincipalCallback (clientSubject , new String [] { "architect" }) };
53
+ } else {
54
+
55
+ // The JASPIC protocol for "do nothing"
56
+ callbacks = new Callback [] { new CallerPrincipalCallback (clientSubject , (Principal ) null ) };
57
+ }
58
+
59
+ try {
60
+ handler .handle (callbacks );
61
+ } catch (IOException | UnsupportedCallbackException e ) {
62
+ throw (AuthException ) new AuthException ().initCause (e );
63
+ }
64
+
65
+ return SUCCESS ;
66
+ }
67
+
68
+ @ Override
69
+ public Class <?>[] getSupportedMessageTypes () {
70
+ return supportedMessageTypes ;
71
+ }
72
+
73
+ @ Override
74
+ public AuthStatus secureResponse (MessageInfo messageInfo , Subject serviceSubject ) throws AuthException {
75
+ return AuthStatus .SEND_SUCCESS ;
76
+ }
77
+
78
+ @ Override
79
+ public void cleanSubject (MessageInfo messageInfo , Subject subject ) throws AuthException {
80
+
81
+ }
82
+ }
0 commit comments