Project

General

Profile

« Previous | Next » 

Revision 51979

Add InputValidator.java and Use it instead of regular expressions and Fix email unicode

View differences:

modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/utils/LDAPActions.java
55 55
            throw new CustomLDAPException("Invalid email!");
56 56
        }
57 57

  
58
        if(!InputValidator.isValidPassword(password)) {
59
            throw new CustomLDAPException("Invalid password!");
60
        }
61

  
58 62
        Filter uidFilter = Filter.createEqualityFilter("uid", username);
59 63
        SearchRequest uidRequest = new SearchRequest("dc=openaire,dc=eu", SearchScope.SUB, uidFilter, "cn", "mail", "uid", "objectClass");
60 64

  
......
80 84
                "givenName: " + firstName,
81 85
                "sn: " + lastName,
82 86
                "eduPersonPrincipalName: " + username + "@openaire.eu");
87

  
83 88
        if(institution != null && !institution.isEmpty()) {
84 89
            entry.addAttribute("o", institution);
85 90
        }
91

  
86 92
        ldapConnector.getConnection().add(entry);
87 93
        PasswordModifyExtendedRequest passwordModifyExtendedRequest = new PasswordModifyExtendedRequest(entry.getDN(), (String) null, password);
88 94
        PasswordModifyExtendedResult passwordModifyResult = (PasswordModifyExtendedResult) ldapConnector.getConnection().processExtendedOperation(passwordModifyExtendedRequest);
......
127 133
                mods.add(new Modification(ModificationType.REPLACE, "displayName", firstName + " " + lastName));
128 134
            }
129 135

  
136
            if(!InputValidator.isValidPassword(password)) {
137
                throw new CustomLDAPException("Invalid password!");
138
            }
139

  
130 140
            //mods.add(new Modification(ModificationType.REPLACE, "userPassword",password));
131 141
            if(!mods.isEmpty()) {
132 142
                ldapConnector.getConnection().modify(entry.getDN(), mods);
......
196 206

  
197 207
    public Entry createZombieUser(String username, String email, String firstName, String lastName, String institution, String password) throws Exception {
198 208

  
199
        if(!username.matches("^[a-zA-Z0-9][a-zA-Z0-9\\.\\_\\-]{4,150}")){
209
        if(!InputValidator.isValidUsername(username)) {
200 210
            throw new CustomLDAPException("Invalid username!");
201 211
        }
202 212

  
......
204 214
            throw new CustomLDAPException("Invalid email!");
205 215
        }
206 216

  
217
        if(!InputValidator.isValidPassword(password)) {
218
            throw new CustomLDAPException("Invalid password!");
219
        }
220

  
207 221
        Filter uidFilter = Filter.createEqualityFilter("uid", username);
208 222
        SearchRequest uidRequest = new SearchRequest("dc=openaire,dc=eu", SearchScope.SUB, uidFilter, "cn", "mail", "uid", "objectClass");
209 223

  
......
230 244
                "givenName: " + firstName,
231 245
                "sn: " + lastName,
232 246
                "eduPersonPrincipalName: " + username + "@openaire.eu");
247

  
233 248
        if(institution != null && !institution.isEmpty()) {
234 249
            entry.addAttribute("o", institution);
235 250
        }
251

  
236 252
        ldapConnector.getConnection().add(entry);
237 253
        PasswordModifyExtendedRequest passwordModifyExtendedRequest = new PasswordModifyExtendedRequest(entry.getDN(), (String) null, password);
238 254
        PasswordModifyExtendedResult passwordModifyResult = (PasswordModifyExtendedResult) ldapConnector.getConnection().processExtendedOperation(passwordModifyExtendedRequest);
......
249 265
            ModifyDNRequest modifyDNRequest =
250 266
                    new ModifyDNRequest("uid=" + username + ",ou=zombies,dc=openaire,dc=eu", "uid=" + username, true, "ou=users,dc=openaire,dc=eu");
251 267

  
252
//            ModifyDNRequest modifyDNRequest =
253
//                    new ModifyDNRequest("uid=" + username + ",ou=zombies,dc=openaire,dc=eu","uid=" + username + ",ou=users,dc=openaire,dc=eu", true);
254
//
255 268
            LDAPResult result = connection.modifyDN(modifyDNRequest);
256 269
        }
257 270
        catch (Exception e){
......
377 390
                //logger.info("dn " + dn);
378 391
            }
379 392

  
380
            if(!password.matches("(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{6,}")){
393
            if(!InputValidator.isValidPassword(password)) {
381 394
                throw new CustomLDAPException("Invalid password!");
382 395
            }
383 396

  
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/utils/InputValidator.java
1
package eu.dnetlib.openaire.user.utils;
2

  
3
import org.apache.log4j.Logger;
4

  
5
/**
6
 * Created by sofia on 20/4/2018.
7
 */
8
public class InputValidator {
9

  
10
    private static Logger logger = Logger.getLogger(InputValidator.class);
11

  
12
    /*
13
         ^                # start-of-string
14
        (?=.*[0-9])       # a digit must occur at least once
15
        (?=.*[a-z])       # a lower case letter must occur at least once
16
        (?=.*[A-Z])       # an upper case letter must occur at least once
17
        (?=.*[@#$%^&+=])  # a special character must occur at least once. This has been removed.
18
                          # Please add if special character is needed.
19
        (?=\S+$)          # no whitespace allowed in the entire string
20
        .{6,}             # anything, at least six places though
21
        $                 # end-of-string
22
    */
23

  
24
    public static String validPassword = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=\\S+$).{6,}$";
25

  
26
    /*
27
        ^[a-zA-Z0-9]            # starts with character or digit
28
        ^[a-zA-Z0-9\\.\\_\\-]   # contains only characters, numbers, underscores, hyphens, periods
29
        {4,150}                 # anything, at least eight places though
30
        $                       # end-of-string
31
    */
32
    public static String validUsername = "^[a-zA-Z0-9][a-zA-Z0-9\\.\\_\\-]{4,150}";
33

  
34
    /*
35
        ^[a-zA-Z0-9]            # starts with character or digit
36
    */
37
    public static String startsWith = "^[a-zA-Z0-9].*";
38

  
39
    /*
40
        "^[a-zA-Z0-9\\.\\_\\-]"  #contains only characters, numbers, underscores, hyphens, periods
41
    */
42
    public static String allowedChars = "^[a-zA-Z0-9\\.\\_\\-]";
43

  
44
    public static boolean isFilled(String input) {
45
        return (input != null && !input.isEmpty());
46
    }
47

  
48
    public static boolean isValidPassword(String password) {
49
        return password.matches(validPassword);
50
    }
51

  
52
    public static boolean isValidUsername(String username) {
53
        return username.matches(validUsername);
54
    }
55

  
56
    public static boolean startsWithLetterOrDigit(String username) {
57
        return username.matches(startsWith);
58
    }
59

  
60
    public static boolean containsOnlyAllowedChars(String username) {
61
        return username.matches(allowedChars);
62
    }
63

  
64
    public static boolean containsLessCharsThan(int count, String input) {
65
        return (input.length() < count);
66
    }
67

  
68
    public static boolean containsMoreCharsThan(int count, String input) {
69
        return (input.length() > count);
70
    }
71
}
modules/uoa-user-management/trunk/src/main/java/eu/dnetlib/openaire/user/utils/EmailSender.java
35 35
        //properties.put("mail.smtp.starttls.enable", "true");
36 36
        //properties.put("mail.smtp.ssl.trust", "*");
37 37

  
38
        //Dev for sending email from gmail
39
        /*
40
        properties.put("mail.smtp.auth", "true"); //enable authentication
41
        properties.put("mail.smtp.starttls.enable", "true");
42
        properties.put("mail.smtp.ssl.trust", "*");
43
        */
38 44
        Session session = javax.mail.Session.getInstance(properties,
39 45
            new Authenticator() {
40 46
                protected PasswordAuthentication getPasswordAuthentication() {
......
52 58
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(recipient));
53 59

  
54 60
            // Set Subject: header field
55
            message.setSubject(subject);
61
            message.setSubject(subject, "UTF-8");
56 62

  
57 63
            // For simple text setText() can be used instead of setContent()
58 64

  
59 65
            // Send the actual HTML message, as big as you like
60
            message.setContent(body, "text/html");
66
            message.setContent(body, "text/html;charset=UTF-8");
61 67

  
62 68
            // Send message
63 69
            Transport.send(message);

Also available in: Unified diff