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.saml.authentication;
35
36 import fr.paris.lutece.plugins.mylutece.authentication.PortalAuthentication;
37 import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.config.ConfigProperties;
38 import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.config.Constants;
39 import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.engine.SAMLTokenHandler;
40 import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.CertificateValidationException;
41 import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.InvalidAttributeException;
42 import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.SAMLCheckerException;
43 import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.SAMLParsingException;
44 import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.SAMLTokenExtractorException;
45 import fr.paris.lutece.plugins.mylutece.modules.saml.authentication.exceptions.SignatureValidationException;
46 import fr.paris.lutece.plugins.mylutece.modules.saml.service.SAMLPlugin;
47 import fr.paris.lutece.portal.service.security.LoginRedirectException;
48 import fr.paris.lutece.portal.service.security.LuteceUser;
49 import fr.paris.lutece.portal.service.util.AppLogService;
50
51 import java.util.ArrayList;
52 import java.util.Collection;
53 import java.util.Iterator;
54 import java.util.Map;
55 import java.util.Map.Entry;
56
57 import javax.security.auth.login.LoginException;
58
59 import javax.servlet.http.HttpServletRequest;
60
61
62 public class SAMLAuthentication extends PortalAuthentication
63 {
64
65
66
67
68
69 public LuteceUser getAnonymousUser( )
70 {
71 return new SAMLUser( LuteceUser.ANONYMOUS_USERNAME, this );
72 }
73
74
75
76
77
78 public String getAuthServiceName( )
79 {
80 return this.getClass( ).getName( );
81 }
82
83
84
85
86
87
88 public String getAuthType( HttpServletRequest request )
89 {
90 return HttpServletRequest.BASIC_AUTH;
91 }
92
93
94
95
96
97
98
99
100 public boolean isUserInRole( LuteceUser user, HttpServletRequest request, String strRole )
101 {
102 return true;
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117 public LuteceUser login( String strUserName, String strUserPassword, HttpServletRequest request )
118 throws LoginException, LoginRedirectException
119 {
120 SAMLTokenHandler tokenHandler = new SAMLTokenHandler( );
121 SAMLUser user = null;
122
123 try
124 {
125
126 tokenHandler.checkSAMLResponse( request );
127
128
129 user = createSAMLUser( tokenHandler );
130 }
131 catch ( SignatureValidationException e )
132 {
133 AppLogService.error( e.getMessage( ), e );
134 throw new LoginException( );
135 }
136 catch ( CertificateValidationException e )
137 {
138 AppLogService.error( e.getMessage( ), e );
139 throw new LoginException( );
140 }
141 catch ( InvalidAttributeException e )
142 {
143 AppLogService.error( e.getMessage( ), e );
144 throw new LoginException( );
145 }
146 catch ( SAMLTokenExtractorException e )
147 {
148 AppLogService.error( e.getMessage( ), e );
149 throw new LoginException( );
150 }
151 catch ( SAMLParsingException e )
152 {
153 AppLogService.error( e.getMessage( ), e );
154 throw new LoginException( );
155 }
156 catch ( SAMLCheckerException e )
157 {
158 AppLogService.error( e.getMessage( ), e );
159 throw new LoginException( );
160 }
161
162 return user;
163 }
164
165
166
167
168
169 public void logout( LuteceUser user )
170 {
171 }
172
173 private SAMLUser createSAMLUser( SAMLTokenHandler tokenHandler )
174 throws SAMLParsingException
175 {
176
177 SAMLUser user = new SAMLUser( tokenHandler.getLuteceUserName( ), this );
178
179
180 Map<String, String> userInfos = tokenHandler.getLuteceUserProperties( );
181 Iterator<Entry<String, String>> it = userInfos.entrySet( ).iterator( );
182
183 while ( it.hasNext( ) )
184 {
185 Map.Entry<String, String> pairs = (Map.Entry<String, String>) it.next( );
186 user.setUserInfo( pairs.getKey( ), pairs.getValue( ) );
187 }
188
189
190 Collection<String> groups = tokenHandler.getLuteceUserGroups( );
191 user.setGroups( groups );
192
193
194 Collection<String> roles = new ArrayList<String>( );
195 roles.add( ConfigProperties.getInstance( ).getProperty( Constants.LUTECE_USER_ROLE_PROP ) );
196 user.setRoles( roles );
197
198 AppLogService.info( "Cr�ation LuteceUser : Nom=" + user.getName( ) );
199
200 return user;
201 }
202
203 public String[] getRolesByUser( LuteceUser user )
204 {
205 return null;
206 }
207
208
209
210
211
212 public String getName()
213 {
214 return SAMLPlugin.PLUGIN_NAME;
215 }
216
217
218
219
220
221 public String getPluginName()
222 {
223 return SAMLPlugin.PLUGIN_NAME;
224 }
225 }