1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.mylutece.modules.casexternal.authentication;
35
36
37 import fr.paris.lutece.plugins.mylutece.authentication.ExternalAuthentication;
38 import fr.paris.lutece.plugins.mylutece.modules.casexternal.service.CASExternalPlugin;
39 import fr.paris.lutece.portal.service.security.LoginRedirectException;
40
41 import fr.paris.lutece.portal.service.security.LuteceUser;
42 import fr.paris.lutece.portal.service.util.AppLogService;
43 import fr.paris.lutece.portal.service.util.AppPropertiesService;
44 import org.jasig.cas.client.authentication.AttributePrincipal;
45
46 import java.util.Iterator;
47 import java.util.Map;
48
49 import javax.security.auth.login.LoginException;
50
51 import javax.servlet.http.HttpServletRequest;
52
53
54
55
56
57
58
59 public class CASExternalAuthentication extends ExternalAuthentication
60 {
61
62
63 private static final String PROPERTY_AUTH_SERVICE_NAME = AppPropertiesService.getProperty( "mylutece-casexternal.service.name" );
64 private static final String PROPERTY_DEFAULT_ROLE_NAME = AppPropertiesService.getProperty( "mylutece-casexternal.role.name" );
65 private static final String PROPERTY_USER_DIRECTION = "mylutece-casexternal.user.direction";
66 private static final String PROPERTY_USER_ROLE = "mylutece-casexternal.user.role";
67
68
69
70
71 public CASExternalAuthentication( )
72 {
73 super( );
74 }
75
76
77
78
79
80
81 public String getAuthServiceName( )
82 {
83 return PROPERTY_AUTH_SERVICE_NAME;
84 }
85
86
87
88
89
90
91
92
93 public String getAuthType( HttpServletRequest request )
94 {
95 return HttpServletRequest.BASIC_AUTH;
96 }
97
98
99
100
101
102
103
104 public void logout( LuteceUser user )
105 {
106 }
107
108
109
110
111
112 public String[] getRolesByUser( LuteceUser user )
113 {
114 return null;
115 }
116
117
118
119
120
121
122 public LuteceUser getAnonymousUser( )
123 {
124 return new CASExternalUser( LuteceUser.ANONYMOUS_USERNAME, this );
125 }
126
127
128
129
130
131 public LuteceUser getHttpAuthenticatedUser(HttpServletRequest request) {
132
133 String strCASExternalUserLogin = request.getRemoteUser();
134 AppLogService.debug("You are succesfully logged in as user " + request.getRemoteUser());
135 CASExternalUser user = null;
136 String strUserDir;
137 String strUserRole;
138
139 if ( strCASExternalUserLogin != null )
140 {
141 user = new CASExternalUser( strCASExternalUserLogin, this );
142 }
143
144 AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();
145 Map attributes = principal.getAttributes();
146
147 if (attributes.size() > 0) {
148
149 AppLogService.debug("You have " + attributes.size() + " attributes : ");
150 Iterator keyIterator = attributes.keySet().iterator();
151
152 while (keyIterator.hasNext()) {
153
154 String strKey = keyIterator.next().toString();
155 String strValue = attributes.get(strKey).toString();
156 user.setUserInfo(strKey, strValue);
157 AppLogService.debug("key : '" + strKey + "' / value : '" + strValue + "'");
158 }
159
160
161
162 strUserDir = AppPropertiesService.getProperty( PROPERTY_USER_DIRECTION );
163 strUserRole = AppPropertiesService.getProperty( PROPERTY_USER_ROLE );
164
165 user.setUserInfo("direction", strUserDir);
166 AppLogService.debug("direction : '" + strUserDir + "'");
167 user.setUserInfo("role", strUserRole);
168 AppLogService.debug("role : '" + strUserRole + "'");
169
170 }
171 else
172 {
173 AppLogService.debug("You have no attributes set");
174 }
175
176 return user;
177 }
178
179
180
181
182
183 public LuteceUser login(String string, String string1, HttpServletRequest hsr) throws LoginException, LoginRedirectException {
184 throw new UnsupportedOperationException("Not supported yet.");
185 }
186
187
188
189
190
191 public boolean isUserInRole(LuteceUser lu, HttpServletRequest hsr, String string) {
192 throw new UnsupportedOperationException("Not supported yet.");
193 }
194
195
196
197
198
199 public String getName()
200 {
201 return CASExternalPlugin.PLUGIN_NAME;
202 }
203
204
205
206
207
208 public String getPluginName()
209 {
210 return CASExternalPlugin.PLUGIN_NAME;
211 }
212 }