View Javadoc
1   package fr.ifremer.quadrige3.synchro.server.application;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Synchro server
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.quadrige3.synchro.server.config.SynchroServerConfiguration;
27  import fr.ifremer.quadrige3.synchro.server.security.QuadrigeGrantedAuthority;
28  import fr.ifremer.quadrige3.synchro.server.security.SecurityContextHelper;
29  import fr.ifremer.quadrige3.synchro.server.service.ServiceLocator;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
33  import org.apache.wicket.authroles.authorization.strategies.role.Roles;
34  import org.apache.wicket.request.Request;
35  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
36  import org.springframework.security.core.Authentication;
37  import org.springframework.security.core.AuthenticationException;
38  import org.springframework.security.core.GrantedAuthority;
39  import org.springframework.security.core.context.SecurityContextHolder;
40  
41  import java.io.File;
42  
43  public class WebSession extends AuthenticatedWebSession {
44  
45      private static final long serialVersionUID = 1L;
46      private final Log log = LogFactory.getLog(getClass());
47  
48      private File importOutputFile;
49  
50      public WebSession(Request request) {
51          super(request);
52          bind();
53          this.importOutputFile = null;
54      }
55  
56      @Override
57      public Roles getRoles() {
58          Roles roles = new Roles();
59          getRolesIfSignedIn(roles);
60          return roles;
61      }
62  
63      private void getRolesIfSignedIn(Roles roles) {
64          if (isUserAuthenticated() || SecurityContextHelper.isAuthenticateNotAnonymous()) {
65              Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
66              for (GrantedAuthority authority : authentication.getAuthorities()) {
67                  roles.add(authority.getAuthority());
68              }
69          }
70      }
71  
72      public boolean isUserAuthenticated() {
73          return getUserId() > 0;
74      }
75  
76      public boolean isUserAdmin() {
77          return getRoles().hasRole(QuadrigeGrantedAuthority.ROLE_ADMIN.name());
78      }
79  
80      public final int getUserId() {
81          return SecurityContextHelper.getPrincipalUserId();
82      }
83  
84      public void setImportOutputFile(File importOutputFile) {
85          this.importOutputFile = importOutputFile;
86      }
87  
88      public File getImportOutputFile() {
89          return importOutputFile;
90      }
91  
92      /* -- Internal methods -- */
93  
94      protected SynchroServerConfiguration getConfiguration() {
95          return ((Application) getApplication()).getConfiguration();
96      }
97  
98      // Note : Méthode non appellée dans le cas d'une authentification CAS
99      @Override
100     public boolean authenticate(String username, String password) throws AuthenticationException {
101         ServiceLocator serviceLocator = ServiceLocator.instance();
102         boolean authenticated;
103         try {
104             Authentication authentication = serviceLocator.getAuthenticationManager().authenticate(new UsernamePasswordAuthenticationToken(username, password));
105             SecurityContextHolder.getContext().setAuthentication(authentication);
106             authenticated = authentication.isAuthenticated();
107         } catch (AuthenticationException e) {
108             String errorMessage = String.format("Authentication failed for user '%s' with error : %s", username, e.getLocalizedMessage());
109             if (log.isDebugEnabled()) {
110                 log.warn(errorMessage, e);
111             }
112             else {
113                 log.warn(errorMessage, e);
114             }
115             throw e;
116         }
117         return authenticated;
118     }
119 }