1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package fr.cnes.doi.security;
20
21 import fr.cnes.doi.db.AbstractUserRoleDBHelper;
22 import java.util.Base64;
23
24 import org.apache.logging.log4j.LogManager;
25 import org.apache.logging.log4j.Logger;
26 import org.restlet.Request;
27 import org.restlet.Response;
28 import org.restlet.data.ChallengeResponse;
29 import org.restlet.data.ChallengeScheme;
30 import org.restlet.security.Verifier;
31
32 import fr.cnes.doi.logging.business.JsonMessage;
33 import fr.cnes.doi.plugin.PluginFactory;
34 import fr.cnes.doi.db.IAuthenticationDBHelper;
35 import java.nio.charset.Charset;
36
37
38
39
40
41
42 public class LoginBasedVerifier implements Verifier {
43
44
45
46
47 private static final Logger LOG = LogManager.getLogger(LoginBasedVerifier.class.getName());
48
49
50
51
52 private final IAuthenticationDBHelper authenticationService;
53
54
55
56
57 public LoginBasedVerifier() {
58 this.authenticationService = PluginFactory.getAuthenticationSystem();
59 }
60
61
62
63
64
65
66
67
68 @Override
69 public int verify(final Request request, final Response response) {
70 LOG.traceEntry(new JsonMessage(request));
71 final int result;
72 final ChallengeResponse challResponse = request.getChallengeResponse();
73
74 if (challResponse == null) {
75 result = Verifier.RESULT_MISSING;
76 } else if (challResponse.getScheme().equals(ChallengeScheme.HTTP_OAUTH_BEARER)) {
77 result = Verifier.RESULT_MISSING;
78 } else {
79 result = processAuthentication(request, challResponse);
80 }
81 return LOG.traceExit(result);
82 }
83
84
85
86
87
88
89
90
91 private int processAuthentication(final Request request, final ChallengeResponse challResponse) {
92 LOG.traceEntry(new JsonMessage(request));
93 final int result;
94 final String login = challResponse.getRawValue();
95 LOG.debug("User from challenge response : " + login);
96
97 if (login == null) {
98 return LOG.traceExit(Verifier.RESULT_MISSING);
99 }
100
101 final String decodedLogin = new String(Base64.getDecoder().decode(login), Charset.
102 defaultCharset());
103 final String[] userLogin = decodedLogin.split(":");
104
105 final AbstractUserRoleDBHelper manageUsers = PluginFactory.getUserManagement();
106 if (manageUsers.isUserExist(userLogin[0])) {
107 result = authenticationService.authenticateUser(userLogin[0], userLogin[1])
108 ? Verifier.RESULT_VALID : Verifier.RESULT_INVALID;
109 } else {
110 result = Verifier.RESULT_INVALID;
111 }
112 if (result == Verifier.RESULT_VALID) {
113 LOG.info("{} is authenticated, set it in get client info {}", userLogin[0], manageUsers.
114 getRealm().findUser(userLogin[0]));
115 request.getClientInfo().setUser(manageUsers.getRealm().findUser(userLogin[0]));
116 }
117 return LOG.traceExit(result);
118 }
119 }