Project

General

Profile

« Previous | Next » 

Revision 8429

Added by Jing Tao over 10 years ago

Add the methods about reset and change password.

View differences:

src/edu/ucsb/nceas/metacat/authentication/AuthFile.java
29 29
import java.security.GeneralSecurityException;
30 30
import java.util.HashMap;
31 31
import java.util.List;
32
import java.util.Random;
32 33
import java.util.Vector;
33 34

  
34 35
import javax.crypto.Cipher;
......
354 355
     * @param password  the password of the user.
355 356
     * @return
356 357
     */
357
    public String modifyUserPassword(String userName, String password) {
358
    public String resetPassword(String userName) throws AuthenticationException {
359
        String password = new String(RandomPasswordGenerator.generatePswd(10, 12, 4, 3, 2));
360
        changePassword(userName, password);
358 361
        return password;
359 362
    }
360 363
    
361 364
    /**
365
     * Change the password of the user to the new one. But we need to know the old password
366
     * @param usrName the specified user.   
367
     * @param oldPassword the old password of the user      
368
     * @param newPassword the new password which will be set
369
     */
370
    public void modifyPassword(String userName, String oldPassword, String newPassword) throws AuthenticationException {
371
        if(!authenticate(userName, oldPassword)) {
372
            throw new AuthenticationException("AuthFile.modifyUserPassword - the username or the old password is not correct");
373
        }
374
        changePassword(userName, newPassword);
375
    }
376
    
377
    /**
362 378
     * Add a user to a group
363 379
     * @param userName  the name of the user. the user should already exist
364 380
     * @param group  the name of the group. the group should already exist
......
377 393
    }
378 394
    
379 395
    /**
396
     * Change the password of the user to the specified one
397
     * @param userName
398
     * @param password
399
     */
400
    private void changePassword(String userName, String password) throws AuthenticationException{
401
        if(!userExists(userName)) {
402
            throw new AuthenticationException("AuthFile.changePassword - can't change the password for the user "+userName+" since it doesn't eixt.");
403
        }
404
        String encryped = null;
405
        try {
406
            encryped = encrypt(password);
407
        } catch (Exception e) {
408
            throw new AuthenticationException("AuthFile.changepassword - can't encrype the new password for the user "+userName+" since "+e.getMessage());
409
        }
410
        userpassword.setProperty(USERS+SLASH+USER+"["+AT+NAME+"='"+userName+"']"+SLASH+PASSWORD, encryped);
411
    }
412
    
413
    /**
380 414
     * If the specified user name exist or not
381 415
     * @param userName the name of the user
382 416
     * @return true if the user eixsit
......
446 480
    private static byte[] base64Decode(String property) throws IOException {
447 481
        return Base64.decodeBase64(property);
448 482
    }
483
    
484
    /**
485
     * A internal class to generate random passowrd
486
     * @author tao
487
     *
488
     */
489
    static class RandomPasswordGenerator {
490
        private static final String ALPHA_CAPS  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
491
        private static final String ALPHA   = "abcdefghijklmnopqrstuvwxyz";
492
        private static final String NUM     = "0123456789";
493
        private static final String SPL_CHARS   = "!$^_-/";
494
     
495
        public static char[] generatePswd(int minLen, int maxLen, int noOfCAPSAlpha,
496
                int noOfDigits, int noOfSplChars) {
497
            if(minLen > maxLen)
498
                throw new IllegalArgumentException("Min. Length > Max. Length!");
499
            if( (noOfCAPSAlpha + noOfDigits + noOfSplChars) > minLen )
500
                throw new IllegalArgumentException
501
                ("Min. Length should be atleast sum of (CAPS, DIGITS, SPL CHARS) Length!");
502
            Random rnd = new Random();
503
            int len = rnd.nextInt(maxLen - minLen + 1) + minLen;
504
            char[] pswd = new char[len];
505
            int index = 0;
506
            for (int i = 0; i < noOfCAPSAlpha; i++) {
507
                index = getNextIndex(rnd, len, pswd);
508
                pswd[index] = ALPHA_CAPS.charAt(rnd.nextInt(ALPHA_CAPS.length()));
509
            }
510
            for (int i = 0; i < noOfDigits; i++) {
511
                index = getNextIndex(rnd, len, pswd);
512
                pswd[index] = NUM.charAt(rnd.nextInt(NUM.length()));
513
            }
514
            for (int i = 0; i < noOfSplChars; i++) {
515
                index = getNextIndex(rnd, len, pswd);
516
                pswd[index] = SPL_CHARS.charAt(rnd.nextInt(SPL_CHARS.length()));
517
            }
518
            for(int i = 0; i < len; i++) {
519
                if(pswd[i] == 0) {
520
                    pswd[i] = ALPHA.charAt(rnd.nextInt(ALPHA.length()));
521
                }
522
            }
523
            return pswd;
524
        }
525
     
526
        private static int getNextIndex(Random rnd, int len, char[] pswd) {
527
            int index = rnd.nextInt(len);
528
            while(pswd[index = rnd.nextInt(len)] != 0);
529
            return index;
530
        }
531
    }
449 532

  
450 533
}

Also available in: Unified diff