1 |
59219
|
k.triantaf
|
package eu.dnetlib.openaire.usermanagement.utils;
|
2 |
|
|
|
3 |
59242
|
k.triantaf
|
import com.google.gson.JsonArray;
|
4 |
59219
|
k.triantaf
|
import com.google.gson.JsonObject;
|
5 |
59502
|
k.triantaf
|
import eu.dnetlib.openaire.user.pojos.RoleVerification;
|
6 |
59219
|
k.triantaf
|
import eu.dnetlib.openaire.user.utils.ManagerVerificationActions;
|
7 |
59274
|
k.triantaf
|
import org.apache.log4j.Logger;
|
8 |
|
|
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
|
9 |
59219
|
k.triantaf
|
import org.springframework.beans.factory.annotation.Autowired;
|
10 |
59274
|
k.triantaf
|
import org.springframework.security.core.context.SecurityContextHolder;
|
11 |
59219
|
k.triantaf
|
import org.springframework.stereotype.Component;
|
12 |
|
|
|
13 |
|
|
import java.sql.Timestamp;
|
14 |
59502
|
k.triantaf
|
import java.util.Date;
|
15 |
|
|
import java.util.List;
|
16 |
|
|
import java.util.Random;
|
17 |
59219
|
k.triantaf
|
|
18 |
|
|
|
19 |
59274
|
k.triantaf
|
@Component("VerificationUtils")
|
20 |
59219
|
k.triantaf
|
public class VerificationUtils {
|
21 |
|
|
|
22 |
|
|
private final Random random = new Random();
|
23 |
59274
|
k.triantaf
|
private static final Logger logger = Logger.getLogger(VerificationUtils.class);
|
24 |
59219
|
k.triantaf
|
|
25 |
|
|
@Autowired
|
26 |
|
|
private ManagerVerificationActions actions;
|
27 |
|
|
|
28 |
59502
|
k.triantaf
|
public JsonObject createManagerInvitation(String email, String type, String entity) {
|
29 |
|
|
RoleVerification roleVerification = actions.getManagerVerification(email, type, entity);
|
30 |
|
|
if(roleVerification == null) {
|
31 |
|
|
String id;
|
32 |
|
|
do {
|
33 |
|
|
id = createId();
|
34 |
|
|
} while (exists(id));
|
35 |
|
|
roleVerification = actions.addManagerVerification(id, email, type, entity, createVerificationCode(), new Timestamp(new Date().getTime()));
|
36 |
|
|
}
|
37 |
|
|
JsonObject invitation = new JsonObject();
|
38 |
|
|
invitation.addProperty("link", roleVerification.getId());
|
39 |
|
|
invitation.addProperty("code", roleVerification.getVerificationCode());
|
40 |
|
|
return invitation;
|
41 |
|
|
}
|
42 |
|
|
|
43 |
|
|
public JsonObject createMemberInvitation(String email, String type, String entity) {
|
44 |
59219
|
k.triantaf
|
String id;
|
45 |
|
|
do {
|
46 |
|
|
id = createId();
|
47 |
59274
|
k.triantaf
|
} while (exists(id));
|
48 |
59502
|
k.triantaf
|
RoleVerification roleVerification = actions.getMemberVerification(email, type, entity);
|
49 |
|
|
if(roleVerification == null) {
|
50 |
|
|
roleVerification = actions.addMemberVerification(id, email, type, entity, createVerificationCode(), new Timestamp(new Date().getTime()));
|
51 |
|
|
} JsonObject invitation = new JsonObject();
|
52 |
|
|
invitation.addProperty("link", roleVerification.getId());
|
53 |
|
|
invitation.addProperty("code", roleVerification.getVerificationCode());
|
54 |
59219
|
k.triantaf
|
return invitation;
|
55 |
|
|
}
|
56 |
|
|
|
57 |
59502
|
k.triantaf
|
public void deleteManagerVerifications(String email, String type, String entity) {
|
58 |
|
|
RoleVerification roleVerification = actions.getManagerVerification(email, type, entity);
|
59 |
|
|
if (roleVerification != null) {
|
60 |
|
|
deleteVerification(roleVerification.getId());
|
61 |
59242
|
k.triantaf
|
}
|
62 |
|
|
}
|
63 |
|
|
|
64 |
59502
|
k.triantaf
|
public void deleteMemberVerifications(String email, String type, String entity) {
|
65 |
|
|
RoleVerification roleVerification = actions.getMemberVerification(email, type, entity);
|
66 |
|
|
if (roleVerification != null) {
|
67 |
|
|
deleteVerification(roleVerification.getId());
|
68 |
59242
|
k.triantaf
|
}
|
69 |
|
|
}
|
70 |
|
|
|
71 |
59219
|
k.triantaf
|
public void deleteVerification(String id) {
|
72 |
59502
|
k.triantaf
|
actions.delete(id);
|
73 |
59219
|
k.triantaf
|
}
|
74 |
|
|
|
75 |
59502
|
k.triantaf
|
public JsonArray getInvitedManagers(String type, String id) {
|
76 |
|
|
List<String> emails = actions.getInvitedManagers(type, id);
|
77 |
59242
|
k.triantaf
|
JsonArray users = new JsonArray();
|
78 |
|
|
emails.forEach(users::add);
|
79 |
|
|
return users;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
59502
|
k.triantaf
|
public JsonArray getInvitedMembers(String type, String id) {
|
83 |
|
|
List<String> emails = actions.getInviteMembers(type, id);
|
84 |
|
|
JsonArray users = new JsonArray();
|
85 |
|
|
emails.forEach(users::add);
|
86 |
|
|
return users;
|
87 |
59219
|
k.triantaf
|
}
|
88 |
|
|
|
89 |
59502
|
k.triantaf
|
public RoleVerification getVerification(String id) {
|
90 |
|
|
return actions.get(id);
|
91 |
|
|
}
|
92 |
|
|
|
93 |
59219
|
k.triantaf
|
public boolean exists(String id) {
|
94 |
59502
|
k.triantaf
|
return actions.exists(id);
|
95 |
59219
|
k.triantaf
|
}
|
96 |
|
|
|
97 |
59274
|
k.triantaf
|
public boolean ownedVerification(String id) {
|
98 |
|
|
try {
|
99 |
59502
|
k.triantaf
|
RoleVerification managerVerification = getVerification(id);
|
100 |
59274
|
k.triantaf
|
if (managerVerification != null) {
|
101 |
|
|
OIDCAuthenticationToken authentication = (OIDCAuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
|
102 |
|
|
String email = authentication.getUserInfo().getEmail().toLowerCase();
|
103 |
|
|
return managerVerification.getEmail().toLowerCase().equals(email);
|
104 |
|
|
}
|
105 |
|
|
} catch (Exception e) {
|
106 |
|
|
logger.error("Get User info: An error occurred ", e);
|
107 |
|
|
return false;
|
108 |
|
|
}
|
109 |
|
|
return false;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
59219
|
k.triantaf
|
private String createId() {
|
113 |
59274
|
k.triantaf
|
return random.ints(48, 123)
|
114 |
59219
|
k.triantaf
|
.filter(i -> (i <= 57 || i >= 65) && (i <= 90 || i >= 97))
|
115 |
|
|
.limit(16)
|
116 |
|
|
.collect(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
|
117 |
|
|
.toString();
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
private String createVerificationCode() {
|
121 |
|
|
StringBuilder code = new StringBuilder();
|
122 |
|
|
for (int i = 0; i < 6; i++) {
|
123 |
|
|
code.append(random.nextInt(9));
|
124 |
|
|
}
|
125 |
|
|
return code.toString();
|
126 |
|
|
}
|
127 |
|
|
}
|