1 package fr.ifremer.quadrige3.core.security.remote;
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 fr.ifremer.quadrige3.core.config.QuadrigeCoreConfiguration;
27 import fr.ifremer.quadrige3.core.security.AuthenticationInfo;
28 import fr.ifremer.quadrige3.core.service.administration.user.UserService;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.http.HttpStatus;
32 import org.apache.http.auth.AuthScope;
33 import org.apache.http.auth.UsernamePasswordCredentials;
34 import org.apache.http.client.CredentialsProvider;
35 import org.apache.http.client.config.RequestConfig;
36 import org.apache.http.client.methods.CloseableHttpResponse;
37 import org.apache.http.client.methods.HttpGet;
38 import org.apache.http.impl.client.BasicCredentialsProvider;
39 import org.apache.http.impl.client.CloseableHttpClient;
40 import org.apache.http.impl.client.HttpClients;
41 import org.apache.http.util.EntityUtils;
42 import org.nuiton.i18n.I18n;
43 import org.springframework.stereotype.Service;
44
45 import javax.annotation.Resource;
46 import java.io.IOException;
47 import java.net.URISyntaxException;
48 import java.net.URL;
49
50 import static org.nuiton.i18n.I18n.t;
51
52
53
54
55
56 @Service("authenticationRemoteService")
57 public class AuthenticationRemoteServiceImpl implements AuthenticationRemoteService {
58
59 private static final Log log = LogFactory.getLog(AuthenticationRemoteServiceImpl.class);
60
61 @Resource(name = "userService")
62 protected UserService userService;
63
64 @Resource
65 protected QuadrigeCoreConfiguration config;
66
67
68 @Override
69 public boolean canAuthenticate(AuthenticationInfo authenticationInfo) throws AuthenticationRemoteException {
70 boolean authenticated = false;
71 Boolean isExtranet = userService.isLoginExtranet(authenticationInfo.getLogin());
72
73 try {
74
75 if (isExtranet != null) {
76 authenticated = authenticate(isExtranet, authenticationInfo);
77 }
78
79
80 else {
81
82 authenticated = authenticate(true, authenticationInfo);
83
84
85 if (!authenticated) {
86 authenticated = authenticate(false, authenticationInfo);
87 }
88 }
89
90 } catch (IOException e) {
91 log.error(I18n.t("quadrige3.error.authenticate.failed", e.getMessage()), e);
92 } catch (URISyntaxException e) {
93 throw new AuthenticationRemoteException(t("quadrige3.error.authenticate.failed", e, e.getMessage()));
94 }
95 return authenticated;
96 }
97
98
99
100 private boolean authenticate(boolean isExtranet, AuthenticationInfo authenticationInfo) throws IOException, URISyntaxException {
101 URL url = isExtranet ? config.getAuthenticationExtranetSiteUrl() : config.getAuthenticationIntranetSiteUrl();
102 Integer timeOut = isExtranet ? config.getAuthenticationExtranetSiteTimeout() : config.getAuthenticationIntranetSiteTimeout();
103 return authenticate(url, timeOut, authenticationInfo);
104 }
105
106 private boolean authenticate(URL url, Integer timeOut, AuthenticationInfo authenticationInfo) throws IOException, URISyntaxException {
107 boolean success;
108
109 if (log.isDebugEnabled()) {
110 log.debug(String.format("try to authenticate on [%s]", url));
111 }
112
113
114 CredentialsProvider credentialProvider = new BasicCredentialsProvider();
115 credentialProvider.setCredentials(new AuthScope(url.getHost(), url.getPort()),
116 new UsernamePasswordCredentials(authenticationInfo.getLogin(), authenticationInfo.getPassword()));
117
118
119 RequestConfig config = RequestConfig.custom().setSocketTimeout(timeOut).setConnectTimeout(timeOut).build();
120
121
122 try (CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(config).setDefaultCredentialsProvider(credentialProvider).build()) {
123 HttpGet httpget = new HttpGet(url.toURI());
124 if (log.isDebugEnabled()) {
125 log.debug("Executing request : " + httpget.getRequestLine());
126 }
127
128
129 try (CloseableHttpResponse response = httpclient.execute(httpget)) {
130 if (log.isDebugEnabled()) {
131 log.debug("Received response : " + response.getStatusLine());
132 }
133 int statusCode = response.getStatusLine().getStatusCode();
134 success = statusCode == HttpStatus.SC_OK;
135 EntityUtils.consume(response.getEntity());
136 }
137 }
138
139 return success;
140 }
141 }