1 package fr.ifremer.quadrige3.core.security;
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 import com.google.common.collect.Lists;
27 import fr.ifremer.quadrige3.core.exception.QuadrigeTechnicalException;
28 import fr.ifremer.quadrige3.core.service.ClientServiceLocator;
29 import org.apache.commons.collections4.CollectionUtils;
30 import org.apache.commons.lang3.StringUtils;
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
34 import org.springframework.security.core.Authentication;
35 import org.springframework.security.core.AuthenticationException;
36 import org.springframework.security.core.context.SecurityContextHolder;
37
38 import java.util.Collection;
39 import java.util.Collections;
40 import java.util.List;
41
42
43
44
45
46
47 public class SecurityContextHelper {
48
49 private static final Log LOG = LogFactory.getLog(SecurityContextHelper.class);
50
51
52
53
54
55
56 public static Authentication getAuthentication() {
57 return SecurityContextHolder.getContext().getAuthentication();
58 }
59
60
61
62
63 public static void clear() {
64 SecurityContextHolder.clearContext();
65 }
66
67
68
69
70
71
72 public static QuadrigeUserDetails getQuadrigeUser() {
73 Authentication authentication = getAuthentication();
74 if (authentication == null || authentication.getPrincipal() == null) {
75 return null;
76 }
77 Object principal = authentication.getPrincipal();
78 if (principal instanceof QuadrigeUserDetails) {
79 return (QuadrigeUserDetails) principal;
80 }
81 return null;
82 }
83
84 public static int getQuadrigeUserId() {
85 QuadrigeUserDetails quadrigeUser = getQuadrigeUser();
86 if (quadrigeUser == null) {
87 throw new QuadrigeTechnicalException("No Authenticated Quadrige User");
88 }
89 return quadrigeUser.getUserId();
90 }
91
92
93
94
95
96
97
98
99 public static boolean authenticate(String login, String password) {
100 if (StringUtils.isBlank(password)) {
101
102
103 password = StringUtils.EMPTY;
104 }
105 else {
106
107
108 password = Encryption.sha(password);
109 }
110
111 Authentication authentication = new UsernamePasswordAuthenticationToken(login, password);
112 try {
113 if (LOG.isDebugEnabled()) {
114 LOG.debug("try to authenticate '" + login + "' in local database");
115 }
116
117 authentication = ClientServiceLocator.instance().getAuthenticationManager().authenticate(authentication);
118
119 if (LOG.isDebugEnabled()) {
120 LOG.debug("database authentication successful");
121 }
122 } catch (AuthenticationException ae) {
123 authentication.setAuthenticated(false);
124 if (LOG.isDebugEnabled()) {
125 LOG.debug("database authentication failed : " + ae.getLocalizedMessage());
126 }
127 }
128 SecurityContextHolder.getContext().setAuthentication(authentication);
129 return authentication.isAuthenticated();
130 }
131
132
133
134
135
136
137
138 public static boolean hasAuthority(QuadrigeUserAuthority authority) {
139 return hasAuthority(getAuthentication(), authority);
140 }
141
142
143
144
145
146
147
148 public static boolean hasAuthority(List<QuadrigeUserAuthority> authorities) {
149 return hasAuthority(getAuthentication(), authorities);
150 }
151
152
153
154
155
156
157
158
159 @SuppressWarnings("unchecked")
160 public static boolean hasAuthority(Authentication authentication, Object authorities) {
161
162 if (authentication == null || CollectionUtils.isEmpty(authentication.getAuthorities()) || authorities == null) {
163 return false;
164 }
165
166 List<QuadrigeUserAuthority> authList = Lists.newArrayList();
167 if (authorities instanceof Collection) {
168 authList.addAll((Collection<? extends QuadrigeUserAuthority>) authorities);
169 }
170 else if (authorities instanceof QuadrigeUserAuthority) {
171 authList.add((QuadrigeUserAuthority) authorities);
172 }
173 else {
174 LOG.error("the 'authorities' object is not a QuadrigeUserAuthority and not a Collection<QuadrigeUserAuthority>");
175 return false;
176 }
177
178
179 authList.retainAll(authentication.getAuthorities());
180
181
182 return !authList.isEmpty();
183
184 }
185
186
187
188
189
190
191
192 public static boolean hasMinimumAuthority(QuadrigeUserAuthority authority) {
193
194 return hasMinimumAuthority(getAuthentication(), authority);
195 }
196
197
198
199
200
201
202
203
204
205 @SuppressWarnings("unchecked")
206 public static boolean hasMinimumAuthority(Authentication authentication, Object authorities) {
207
208 if (authentication == null || CollectionUtils.isEmpty(authentication.getAuthorities()) || authorities == null) {
209 return false;
210 }
211
212 List<QuadrigeUserAuthority> miniAuthList = Lists.newArrayList();
213 if (authorities instanceof Collection) {
214 miniAuthList.addAll((Collection<? extends QuadrigeUserAuthority>) authorities);
215 }
216 else if (authorities instanceof QuadrigeUserAuthority) {
217 miniAuthList.add((QuadrigeUserAuthority) authorities);
218 }
219 else {
220 LOG.error("the 'authorities' object is not a QuadrigeUserAuthority and not a Collection<QuadrigeUserAuthority>");
221 return false;
222 }
223
224
225 Collections.sort(miniAuthList);
226
227
228 QuadrigeUserAuthority minimumAuthority = miniAuthList.get(miniAuthList.size() - 1);
229
230 List<QuadrigeUserAuthority> userAuthorities = (List<QuadrigeUserAuthority>) authentication.getAuthorities();
231 for (QuadrigeUserAuthority userAuthority : userAuthorities) {
232 if (userAuthority.compareTo(minimumAuthority) >= 0) {
233 return true;
234 }
235 }
236
237 return false;
238 }
239
240 }