Project

General

Profile

1
package eu.dnetlib.openaire.usermanagement.utils;
2

    
3
import com.google.gson.JsonArray;
4
import com.google.gson.JsonObject;
5
import eu.dnetlib.openaire.user.pojos.RoleVerification;
6
import eu.dnetlib.openaire.user.utils.ManagerVerificationActions;
7
import org.apache.log4j.Logger;
8
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.security.core.context.SecurityContextHolder;
11
import org.springframework.stereotype.Component;
12

    
13
import java.sql.Timestamp;
14
import java.util.Date;
15
import java.util.List;
16
import java.util.Random;
17

    
18

    
19
@Component("VerificationUtils")
20
public class VerificationUtils {
21

    
22
    private final Random random = new Random();
23
    private static final Logger logger = Logger.getLogger(VerificationUtils.class);
24

    
25
    @Autowired
26
    private ManagerVerificationActions actions;
27

    
28
    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
        String id;
45
        do {
46
            id = createId();
47
        } while (exists(id));
48
        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
        return invitation;
55
    }
56

    
57
    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
        }
62
    }
63

    
64
    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
        }
69
    }
70

    
71
    public void deleteVerification(String id) {
72
        actions.delete(id);
73
    }
74

    
75
    public JsonArray getInvitedManagers(String type, String id) {
76
        List<String> emails = actions.getInvitedManagers(type, id);
77
        JsonArray users = new JsonArray();
78
        emails.forEach(users::add);
79
        return users;
80
    }
81

    
82
    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
    }
88

    
89
    public RoleVerification getVerification(String id) {
90
        return actions.get(id);
91
    }
92

    
93
    public boolean exists(String id) {
94
        return actions.exists(id);
95
    }
96

    
97
    public boolean ownedVerification(String id) {
98
        try {
99
            RoleVerification managerVerification = getVerification(id);
100
            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
    private String createId() {
113
        return random.ints(48, 123)
114
                .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
}
(9-9/9)