Revision 62432
Added by Konstantinos Triantafyllou over 1 year ago
modules/uoa-user-management/tags/uoa-user-management-2.0.5/src/main/java/eu/dnetlib/openaire/user/queries/UserVerificationQueries.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.queries; |
|
2 |
|
|
3 |
/** |
|
4 |
* Created by sofia on 3/10/2017. |
|
5 |
*/ |
|
6 |
public interface UserVerificationQueries { |
|
7 |
|
|
8 |
String FETCH_ALL = "SELECT * FROM user_verification"; |
|
9 |
String FETCH_BY_ID = "SELECT * FROM user_verification WHERE id = ?"; |
|
10 |
String FETCH_BY_USERNAME = "SELECT * FROM user_verification WHERE username = ?"; |
|
11 |
String FETCH_BY_VERIFICATION_CODE = "SELECT * FROM user_verification WHERE verification_code = ?"; |
|
12 |
String FETCH_BY_DATE = "SELECT * FROM user_verification WHERE date = ?"; |
|
13 |
|
|
14 |
String COUNT_ALL = "SELECT COUNT(*) FROM user_verification"; |
|
15 |
|
|
16 |
String INSERT = "INSERT INTO user_verification (" + |
|
17 |
"username, " + |
|
18 |
"verification_code, " + |
|
19 |
"date " + |
|
20 |
") VALUES (?,?,?)"; |
|
21 |
|
|
22 |
String UPDATE = "UPDATE user_verification SET " + |
|
23 |
"username = ?, " + |
|
24 |
"verification_code = ?, " + |
|
25 |
"date = ? " + |
|
26 |
"WHERE id = ?"; |
|
27 |
|
|
28 |
String DELETE = "DELETE FROM user_verification WHERE username = ?"; |
|
29 |
|
|
30 |
} |
modules/uoa-user-management/tags/uoa-user-management-2.0.5/pom.xml | ||
---|---|---|
1 |
<?xml version="1.0"?> |
|
2 |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> |
|
3 |
<parent> |
|
4 |
<groupId>eu.dnetlib</groupId> |
|
5 |
<artifactId>dnet45-parent</artifactId> |
|
6 |
<version>1.0.0</version> |
|
7 |
</parent> |
|
8 |
<modelVersion>4.0.0</modelVersion> |
|
9 |
<groupId>eu.dnetlib</groupId> |
|
10 |
<artifactId>uoa-user-management</artifactId> |
|
11 |
<packaging>jar</packaging> |
|
12 |
<version>2.0.5</version> |
|
13 |
<scm> |
|
14 |
<developerConnection>scm:svn:https://svn.driver.research-infrastructures.eu/driver/dnet45/modules/uoa-user-management/tags/uoa-user-management-2.0.5</developerConnection> |
|
15 |
</scm> |
|
16 |
<dependencies> |
|
17 |
<dependency> |
|
18 |
<groupId>junit</groupId> |
|
19 |
<artifactId>junit</artifactId> |
|
20 |
<version>${junit.version}</version> |
|
21 |
<scope>test</scope> |
|
22 |
</dependency> |
|
23 |
|
|
24 |
<dependency> |
|
25 |
<groupId>org.springframework</groupId> |
|
26 |
<artifactId>spring-beans</artifactId> |
|
27 |
<version>${spring.version}</version> |
|
28 |
</dependency> |
|
29 |
<dependency> |
|
30 |
<groupId>org.springframework</groupId> |
|
31 |
<artifactId>spring-context</artifactId> |
|
32 |
<version>${spring.version}</version> |
|
33 |
</dependency> |
|
34 |
<dependency> |
|
35 |
<groupId>com.sun.jersey.contribs</groupId> |
|
36 |
<artifactId>jersey-spring</artifactId> |
|
37 |
<version>1.8</version> |
|
38 |
<exclusions> |
|
39 |
<exclusion> |
|
40 |
<groupId>org.springframework</groupId> |
|
41 |
<artifactId>spring</artifactId> |
|
42 |
</exclusion> |
|
43 |
<exclusion> |
|
44 |
<groupId>org.springframework</groupId> |
|
45 |
<artifactId>spring-core</artifactId> |
|
46 |
</exclusion> |
|
47 |
<exclusion> |
|
48 |
<groupId>org.springframework</groupId> |
|
49 |
<artifactId>spring-web</artifactId> |
|
50 |
</exclusion> |
|
51 |
<exclusion> |
|
52 |
<groupId>org.springframework</groupId> |
|
53 |
<artifactId>spring-beans</artifactId> |
|
54 |
</exclusion> |
|
55 |
<exclusion> |
|
56 |
<groupId>org.springframework</groupId> |
|
57 |
<artifactId>spring-context</artifactId> |
|
58 |
</exclusion> |
|
59 |
<exclusion> |
|
60 |
<groupId>org.springframework</groupId> |
|
61 |
<artifactId>spring-utils</artifactId> |
|
62 |
</exclusion> |
|
63 |
<exclusion> |
|
64 |
<groupId>org.springframework</groupId> |
|
65 |
<artifactId>spring-aop</artifactId> |
|
66 |
</exclusion> |
|
67 |
</exclusions> |
|
68 |
</dependency> |
|
69 |
<dependency> |
|
70 |
<groupId>com.google.code.gson</groupId> |
|
71 |
<artifactId>gson</artifactId> |
|
72 |
<version>2.6.2</version> |
|
73 |
</dependency> |
|
74 |
<dependency> |
|
75 |
<groupId>javax.servlet</groupId> |
|
76 |
<artifactId>javax.servlet-api</artifactId> |
|
77 |
<version>3.0.1</version> |
|
78 |
<scope>provided</scope> |
|
79 |
</dependency> |
|
80 |
<dependency> |
|
81 |
<groupId>org.postgresql</groupId> |
|
82 |
<artifactId>postgresql</artifactId> |
|
83 |
<version>9.4.1211.jre7</version> |
|
84 |
</dependency> |
|
85 |
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-dbcp2 --> |
|
86 |
<dependency> |
|
87 |
<groupId>org.apache.commons</groupId> |
|
88 |
<artifactId>commons-dbcp2</artifactId> |
|
89 |
<version>2.1.1</version> |
|
90 |
</dependency> |
|
91 |
<dependency> |
|
92 |
<groupId>commons-dbcp</groupId> |
|
93 |
<artifactId>commons-dbcp</artifactId> |
|
94 |
<version>20030825.184428</version> |
|
95 |
</dependency> |
|
96 |
<dependency> |
|
97 |
<groupId>com.unboundid</groupId> |
|
98 |
<artifactId>unboundid-ldapsdk</artifactId> |
|
99 |
<version>1.1.3</version> |
|
100 |
</dependency> |
|
101 |
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 --> |
|
102 |
<dependency> |
|
103 |
<groupId>apache</groupId> |
|
104 |
<artifactId>commons-pool</artifactId> |
|
105 |
<version>1.5.3</version> |
|
106 |
</dependency> |
|
107 |
<dependency> |
|
108 |
<groupId>io.jsonwebtoken</groupId> |
|
109 |
<artifactId>jjwt</artifactId> |
|
110 |
<version>0.6.0</version> |
|
111 |
</dependency> |
|
112 |
<dependency> |
|
113 |
<groupId>commons-validator</groupId> |
|
114 |
<artifactId>commons-validator</artifactId> |
|
115 |
<version>1.6</version> |
|
116 |
</dependency> |
|
117 |
<!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail --> |
|
118 |
<dependency> |
|
119 |
<groupId>com.sun.mail</groupId> |
|
120 |
<artifactId>javax.mail</artifactId> |
|
121 |
<version>1.5.1</version> |
|
122 |
</dependency> |
|
123 |
<!-- https://mvnrepository.com/artifact/org.glassfish/javax.json --> |
|
124 |
<dependency> |
|
125 |
<groupId>org.glassfish</groupId> |
|
126 |
<artifactId>javax.json</artifactId> |
|
127 |
<version>1.0.4</version> |
|
128 |
</dependency> |
|
129 |
</dependencies> |
|
130 |
</project> |
modules/uoa-user-management/tags/uoa-user-management-2.0.5/src/main/java/eu/dnetlib/openaire/user/actions/IUserActions.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.actions; |
|
2 |
|
|
3 |
/** |
|
4 |
* Created by sofia on 31/10/2016. |
|
5 |
*/ |
|
6 |
public interface IUserActions { |
|
7 |
|
|
8 |
public String addUser(String email, String password) throws Exception; |
|
9 |
|
|
10 |
public boolean activateUser(String activationId) throws Exception; |
|
11 |
|
|
12 |
public boolean isUserActivated(String email) throws Exception; |
|
13 |
|
|
14 |
public boolean userExists(String email) throws Exception; |
|
15 |
|
|
16 |
public boolean usernameExists(String username) throws Exception; |
|
17 |
|
|
18 |
public boolean correctCreds(String email, String password) throws Exception; |
|
19 |
|
|
20 |
public String prepareResetPassword(String email) throws Exception; |
|
21 |
|
|
22 |
public void resetPassword(String uuid, String password) throws Exception; |
|
23 |
|
|
24 |
public boolean isAdmin(String email) throws Exception; |
|
25 |
|
|
26 |
public void editUser(String email, String fname, String lname, String inst) |
|
27 |
throws Exception; |
|
28 |
|
|
29 |
public String addUser(String username, String email, String password, |
|
30 |
String firstName, String lastName) throws Exception; |
|
31 |
|
|
32 |
public String getEmailFromUsername(String username) throws Exception; |
|
33 |
|
|
34 |
//public UserProfile getUser(String userIdentifier) throws Exception; |
|
35 |
|
|
36 |
} |
modules/uoa-user-management/tags/uoa-user-management-2.0.5/src/main/java/eu/dnetlib/openaire/user/utils/EmailSender.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.utils; |
|
2 |
|
|
3 |
import org.apache.log4j.Logger; |
|
4 |
|
|
5 |
import javax.mail.*; |
|
6 |
import javax.mail.internet.InternetAddress; |
|
7 |
import javax.mail.internet.MimeMessage; |
|
8 |
import java.util.Properties; |
|
9 |
|
|
10 |
/** |
|
11 |
* Created by kiatrop on 9/10/2017. |
|
12 |
*/ |
|
13 |
public class EmailSender { |
|
14 |
|
|
15 |
private String username; |
|
16 |
private String password; |
|
17 |
private String host; |
|
18 |
private String port; |
|
19 |
private String from; |
|
20 |
private String auth; |
|
21 |
private String sslProtocols; |
|
22 |
|
|
23 |
Logger logger = Logger.getLogger(EmailSender.class); |
|
24 |
|
|
25 |
public void sendEmail(String recipient, String subject, String body) throws MessagingException { |
|
26 |
|
|
27 |
// Get system properties |
|
28 |
Properties properties = System.getProperties(); |
|
29 |
properties.put("mail.transport.protocol", "smtp"); |
|
30 |
properties.put("mail.smtp.host", host); |
|
31 |
properties.put("mail.smtp.port", port); |
|
32 |
properties.put("mail.smtp.auth", auth); |
|
33 |
properties.put("mail.smtp.starttls.enable", "true"); |
|
34 |
if(sslProtocols != null) { |
|
35 |
properties.put("mail.smtp.ssl.protocols", sslProtocols); |
|
36 |
} |
|
37 |
Session session = javax.mail.Session.getInstance(properties, |
|
38 |
new Authenticator() { |
|
39 |
protected PasswordAuthentication getPasswordAuthentication() { |
|
40 |
return new PasswordAuthentication(username, password); |
|
41 |
} |
|
42 |
}); |
|
43 |
|
|
44 |
// Create a default MimeMessage object. |
|
45 |
MimeMessage message = new MimeMessage(session); |
|
46 |
|
|
47 |
// Set From: header field of the header. |
|
48 |
message.setFrom(new InternetAddress(from)); |
|
49 |
|
|
50 |
// Set To: header field of the header. |
|
51 |
message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient)); |
|
52 |
|
|
53 |
// Set Subject: header field |
|
54 |
message.setSubject(subject, "UTF-8"); |
|
55 |
|
|
56 |
// For simple text setText() can be used instead of setContent() |
|
57 |
|
|
58 |
// Send the actual HTML message, as big as you like |
|
59 |
message.setContent(body, "text/html;charset=UTF-8"); |
|
60 |
|
|
61 |
// Send message |
|
62 |
Transport.send(message); |
|
63 |
logger.debug("Sent message successfully....\n"); |
|
64 |
|
|
65 |
} |
|
66 |
|
|
67 |
public String getUsername() { |
|
68 |
return username; |
|
69 |
} |
|
70 |
|
|
71 |
public void setUsername(String username) { |
|
72 |
this.username = username; |
|
73 |
} |
|
74 |
|
|
75 |
public String getPassword() { |
|
76 |
return password; |
|
77 |
} |
|
78 |
|
|
79 |
public void setPassword(String password) { |
|
80 |
this.password = password; |
|
81 |
} |
|
82 |
|
|
83 |
public String getHost() { |
|
84 |
return host; |
|
85 |
} |
|
86 |
|
|
87 |
public void setHost(String host) { |
|
88 |
this.host = host; |
|
89 |
} |
|
90 |
|
|
91 |
public String getPort() { |
|
92 |
return port; |
|
93 |
} |
|
94 |
|
|
95 |
public void setPort(String port) { |
|
96 |
this.port = port; |
|
97 |
} |
|
98 |
|
|
99 |
public String getFrom() { |
|
100 |
return from; |
|
101 |
} |
|
102 |
|
|
103 |
public void setFrom(String from) { |
|
104 |
this.from = from; |
|
105 |
} |
|
106 |
|
|
107 |
public String getAuth() { |
|
108 |
return auth; |
|
109 |
} |
|
110 |
|
|
111 |
public void setAuth(String auth) { |
|
112 |
this.auth = auth; |
|
113 |
} |
|
114 |
|
|
115 |
public String getSslProtocols() { |
|
116 |
return sslProtocols; |
|
117 |
} |
|
118 |
|
|
119 |
public void setSslProtocols(String sslProtocols) { |
|
120 |
this.sslProtocols = sslProtocols; |
|
121 |
} |
|
122 |
} |
modules/uoa-user-management/tags/uoa-user-management-2.0.5/create-table | ||
---|---|---|
1 |
CREATE TABLE role_verification ( |
|
2 |
id VARCHAR(255) PRIMARY KEY, |
|
3 |
email VARCHAR(255) NOT NULL, |
|
4 |
verification_code VARCHAR(6) NOT NULL, |
|
5 |
type VARCHAR(255) NOT NULL, |
|
6 |
entity VARCHAR(255) NOT NULL, |
|
7 |
verification_type VARCHAR(255) NOT NULL, |
|
8 |
date TIMESTAMP NOT NULL |
|
9 |
); |
|
10 |
|
|
11 |
GRANT ALL PRIVILEGES ON TABLE role_verification TO postgresi2; |
|
12 |
|
|
13 |
CREATE TABLE registered_service ( |
|
14 |
id SERIAL PRIMARY KEY, |
|
15 |
client_id VARCHAR(255) NOT NULL, |
|
16 |
owner VARCHAR(255) NOT NULL, |
|
17 |
name VARCHAR(255) NOT NULL, |
|
18 |
creation_date TIMESTAMP NOT NULL, |
|
19 |
registration_access_token TEXT NOT NULL, |
|
20 |
key_type VARCHAR(255) |
|
21 |
); |
|
22 |
|
|
23 |
GRANT ALL PRIVILEGES ON TABLE registered_service TO postgresi2; |
|
24 |
GRANT USAGE, SELECT ON SEQUENCE registered_service_id_seq TO postgresi2; |
|
25 |
|
|
26 |
|
modules/uoa-user-management/tags/uoa-user-management-2.0.5/src/main/java/eu/dnetlib/openaire/user/dao/SQLUserDAO.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.dao; |
|
2 |
|
|
3 |
import eu.dnetlib.openaire.user.pojos.migration.User; |
|
4 |
import eu.dnetlib.openaire.user.store.Statement; |
|
5 |
|
|
6 |
import java.sql.ResultSet; |
|
7 |
import java.sql.SQLException; |
|
8 |
import java.util.List; |
|
9 |
|
|
10 |
|
|
11 |
/** |
|
12 |
* Created by sofia on 24/6/2016. |
|
13 |
*/ |
|
14 |
public class SQLUserDAO implements UserDAO { |
|
15 |
@Override |
|
16 |
public User fetchById(String id) { |
|
17 |
return null; |
|
18 |
} |
|
19 |
|
|
20 |
@Override |
|
21 |
public User fetchByEmail(String mail) { |
|
22 |
return null; |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
public User fetchByUsername(String username) { |
|
27 |
return null; |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
public User fetchByName(String name) { |
|
32 |
return null; |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public User fetchBySurname(String surname) { |
|
37 |
return null; |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public List<User> fetchAll() { |
|
42 |
return null; |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
public int countAll() { |
|
47 |
return 0; |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public void insert(User u) { |
|
52 |
|
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public void delete(User u) { |
|
57 |
|
|
58 |
} |
|
59 |
|
|
60 |
@Override |
|
61 |
public User update(User u) { |
|
62 |
return null; |
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
public User fromResultSet(ResultSet set) throws SQLException { |
|
67 |
|
|
68 |
User user = new User(set.getString("username")); |
|
69 |
|
|
70 |
user.setPassword(set.getString("password")); |
|
71 |
user.setEmail(set.getString("email")); |
|
72 |
user.setName(set.getString("name")); |
|
73 |
user.setSurname(set.getString("surname")); |
|
74 |
|
|
75 |
return user; |
|
76 |
} |
|
77 |
|
|
78 |
@Override |
|
79 |
public int executeUpdate(String sql) { |
|
80 |
return 0; |
|
81 |
} |
|
82 |
|
|
83 |
@Override |
|
84 |
public int executeUpdate(String sql, Statement.Initializer init) { |
|
85 |
return 0; |
|
86 |
} |
|
87 |
|
|
88 |
@Override |
|
89 |
public User executeQuery(String sql) { |
|
90 |
return null; |
|
91 |
} |
|
92 |
|
|
93 |
@Override |
|
94 |
public User executeQuery(String sql, Statement.Initializer init) { |
|
95 |
return null; |
|
96 |
} |
|
97 |
|
|
98 |
@Override |
|
99 |
public long executeCount(String sql) { |
|
100 |
return 0; |
|
101 |
} |
|
102 |
|
|
103 |
@Override |
|
104 |
public long executeCount(String sql, Statement.Initializer init) { |
|
105 |
return 0; |
|
106 |
} |
|
107 |
} |
modules/uoa-user-management/tags/uoa-user-management-2.0.5/src/main/resources/eu/dnetlib/openaire/user/springContext-userManagementService.properties | ||
---|---|---|
1 |
openaire.users.db.driverClassName = org.postgresql.Driver |
|
2 |
openaire.users.db.url = jdbc:postgresql://mpagasas.di.uoa.gr:5433/MigrationUser |
|
3 |
openaire.users.db.username = postgres |
|
4 |
openaire.users.db.password = 1@3$56213980 |
|
5 |
|
|
6 |
services.userman.ldap.address = esperos.di.uoa.gr |
|
7 |
services.userman.ldap.username = cn=admin,dc=openaire,dc=eu |
|
8 |
services.userman.ldap.usersDN = ou=users,dc=openaire,dc=eu |
|
9 |
services.userman.ldap.zombiesDN = ou=zombies,dc=openaire,dc=eu |
|
10 |
|
|
11 |
services.userman.ldap.password = serenata |
|
12 |
services.userman.ldap.port = 389 |
|
13 |
|
|
14 |
#email sender properties |
|
15 |
mail.smtp.host = smtp.gmail.com |
|
16 |
mail.smtp.port = 587 |
|
17 |
mail.smtp.auth = true |
|
18 |
mail.users.username = openaire.test@gmail.com |
|
19 |
#mail.users.password = P@ssw0rD |
|
20 |
mail.smtp.from = openaire.test@gmail.com |
|
21 |
|
|
22 |
mail.users.password = ^($*@$)*!$ |
modules/uoa-user-management/tags/uoa-user-management-2.0.5/src/main/java/eu/dnetlib/openaire/user/utils/VerifyRecaptcha.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.utils; |
|
2 |
|
|
3 |
import org.apache.log4j.Logger; |
|
4 |
|
|
5 |
import javax.json.Json; |
|
6 |
import javax.json.JsonObject; |
|
7 |
import javax.json.JsonReader; |
|
8 |
import javax.net.ssl.HttpsURLConnection; |
|
9 |
import java.io.BufferedReader; |
|
10 |
import java.io.DataOutputStream; |
|
11 |
import java.io.InputStreamReader; |
|
12 |
import java.io.StringReader; |
|
13 |
import java.net.URL; |
|
14 |
|
|
15 |
public class VerifyRecaptcha { |
|
16 |
|
|
17 |
public static final String url = "https://www.google.com/recaptcha/api/siteverify"; |
|
18 |
|
|
19 |
//private final static String USER_AGENT = "Mozilla/5.0"; |
|
20 |
|
|
21 |
private static Logger logger = Logger.getLogger(VerifyRecaptcha.class); |
|
22 |
public static boolean verify(String gRecaptchaResponse, String secret) { |
|
23 |
|
|
24 |
logger.debug("Recaptch secret " + secret); |
|
25 |
logger.debug("response " + gRecaptchaResponse); |
|
26 |
|
|
27 |
if (gRecaptchaResponse == null || "".equals(gRecaptchaResponse)) { |
|
28 |
return false; |
|
29 |
} |
|
30 |
|
|
31 |
try{ |
|
32 |
URL obj = new URL(url); |
|
33 |
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection(); |
|
34 |
|
|
35 |
// add reuqest header |
|
36 |
con.setRequestMethod("POST"); |
|
37 |
//con.setRequestProperty("User-Agent", USER_AGENT); |
|
38 |
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); |
|
39 |
|
|
40 |
String postParams = "secret=" + secret + "&response=" |
|
41 |
+ gRecaptchaResponse; |
|
42 |
|
|
43 |
// Send post request |
|
44 |
con.setDoOutput(true); |
|
45 |
DataOutputStream wr = new DataOutputStream(con.getOutputStream()); |
|
46 |
wr.writeBytes(postParams); |
|
47 |
wr.flush(); |
|
48 |
wr.close(); |
|
49 |
|
|
50 |
int responseCode = con.getResponseCode(); |
|
51 |
logger.debug("\nSending 'POST' request to URL : " + url); |
|
52 |
logger.debug("Post parameters : " + postParams); |
|
53 |
logger.info("recaptcha response Code : " + responseCode); |
|
54 |
|
|
55 |
BufferedReader in = new BufferedReader(new InputStreamReader( |
|
56 |
con.getInputStream())); |
|
57 |
String inputLine; |
|
58 |
StringBuffer response = new StringBuffer(); |
|
59 |
|
|
60 |
while ((inputLine = in.readLine()) != null) { |
|
61 |
response.append(inputLine); |
|
62 |
} |
|
63 |
in.close(); |
|
64 |
|
|
65 |
// print result |
|
66 |
logger.debug(response.toString()); |
|
67 |
|
|
68 |
//parse JSON response and return 'success' value |
|
69 |
JsonReader jsonReader = Json.createReader(new StringReader(response.toString())); |
|
70 |
JsonObject jsonObject = jsonReader.readObject(); |
|
71 |
jsonReader.close(); |
|
72 |
|
|
73 |
return jsonObject.getBoolean("success"); |
|
74 |
|
|
75 |
} catch(Exception e){ |
|
76 |
logger.error("Error validating recaptcha"); |
|
77 |
return false; |
|
78 |
} |
|
79 |
} |
|
80 |
|
|
81 |
} |
modules/uoa-user-management/tags/uoa-user-management-2.0.5/deploy.info | ||
---|---|---|
1 |
{ |
|
2 |
"type_source": "SVN", |
|
3 |
"goal": "package -U source:jar", |
|
4 |
"url": "http://svn-public.driver.research-infrastructures.eu/driver/dnet45/modules/uoa-user-management/trunk", |
|
5 |
"deploy_repository": "dnet45-snapshots", |
|
6 |
"version": "4", |
|
7 |
"mail": "kiatrop@di.uoa.gr, argirok@di.uoa.gr, k.triantafyllou@di.uoa.gr", |
|
8 |
"deploy_repository_url": "https://maven.d4science.org/nexus/content/repositories/dnet45-snapshots", |
|
9 |
"name": "uoa-user-management" |
|
10 |
} |
|
11 |
|
modules/uoa-user-management/tags/uoa-user-management-2.0.5/src/main/java/eu/dnetlib/openaire/user/dao/LDAPUserDAO.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.dao; |
|
2 |
|
|
3 |
import eu.dnetlib.openaire.user.pojos.migration.User; |
|
4 |
import eu.dnetlib.openaire.user.store.LDAPConnector; |
|
5 |
import eu.dnetlib.openaire.user.store.Statement; |
|
6 |
import org.springframework.beans.factory.annotation.Autowired; |
|
7 |
|
|
8 |
import java.sql.ResultSet; |
|
9 |
import java.sql.SQLException; |
|
10 |
import java.util.List; |
|
11 |
|
|
12 |
/** |
|
13 |
* Created by sofia on 4/8/2016. |
|
14 |
*/ |
|
15 |
public class LDAPUserDAO implements UserDAO { |
|
16 |
|
|
17 |
@Autowired |
|
18 |
private LDAPConnector ldapConnector; |
|
19 |
|
|
20 |
@Override |
|
21 |
public List<User> fetchAll() { |
|
22 |
return null; |
|
23 |
} |
|
24 |
|
|
25 |
@Override |
|
26 |
public User fetchById(String id) { |
|
27 |
return null; |
|
28 |
} |
|
29 |
|
|
30 |
@Override |
|
31 |
public User fetchByEmail(String mail) { |
|
32 |
return null; |
|
33 |
} |
|
34 |
|
|
35 |
@Override |
|
36 |
public User fetchByUsername(String username) { |
|
37 |
return null; |
|
38 |
} |
|
39 |
|
|
40 |
@Override |
|
41 |
public User fetchByName(String name) { |
|
42 |
return null; |
|
43 |
} |
|
44 |
|
|
45 |
@Override |
|
46 |
public User fetchBySurname(String surname) { |
|
47 |
return null; |
|
48 |
} |
|
49 |
|
|
50 |
@Override |
|
51 |
public int countAll() { |
|
52 |
return 0; |
|
53 |
} |
|
54 |
|
|
55 |
@Override |
|
56 |
public void insert(User u) { |
|
57 |
|
|
58 |
} |
|
59 |
|
|
60 |
@Override |
|
61 |
public void delete(User u) { |
|
62 |
|
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
public User update(User u) { |
|
67 |
return null; |
|
68 |
} |
|
69 |
|
|
70 |
@Override |
|
71 |
public User fromResultSet(ResultSet set) throws SQLException { |
|
72 |
return null; |
|
73 |
} |
|
74 |
|
|
75 |
@Override |
|
76 |
public int executeUpdate(String sql) { |
|
77 |
return 0; |
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
public int executeUpdate(String sql, Statement.Initializer init) { |
|
82 |
return 0; |
|
83 |
} |
|
84 |
|
|
85 |
@Override |
|
86 |
public User executeQuery(String sql) { |
|
87 |
return null; |
|
88 |
} |
|
89 |
|
|
90 |
@Override |
|
91 |
public User executeQuery(String sql, Statement.Initializer init) { |
|
92 |
return null; |
|
93 |
} |
|
94 |
|
|
95 |
@Override |
|
96 |
public long executeCount(String sql) { |
|
97 |
return 0; |
|
98 |
} |
|
99 |
|
|
100 |
@Override |
|
101 |
public long executeCount(String sql, Statement.Initializer init) { |
|
102 |
return 0; |
|
103 |
} |
|
104 |
} |
modules/uoa-user-management/tags/uoa-user-management-2.0.5/src/main/java/eu/dnetlib/openaire/user/store/DataSourceConnector.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.store; |
|
2 |
|
|
3 |
import org.apache.commons.dbcp.BasicDataSource; |
|
4 |
import org.apache.log4j.Logger; |
|
5 |
import org.springframework.beans.factory.annotation.Value; |
|
6 |
import org.springframework.stereotype.Component; |
|
7 |
|
|
8 |
import javax.annotation.PostConstruct; |
|
9 |
import javax.sql.DataSource; |
|
10 |
import java.sql.Connection; |
|
11 |
import java.sql.DriverManager; |
|
12 |
import java.sql.SQLException; |
|
13 |
|
|
14 |
/** |
|
15 |
* Created by sofia on 31/10/2016. |
|
16 |
*/ |
|
17 |
@Component(value = "dataSourceConnector") |
|
18 |
public class DataSourceConnector { |
|
19 |
|
|
20 |
@Value("${openaire.users.db.username}") |
|
21 |
private String username; |
|
22 |
@Value("${openaire.users.db.password}") |
|
23 |
private String password; |
|
24 |
@Value("${openaire.users.db.url}") |
|
25 |
private String dbUrl; |
|
26 |
@Value("${openaire.users.db.driverClassName}") |
|
27 |
private String driver; |
|
28 |
|
|
29 |
private static boolean driverLoad = false; |
|
30 |
|
|
31 |
private DataSource datasource; |
|
32 |
|
|
33 |
private static final Logger logger = Logger.getLogger(DataSourceConnector.class); |
|
34 |
|
|
35 |
@PostConstruct |
|
36 |
public void init() { |
|
37 |
logger.info("Initializing data source"); |
|
38 |
|
|
39 |
try { |
|
40 |
Class.forName(driver); |
|
41 |
driverLoad = true; |
|
42 |
|
|
43 |
BasicDataSource bds = new BasicDataSource(); |
|
44 |
|
|
45 |
bds.setDriverClassName(driver); |
|
46 |
bds.setUrl(dbUrl); |
|
47 |
bds.setUsername(username); |
|
48 |
bds.setPassword(password); |
|
49 |
bds.setMaxIdle(10); |
|
50 |
bds.setMaxActive(100); |
|
51 |
bds.setMaxWait(10000); |
|
52 |
bds.setValidationQuery("select 1"); |
|
53 |
bds.setTestOnBorrow(true); |
|
54 |
bds.setTestWhileIdle(true); |
|
55 |
bds.setTimeBetweenEvictionRunsMillis(1200000); |
|
56 |
bds.setMinEvictableIdleTimeMillis(1800000); |
|
57 |
bds.setNumTestsPerEvictionRun(5); |
|
58 |
bds.setDefaultAutoCommit(true); |
|
59 |
|
|
60 |
this.datasource = bds; |
|
61 |
|
|
62 |
// Test Connection |
|
63 |
getConnection().close(); |
|
64 |
logger.info("Datasource " + datasource); |
|
65 |
logger.info(datasource.getConnection()); |
|
66 |
|
|
67 |
} catch (ClassNotFoundException | SQLException exc) { |
|
68 |
logger.error("Unable to connenct to the DB. ", exc); |
|
69 |
throw new RuntimeException(exc); |
|
70 |
} |
|
71 |
} |
|
72 |
|
|
73 |
public Connection getConnection() throws SQLException { |
|
74 |
logger.debug("dbUrl " + dbUrl); |
|
75 |
return DriverManager.getConnection(dbUrl, username, password); |
|
76 |
//return DriverManager.getConnection("jdbc:postgresql://localhost:5433/MigrationUser?user=postgres&password=mypassword"); |
|
77 |
} |
|
78 |
|
|
79 |
public String getUsername() { |
|
80 |
return username; |
|
81 |
} |
|
82 |
|
|
83 |
public void setUsername(String username) { |
|
84 |
this.username = username; |
|
85 |
} |
|
86 |
|
|
87 |
public String getPassword() { |
|
88 |
return password; |
|
89 |
} |
|
90 |
|
|
91 |
public void setPassword(String password) { |
|
92 |
this.password = password; |
|
93 |
} |
|
94 |
|
|
95 |
public String getDbUrl() { |
|
96 |
return dbUrl; |
|
97 |
} |
|
98 |
|
|
99 |
public void setDbUrl(String dbUrl) { |
|
100 |
this.dbUrl = dbUrl; |
|
101 |
} |
|
102 |
|
|
103 |
public String getDriver() { |
|
104 |
return driver; |
|
105 |
} |
|
106 |
|
|
107 |
public void setDriver(String driver) { |
|
108 |
this.driver = driver; |
|
109 |
} |
|
110 |
|
|
111 |
public DataSource getDatasource() { |
|
112 |
return datasource; |
|
113 |
} |
|
114 |
} |
modules/uoa-user-management/tags/uoa-user-management-2.0.5/src/main/java/eu/dnetlib/openaire/user/utils/LDAPActions.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.utils; |
|
2 |
|
|
3 |
import com.unboundid.ldap.sdk.*; |
|
4 |
import com.unboundid.ldap.sdk.extensions.PasswordModifyExtendedRequest; |
|
5 |
import com.unboundid.ldap.sdk.extensions.PasswordModifyExtendedResult; |
|
6 |
import eu.dnetlib.openaire.user.store.LDAPConnector; |
|
7 |
import org.apache.commons.validator.routines.EmailValidator; |
|
8 |
import org.apache.log4j.Logger; |
|
9 |
|
|
10 |
import java.util.ArrayList; |
|
11 |
import java.util.List; |
|
12 |
|
|
13 |
/** |
|
14 |
* Created by kiatrop on 29/9/2017. |
|
15 |
*/ |
|
16 |
|
|
17 |
public class LDAPActions { |
|
18 |
|
|
19 |
private LDAPConnector ldapConnector; |
|
20 |
|
|
21 |
private Logger logger = Logger.getLogger(LDAPConnector.class); |
|
22 |
|
|
23 |
public String getUsername(String email) throws LDAPException { |
|
24 |
Filter filter = Filter.createEqualityFilter("mail", email); |
|
25 |
SearchRequest searchRequest = new SearchRequest("dc=openaire,dc=eu", SearchScope.SUB, filter, "uid"); |
|
26 |
SearchResult searchResult = ldapConnector.getConnection().search(searchRequest); |
|
27 |
|
|
28 |
if (searchResult.getSearchEntries() != null) { |
|
29 |
if (searchResult.getSearchEntries().size() > 1) { |
|
30 |
logger.warn("An email is used for two different usernames! We only keep the first one."); |
|
31 |
} |
|
32 |
|
|
33 |
if (searchResult.getSearchEntries().size() == 0) { |
|
34 |
return null; |
|
35 |
} |
|
36 |
|
|
37 |
if (searchResult.getSearchEntries().get(0) != null) { |
|
38 |
return searchResult.getSearchEntries().get(0).getAttributeValue("uid"); |
|
39 |
} |
|
40 |
} |
|
41 |
|
|
42 |
return null; |
|
43 |
} |
|
44 |
|
|
45 |
public Entry createUser(String username, String email, String firstName, String lastName, String institution, String password) throws Exception { |
|
46 |
|
|
47 |
if(!InputValidator.isValidUsername(username)) { |
|
48 |
throw new CustomLDAPException("Invalid username!"); |
|
49 |
} |
|
50 |
|
|
51 |
if(!EmailValidator.getInstance().isValid(email)){ |
|
52 |
throw new CustomLDAPException("Invalid email!"); |
|
53 |
} |
|
54 |
|
|
55 |
if(!InputValidator.isValidPassword(password)) { |
|
56 |
throw new CustomLDAPException("Invalid password!"); |
|
57 |
} |
|
58 |
|
|
59 |
Filter uidFilter = Filter.createEqualityFilter("uid", username); |
|
60 |
SearchRequest uidRequest = new SearchRequest("dc=openaire,dc=eu", SearchScope.SUB, uidFilter, "cn", "mail", "uid", "objectClass"); |
|
61 |
|
|
62 |
Filter mailFilter = Filter.createEqualityFilter("mail", email); |
|
63 |
SearchRequest mailRequest = new SearchRequest("dc=openaire,dc=eu", SearchScope.SUB, mailFilter, "cn", "mail", "uid", "objectClass"); |
|
64 |
|
|
65 |
SearchResult searchResult = ldapConnector.getConnection().search(uidRequest); |
|
66 |
if(!searchResult.getSearchEntries().isEmpty()){ |
|
67 |
throw new CustomLDAPException("Username " + username + " already exists!"); |
|
68 |
} |
|
69 |
searchResult = ldapConnector.getConnection().search(mailRequest); |
|
70 |
if(!searchResult.getSearchEntries().isEmpty()){ |
|
71 |
throw new CustomLDAPException("Email " + email + " already exists!"); |
|
72 |
} |
|
73 |
|
|
74 |
Entry entry = new Entry("dn: uid=" + username + ",ou=users,dc=openaire,dc=eu", |
|
75 |
"objectClass: inetOrgPerson", |
|
76 |
"objectClass: eduPerson", |
|
77 |
"cn: " + username, |
|
78 |
"uid: " + username, |
|
79 |
"displayName: " + firstName + " " + lastName, |
|
80 |
"mail: " + email, |
|
81 |
"givenName: " + firstName, |
|
82 |
"sn: " + lastName, |
|
83 |
"eduPersonPrincipalName: " + username + "@openaire.eu"); |
|
84 |
|
|
85 |
if(institution != null && !institution.isEmpty()) { |
|
86 |
entry.addAttribute("o", institution); |
|
87 |
} |
|
88 |
|
|
89 |
ldapConnector.getConnection().add(entry); |
|
90 |
PasswordModifyExtendedRequest passwordModifyExtendedRequest = new PasswordModifyExtendedRequest(entry.getDN(), (String) null, password); |
|
91 |
PasswordModifyExtendedResult passwordModifyResult = (PasswordModifyExtendedResult) ldapConnector.getConnection().processExtendedOperation(passwordModifyExtendedRequest); |
|
92 |
logger.info("User: " + username + " was created successfully!"); |
|
93 |
|
|
94 |
return entry; |
|
95 |
} |
|
96 |
|
|
97 |
public void updateUser(String username, String email, String firstName, String lastName, String password) throws Exception { |
|
98 |
SearchRequest searchRequest = new SearchRequest("dc=openaire,dc=eu", SearchScope.SUB, Filter.createEqualityFilter("uid", username), "mail", "givenName", "sn", "displayName"); |
|
99 |
SearchResult searchResult = ldapConnector.getConnection().search(searchRequest); |
|
100 |
List<Modification> mods = new ArrayList<>(); |
|
101 |
|
|
102 |
if (!searchResult.getSearchEntries().isEmpty()) { |
|
103 |
Entry entry = searchResult.getSearchEntries().get(0); |
|
104 |
if(!entry.getAttributeValue("mail").equals(email)){ |
|
105 |
if(!EmailValidator.getInstance().isValid(email)){ |
|
106 |
throw new CustomLDAPException("Invalid email!"); |
|
107 |
} |
|
108 |
Filter uidFilter = Filter.createEqualityFilter("uid", username); |
|
109 |
Filter mailFilter = Filter.createEqualityFilter("mail", email); |
|
110 |
SearchRequest mailRequest = new SearchRequest("dc=openaire,dc=eu", SearchScope.SUB, Filter.createANDFilter(mailFilter, Filter.createNOTFilter(uidFilter)), "mail", "givenName", "sn", "displayName"); |
|
111 |
SearchResult mailResult = ldapConnector.getConnection().search(mailRequest); |
|
112 |
if(!mailResult.getSearchEntries().isEmpty()){ |
|
113 |
throw new CustomLDAPException("Email " + email + " already in use!"); |
|
114 |
} |
|
115 |
mods.add(new Modification(ModificationType.REPLACE, "mail", email)); |
|
116 |
} |
|
117 |
if(entry.getAttributeValue("givenName") == null){ |
|
118 |
mods.add(new Modification(ModificationType.ADD, "givenName", firstName)); |
|
119 |
} else if(!entry.getAttributeValue("givenName").equals(firstName)){ |
|
120 |
mods.add(new Modification(ModificationType.REPLACE, "givenName", firstName)); |
|
121 |
} |
|
122 |
if(entry.getAttributeValue("sn") == null){ |
|
123 |
mods.add(new Modification(ModificationType.ADD, "sn", lastName)); |
|
124 |
} else if(!entry.getAttributeValue("sn").equals(lastName)){ |
|
125 |
mods.add(new Modification(ModificationType.REPLACE, "sn", lastName)); |
|
126 |
} |
|
127 |
if(entry.getAttributeValue("displayName") == null) { |
|
128 |
mods.add(new Modification(ModificationType.ADD, "displayName", firstName + " " + lastName)); |
|
129 |
} else if (!entry.getAttributeValue("displayName").equals(firstName + " " + lastName)) { |
|
130 |
mods.add(new Modification(ModificationType.REPLACE, "displayName", firstName + " " + lastName)); |
|
131 |
} |
|
132 |
|
|
133 |
if(!InputValidator.isValidPassword(password)) { |
|
134 |
throw new CustomLDAPException("Invalid password!"); |
|
135 |
} |
|
136 |
|
|
137 |
//mods.add(new Modification(ModificationType.REPLACE, "userPassword",password)); |
|
138 |
if(!mods.isEmpty()) { |
|
139 |
ldapConnector.getConnection().modify(entry.getDN(), mods); |
|
140 |
} |
|
141 |
PasswordModifyExtendedRequest passwordModifyExtendedRequest = new PasswordModifyExtendedRequest(entry.getDN(), (String) null, password); |
|
142 |
PasswordModifyExtendedResult passwordModifyResult = (PasswordModifyExtendedResult) ldapConnector.getConnection().processExtendedOperation(passwordModifyExtendedRequest); |
|
143 |
} else { |
|
144 |
throw new CustomLDAPException("Username " + username + " not found!"); |
|
145 |
} |
|
146 |
} |
|
147 |
|
|
148 |
// public void resetPassword(String username, String email, String password) throws Exception { |
|
149 |
// SearchRequest searchRequest = new SearchRequest("dc=openaire,dc=eu", SearchScope.SUB, Filter.createEqualityFilter("uid", username), "mail"); |
|
150 |
// SearchResult searchResult = ldapConnector.getConnection().search(searchRequest); |
|
151 |
// List<Modification> mods = new ArrayList<>(); |
|
152 |
// |
|
153 |
// if (!searchResult.getSearchEntries().isEmpty()) { |
|
154 |
// Entry entry = searchResult.getSearchEntries().get(0); |
|
155 |
// if(!entry.getAttributeValue("mail").equals(email)){ |
|
156 |
// if(!EmailValidator.getInstance().isValid(email)){ |
|
157 |
// throw new CustomLDAPException("Invalid email!"); |
|
158 |
// } |
|
159 |
// Filter uidFilter = Filter.createEqualityFilter("uid", username); |
|
160 |
// Filter mailFilter = Filter.createEqualityFilter("mail", email); |
|
161 |
// SearchRequest mailRequest = new SearchRequest("dc=openaire,dc=eu", SearchScope.SUB, Filter.createANDFilter(mailFilter, Filter.createNOTFilter(uidFilter)), "mail", "givenName", "sn", "displayName"); |
|
162 |
// SearchResult mailResult = ldapConnector.getConnection().search(mailRequest); |
|
163 |
// if(!mailResult.getSearchEntries().isEmpty()){ |
|
164 |
// throw new CustomLDAPException("Email " + email + " already in use!"); |
|
165 |
// } |
|
166 |
// mods.add(new Modification(ModificationType.REPLACE, "mail", email)); |
|
167 |
// } |
|
168 |
// //mods.add(new Modification(ModificationType.REPLACE, "userPassword",password)); |
|
169 |
// if(!mods.isEmpty()) { |
|
170 |
// ldapConnector.getConnection().modify(entry.getDN(), mods); |
|
171 |
// } |
|
172 |
// if(!password.matches("(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,}")){ |
|
173 |
// throw new CustomLDAPException("Invalid password!"); |
|
174 |
// } |
|
175 |
// PasswordModifyExtendedRequest passwordModifyExtendedRequest = new PasswordModifyExtendedRequest(entry.getDN(), (String) null, password); |
|
176 |
// PasswordModifyExtendedResult passwordModifyResult = (PasswordModifyExtendedResult) ldapConnector.getConnection().processExtendedOperation(passwordModifyExtendedRequest); |
|
177 |
// } else { |
|
178 |
// throw new CustomLDAPException("Username " + username + " not found!"); |
|
179 |
// } |
|
180 |
// } |
|
181 |
|
|
182 |
public void deleteUser(String username) throws Exception { |
|
183 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
184 |
|
|
185 |
try { |
|
186 |
logger.info("User: " + username + "was deleted!"); |
|
187 |
ldapConnector.getConnection().delete("uid=" + username + "," + ldapConnector.getUsersDN()); |
|
188 |
} catch (Exception e) { |
|
189 |
logger.error("Fail to delete user.", e); |
|
190 |
throw e; |
|
191 |
} finally { |
|
192 |
if (connection != null) |
|
193 |
connection.close(); |
|
194 |
} |
|
195 |
} |
|
196 |
|
|
197 |
public void deleteZombieUser(String username) throws Exception { |
|
198 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
199 |
|
|
200 |
try { |
|
201 |
logger.info("User: " + username + "was deleted!"); |
|
202 |
ldapConnector.getConnection().delete("uid=" + username + "," + ldapConnector.getZombiesDN()); |
|
203 |
} catch (Exception e) { |
|
204 |
logger.error("Fail to delete user.", e); |
|
205 |
throw e; |
|
206 |
} finally { |
|
207 |
if (connection != null) |
|
208 |
connection.close(); |
|
209 |
} |
|
210 |
} |
|
211 |
|
|
212 |
public String getUsersEmail(String username) throws Exception { |
|
213 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
214 |
try { |
|
215 |
logger.debug("getting email for user " + username); |
|
216 |
Filter filter = Filter.createEqualityFilter("uid", username); |
|
217 |
SearchRequest searchRequest = new SearchRequest(ldapConnector.getUsersDN(), SearchScope.SUB, filter, "mail"); |
|
218 |
SearchResult searchResult = connection.search(searchRequest); |
|
219 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
220 |
return entry.getAttributeValue("mail"); |
|
221 |
} |
|
222 |
return null; |
|
223 |
} catch (Exception e) { |
|
224 |
logger.error("Fail to get user's email exists.", e); |
|
225 |
throw e; |
|
226 |
} finally { |
|
227 |
if (connection != null) |
|
228 |
connection.close(); |
|
229 |
} |
|
230 |
} |
|
231 |
|
|
232 |
public Entry createZombieUser(String username, String email, String firstName, String lastName, String institution, String password) throws Exception { |
|
233 |
|
|
234 |
if(!InputValidator.isValidUsername(username)) { |
|
235 |
throw new CustomLDAPException("Invalid username!"); |
|
236 |
} |
|
237 |
|
|
238 |
if(!EmailValidator.getInstance().isValid(email)){ |
|
239 |
throw new CustomLDAPException("Invalid email!"); |
|
240 |
} |
|
241 |
|
|
242 |
if(!InputValidator.isValidPassword(password)) { |
|
243 |
throw new CustomLDAPException("Invalid password!"); |
|
244 |
} |
|
245 |
|
|
246 |
Filter uidFilter = Filter.createEqualityFilter("uid", username); |
|
247 |
SearchRequest uidRequest = new SearchRequest("dc=openaire,dc=eu", SearchScope.SUB, uidFilter, "cn", "mail", "uid", "objectClass"); |
|
248 |
|
|
249 |
Filter mailFilter = Filter.createEqualityFilter("mail", email); |
|
250 |
SearchRequest mailRequest = new SearchRequest("dc=openaire,dc=eu", SearchScope.SUB, mailFilter, "cn", "mail", "uid", "objectClass"); |
|
251 |
|
|
252 |
SearchResult searchResult = ldapConnector.getConnection().search(uidRequest); |
|
253 |
if(!searchResult.getSearchEntries().isEmpty()){ |
|
254 |
logger.info("S" + searchResult.getSearchEntries()); |
|
255 |
throw new CustomLDAPException("Username " + username + " already exists!"); |
|
256 |
} |
|
257 |
searchResult = ldapConnector.getConnection().search(mailRequest); |
|
258 |
if(!searchResult.getSearchEntries().isEmpty()){ |
|
259 |
throw new CustomLDAPException("Email " + email + " already exists!"); |
|
260 |
} |
|
261 |
|
|
262 |
Entry entry = new Entry("dn: uid=" + username + ",ou=zombies,dc=openaire,dc=eu", |
|
263 |
"objectClass: inetOrgPerson", |
|
264 |
"objectClass: eduPerson", |
|
265 |
"cn: " + username, |
|
266 |
"uid: " + username, |
|
267 |
"displayName: " + firstName + " " + lastName, |
|
268 |
"mail: " + email, |
|
269 |
"givenName: " + firstName, |
|
270 |
"sn: " + lastName, |
|
271 |
"eduPersonPrincipalName: " + username + "@openaire.eu"); |
|
272 |
|
|
273 |
if(institution != null && !institution.isEmpty()) { |
|
274 |
entry.addAttribute("o", institution); |
|
275 |
} |
|
276 |
|
|
277 |
ldapConnector.getConnection().add(entry); |
|
278 |
PasswordModifyExtendedRequest passwordModifyExtendedRequest = new PasswordModifyExtendedRequest(entry.getDN(), (String) null, password); |
|
279 |
PasswordModifyExtendedResult passwordModifyResult = (PasswordModifyExtendedResult) ldapConnector.getConnection().processExtendedOperation(passwordModifyExtendedRequest); |
|
280 |
logger.info("User: " + username + " was created successfully!"); |
|
281 |
|
|
282 |
return entry; |
|
283 |
} |
|
284 |
|
|
285 |
public void moveUser(String username) throws Exception { |
|
286 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
287 |
|
|
288 |
try { |
|
289 |
logger.info("Moving user:" +username); |
|
290 |
ModifyDNRequest modifyDNRequest = |
|
291 |
new ModifyDNRequest("uid=" + username + ",ou=zombies,dc=openaire,dc=eu", "uid=" + username, true, "ou=users,dc=openaire,dc=eu"); |
|
292 |
|
|
293 |
LDAPResult result = connection.modifyDN(modifyDNRequest); |
|
294 |
} |
|
295 |
catch (Exception e){ |
|
296 |
logger.error("Fail to move user.", e); |
|
297 |
throw e; |
|
298 |
} finally { |
|
299 |
if (connection != null) |
|
300 |
connection.close(); |
|
301 |
} |
|
302 |
|
|
303 |
} |
|
304 |
|
|
305 |
public boolean isZombieUsersEmail(String email) throws Exception{ |
|
306 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
307 |
|
|
308 |
try { |
|
309 |
logger.debug("checking if zombie user " + email + " exists in ldap"); |
|
310 |
Filter filter = Filter.createEqualityFilter("mail", email); |
|
311 |
SearchRequest searchRequest = new SearchRequest(ldapConnector.getZombiesDN(), SearchScope.SUB, filter, "mail"); |
|
312 |
SearchResult searchResult = connection.search(searchRequest); |
|
313 |
|
|
314 |
if (!searchResult.getSearchEntries().isEmpty()) { |
|
315 |
logger.info("Zombie user with email: " + email + " exists!"); |
|
316 |
return true; |
|
317 |
} else { |
|
318 |
return false; |
|
319 |
} |
|
320 |
} catch (Exception e) { |
|
321 |
logger.error("Fail to check if zombie user email exists.", e); |
|
322 |
throw e; |
|
323 |
} finally { |
|
324 |
if (connection != null) |
|
325 |
connection.close(); |
|
326 |
} |
|
327 |
} |
|
328 |
|
|
329 |
public boolean isZombieUsersUsername(String username) throws Exception { |
|
330 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
331 |
|
|
332 |
try { |
|
333 |
logger.debug("checking if zombie user " + username + " exists in ldap"); |
|
334 |
Filter filter = Filter.createEqualityFilter("uid", username); |
|
335 |
SearchRequest searchRequest = new SearchRequest(ldapConnector.getZombiesDN(), SearchScope.SUB, filter, "uid"); |
|
336 |
SearchResult searchResult = connection.search(searchRequest); |
|
337 |
|
|
338 |
if (!searchResult.getSearchEntries().isEmpty()) { |
|
339 |
logger.info("Zombie user with username: " + username + " exists!"); |
|
340 |
return true; |
|
341 |
} else { |
|
342 |
return false; |
|
343 |
} |
|
344 |
|
|
345 |
} catch (Exception e) { |
|
346 |
logger.error("Fail to check if zombie username exists.", e); |
|
347 |
throw e; |
|
348 |
} finally { |
|
349 |
if (connection != null) |
|
350 |
connection.close(); |
|
351 |
} |
|
352 |
} |
|
353 |
|
|
354 |
public String getZombieUsersEmail(String username) throws Exception { |
|
355 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
356 |
try { |
|
357 |
logger.debug("getting email for zombie user " + username); |
|
358 |
Filter filter = Filter.createEqualityFilter("uid", username); |
|
359 |
SearchRequest searchRequest = new SearchRequest(ldapConnector.getZombiesDN(), SearchScope.SUB, filter, "mail"); |
|
360 |
SearchResult searchResult = connection.search(searchRequest); |
|
361 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
362 |
return entry.getAttributeValue("mail"); |
|
363 |
} |
|
364 |
return null; |
|
365 |
} catch (Exception e) { |
|
366 |
logger.error("Fail to get zombie user's email exists.", e); |
|
367 |
throw e; |
|
368 |
} finally { |
|
369 |
if (connection != null) |
|
370 |
connection.close(); |
|
371 |
} |
|
372 |
} |
|
373 |
|
|
374 |
public String getZombieUsersUserName(String email) throws LDAPException { |
|
375 |
Filter filter = Filter.createEqualityFilter("mail", email); |
|
376 |
SearchRequest searchRequest = new SearchRequest(ldapConnector.getZombiesDN(), SearchScope.SUB, filter, "uid"); |
|
377 |
SearchResult searchResult = ldapConnector.getConnection().search(searchRequest); |
|
378 |
|
|
379 |
if (searchResult.getSearchEntries() != null) { |
|
380 |
if (searchResult.getSearchEntries().size() > 1) { |
|
381 |
logger.warn("An email is used for two different usernames! We only keep the first one."); |
|
382 |
} |
|
383 |
|
|
384 |
if (searchResult.getSearchEntries().size() == 0) { |
|
385 |
return null; |
|
386 |
} |
|
387 |
|
|
388 |
if (searchResult.getSearchEntries().get(0) != null) { |
|
389 |
return searchResult.getSearchEntries().get(0).getAttributeValue("uid"); |
|
390 |
} |
|
391 |
} |
|
392 |
|
|
393 |
return null; |
|
394 |
} |
|
395 |
|
|
396 |
public boolean emailExists(String email) throws Exception { |
|
397 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
398 |
|
|
399 |
try { |
|
400 |
logger.debug("checking if user " + email + " exists in ldap"); |
|
401 |
Filter filter = Filter.createEqualityFilter("mail", email); |
|
402 |
SearchRequest searchRequest = new SearchRequest(ldapConnector.getUsersDN(), SearchScope.SUB, filter, "mail"); |
|
403 |
SearchResult searchResult = connection.search(searchRequest); |
|
404 |
|
|
405 |
if (!searchResult.getSearchEntries().isEmpty()) { |
|
406 |
logger.info("User with email: " + email + " exists!"); |
|
407 |
return true; |
|
408 |
} else { |
|
409 |
return false; |
|
410 |
} |
|
411 |
} catch (Exception e) { |
|
412 |
logger.error("Fail to check if user email exists.", e); |
|
413 |
throw e; |
|
414 |
} finally { |
|
415 |
if (connection != null) |
|
416 |
connection.close(); |
|
417 |
} |
|
418 |
} |
|
419 |
|
|
420 |
public boolean usernameExists(String username) throws Exception { |
|
421 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
422 |
|
|
423 |
try { |
|
424 |
logger.debug("checking if user " + username + " exists in ldap"); |
|
425 |
Filter filter = Filter.createEqualityFilter("uid", username); |
|
426 |
SearchRequest searchRequest = new SearchRequest(ldapConnector.getUsersDN(), SearchScope.SUB, filter, "uid"); |
|
427 |
SearchResult searchResult = connection.search(searchRequest); |
|
428 |
|
|
429 |
if (!searchResult.getSearchEntries().isEmpty()) { |
|
430 |
logger.info("User with username: " + username + " exists!"); |
|
431 |
return true; |
|
432 |
} else { |
|
433 |
return false; |
|
434 |
} |
|
435 |
|
|
436 |
} catch (Exception e) { |
|
437 |
logger.error("Fail to check if username exists.", e); |
|
438 |
throw e; |
|
439 |
} finally { |
|
440 |
if (connection != null) |
|
441 |
connection.close(); |
|
442 |
} |
|
443 |
} |
|
444 |
|
|
445 |
public void resetPassword(String username, String password) throws Exception { |
|
446 |
LDAPConnection connection = ldapConnector.getConnection(); |
|
447 |
|
|
448 |
try { |
|
449 |
|
|
450 |
Filter filter = Filter.createEqualityFilter("uid", username); |
|
451 |
SearchRequest searchRequest = new SearchRequest(ldapConnector.getUsersDN(), SearchScope.SUB, filter, "uid"); |
|
452 |
SearchResult searchResult = connection.search(searchRequest); |
|
453 |
String dn = null; |
|
454 |
for (SearchResultEntry entry : searchResult.getSearchEntries()) { |
|
455 |
dn = "uid=" + entry.getAttributeValue("uid") + "," + ldapConnector.getUsersDN(); |
|
456 |
//logger.info("dn " + dn); |
|
457 |
} |
|
458 |
|
|
459 |
if(!InputValidator.isValidPassword(password)) { |
|
460 |
throw new CustomLDAPException("Invalid password!"); |
|
461 |
} |
|
462 |
|
|
463 |
//Modification mod1 = new Modification(ModificationType.REPLACE, "userPassword", password); |
|
464 |
PasswordModifyExtendedRequest passwordModifyExtendedRequest = new PasswordModifyExtendedRequest(dn, (String) null, password); |
|
465 |
PasswordModifyExtendedResult passwordModifyResult = (PasswordModifyExtendedResult) ldapConnector.getConnection().processExtendedOperation(passwordModifyExtendedRequest); |
|
466 |
//connection.modify(dn, mod1); |
|
467 |
|
|
468 |
} catch (Exception e) { |
|
469 |
logger.error("Fail to reset password.", e); |
|
470 |
throw e; |
|
471 |
|
|
472 |
} finally { |
|
473 |
if (connection != null) |
|
474 |
connection.close(); |
|
475 |
} |
|
476 |
} |
|
477 |
|
|
478 |
|
|
479 |
public LDAPConnector getLdapConnector() { |
|
480 |
return ldapConnector; |
|
481 |
} |
|
482 |
|
|
483 |
public void setLdapConnector(LDAPConnector ldapConnector) { |
|
484 |
this.ldapConnector = ldapConnector; |
|
485 |
} |
|
486 |
} |
modules/uoa-user-management/tags/uoa-user-management-2.0.5/src/main/java/eu/dnetlib/openaire/user/store/Statement.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.store; |
|
2 |
|
|
3 |
import java.sql.PreparedStatement; |
|
4 |
import java.sql.SQLException; |
|
5 |
|
|
6 |
/** |
|
7 |
* Created by sofia on 28/6/2016. |
|
8 |
*/ |
|
9 |
|
|
10 |
public class Statement |
|
11 |
{ |
|
12 |
public interface Initializer { |
|
13 |
void prepare(PreparedStatement stmt) throws SQLException; |
|
14 |
} |
|
15 |
|
|
16 |
private static final Initializer EMPTY = new Initializer() { |
|
17 |
|
|
18 |
@Override |
|
19 |
public void prepare(PreparedStatement stmt) throws SQLException {} |
|
20 |
}; |
|
21 |
|
|
22 |
public static Initializer emptyInitializer() { |
|
23 |
return EMPTY; |
|
24 |
} |
|
25 |
} |
modules/uoa-user-management/tags/uoa-user-management-2.0.5/src/main/java/eu/dnetlib/openaire/user/utils/VerificationActions.java | ||
---|---|---|
1 |
package eu.dnetlib.openaire.user.utils; |
|
2 |
|
|
3 |
import eu.dnetlib.openaire.user.dao.UserVerificationDAO; |
|
4 |
import eu.dnetlib.openaire.user.pojos.UserVerification; |
|
5 |
import eu.dnetlib.openaire.user.store.DataSourceConnector; |
|
6 |
import org.apache.log4j.Logger; |
|
7 |
import org.springframework.beans.factory.annotation.Autowired; |
|
8 |
import org.springframework.stereotype.Component; |
|
9 |
|
|
10 |
import java.sql.SQLException; |
|
11 |
import java.sql.Timestamp; |
|
12 |
import java.time.LocalDateTime; |
|
13 |
import java.time.temporal.ChronoUnit; |
|
14 |
|
|
15 |
/** |
|
16 |
* Created by kiatrop on 2/10/2017. |
|
17 |
*/ |
|
18 |
@Component(value = "verificationActions") |
|
19 |
public class VerificationActions { |
|
20 |
|
|
21 |
@Autowired |
|
22 |
private DataSourceConnector dataSourceConnector; |
|
23 |
|
|
24 |
private Logger logger = Logger.getLogger(VerificationActions.class); |
|
25 |
|
|
26 |
@Autowired |
|
27 |
private UserVerificationDAO userVerificationDAO; |
|
28 |
|
|
29 |
/** |
|
30 |
* Adds new row in the verification table |
|
31 |
*/ |
|
32 |
public void addVerificationEntry(String username, String verificationCode, Timestamp date) { |
|
33 |
|
|
34 |
try { |
|
35 |
UserVerification userVerification = new UserVerification(username); |
|
36 |
logger.info("verificationCode: " + verificationCode); |
|
37 |
userVerification.setVerificationCode(verificationCode); |
|
38 |
userVerification.setDate(date); |
|
39 |
|
|
40 |
userVerificationDAO.insert(userVerification); |
|
41 |
|
|
42 |
logger.info("Insert user: " + username); |
|
43 |
|
|
44 |
} catch (SQLException e) {; |
|
45 |
logger.error("Fail to insert user.", e); |
|
46 |
} |
|
47 |
} |
|
48 |
|
|
49 |
/** |
|
50 |
* Updates the row of verification table with the new date for the given username |
|
51 |
*/ |
|
52 |
public void updateVerificationEntry(String username, String verificationCode, Timestamp date) { |
|
53 |
|
|
54 |
try { |
|
55 |
|
|
56 |
// Get userId to update user |
|
57 |
UserVerification userVerificationOld = userVerificationDAO.fetchByUsername(username); |
|
58 |
UserVerification userVerification = new UserVerification(username); |
|
59 |
userVerification.setVerificationCode(verificationCode); |
|
60 |
userVerification.setDate(date); |
|
61 |
userVerification.setId(userVerificationOld.getId()); |
|
62 |
|
|
63 |
userVerificationDAO.update(userVerification); |
|
64 |
|
|
65 |
//logger.info("Update user: " + username + " with verification code " + verificationCode); |
|
66 |
|
|
67 |
} catch (SQLException e) { |
|
68 |
logger.error("Fail to update user.", e); |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
/** |
|
73 |
* Delete a verification with a specific id |
|
74 |
*/ |
|
75 |
public void deleteVerificationEntry(String username) { |
|
76 |
|
|
77 |
try { |
|
78 |
UserVerification userVerification = new UserVerification(username); |
|
79 |
userVerificationDAO.delete(userVerification); |
|
80 |
logger.info("Delete user: " + username); |
|
81 |
} catch (SQLException e) {; |
|
82 |
logger.error("Fail to delete user.", e); |
|
83 |
} |
|
84 |
} |
|
85 |
|
|
86 |
/** |
|
87 |
* Checks if the verification row exists for the given username |
|
88 |
*/ |
|
89 |
public boolean verificationEntryExists(String username) { |
|
90 |
|
|
91 |
try { |
|
92 |
UserVerification userVerification = userVerificationDAO.fetchByUsername(username); |
|
93 |
|
|
94 |
if (userVerification == null) { |
|
95 |
logger.info("There is no user with username: " + username); |
|
96 |
return false; |
|
97 |
} |
|
98 |
else { |
|
99 |
logger.info("User: " + username + " was found!"); |
|
100 |
return true; |
|
101 |
} |
|
102 |
} catch (SQLException e) { |
|
103 |
logger.error("Fail to search user.", e); |
|
104 |
} |
|
105 |
return true; |
|
106 |
} |
|
107 |
|
|
108 |
/** |
|
109 |
* Checks if the verification row exists for the given username |
|
110 |
*/ |
|
111 |
public boolean verificationCodeIsCorrect(String username, String verificationCode) { |
|
112 |
|
|
113 |
try { |
|
114 |
UserVerification userVerification = userVerificationDAO.fetchByUsername(username); |
|
115 |
|
|
116 |
if (userVerification == null) { |
|
117 |
logger.info("There is no user with username: " + username); |
|
118 |
return false; |
|
119 |
} |
|
120 |
else { |
|
121 |
logger.info("User: " + username + " was found!"); |
|
122 |
|
|
123 |
if (verificationCode.equals(userVerification.getVerificationCode())) { |
|
124 |
logger.info("Verification Code is correct!"); |
|
125 |
//logger.info("... and verification code: " + verificationCode + " is correct!"); |
|
126 |
return true; |
|
127 |
} |
|
128 |
} |
|
129 |
} catch (SQLException e) { |
|
130 |
logger.error("Fail to search user.", e); |
|
131 |
} |
|
132 |
return false; |
|
133 |
} |
|
134 |
|
|
135 |
/** |
Also available in: Unified diff
[maven-release-plugin] copy for tag uoa-user-management-2.0.5