Package ca.carleton.gcrc.couch.user.token

Examples of ca.carleton.gcrc.couch.user.token.PasswordRecoveryToken


    if( null == user ){
      throw new Exception("No user assciated with e-mail address: "+emailAddr);
    }

    // Create token
    PasswordRecoveryToken passwordRecoveryToken = new PasswordRecoveryToken();
    {
      passwordRecoveryToken.setEmailAddress(emailAddr);
     
      long now = (new Date()).getTime();
      long expiryPeriodInMs = 1L * 24L * 60L * 60L * 1000L; // 1 day
      long expiryTime = now + expiryPeriodInMs;
      passwordRecoveryToken.setExpiry( new Date(expiryTime) );
     
      passwordRecoveryToken.setVersion( user.getString("_rev").substring(0,5) );
    }
   
    // Encrypt token
    if( null == serverKey ){
      throw new Exception("Server key was not installed. Configuration must be adjusted.");
View Full Code Here


  public JSONObject validatePasswordRecovery(String b64Token) throws Exception {
    byte[] encryptedToken = Base64.decodeBase64(b64Token);
    Token token = TokenEncryptor.decryptToken(serverKey, encryptedToken);
    if( token instanceof PasswordRecoveryToken ){
      PasswordRecoveryToken passwordRecoveryToken = (PasswordRecoveryToken)token;
     
      // Check expiry time
      Date expiry = passwordRecoveryToken.getExpiry();
      if( null != expiry ){
        Date now = new Date();
        if( now.getTime() > expiry.getTime() ){
          throw new TokenExpiredException("Token is expired");
        }
      }
     
      // Check if user already exists
      String emailAddress = passwordRecoveryToken.getEmailAddress();
      if( null == emailAddress ) {
        throw new Exception("Token does not specify an e-mail address");
      }
      JSONObject userDoc = null;
      try {
        logger.error("userRepository: "+userRepository);
        userDoc = userRepository.getUserFromEmailAddress(emailAddress);
      } catch(Exception e) {
        logger.error("Error",e);
        throw new Exception("There is no user associated with the e-mail address: "+emailAddress+
            ". You must first create a user account.",e);
      }
     
      // Verify that user document was not modified since the token was generated
      {
        String rev = userDoc.optString("_rev");
        if( null == rev ){
          throw new Exception("Revision not available from user document");
        }
        String tokenRev = passwordRecoveryToken.getVersion();
        if( null == tokenRev ){
          throw new Exception("Revision not provided in password recovery token");
        }
        if( false == tokenRev.equals( rev.substring(0, tokenRev.length()) ) ){
          throw new UserUpdatedException("Password recovery token refers to an older version of the user document");
        }
      }
     
      JSONObject result = new JSONObject();
      result.put("valid", true);
      result.put("emailAddress", passwordRecoveryToken.getEmailAddress());
      result.put("name", userDoc.getString("name"));
      return result;
    } else {
      throw new Exception("Unexpected token class: "+token.getClass().getName());
    }
View Full Code Here

    if( null == user ){
      throw new Exception("No user assciated with e-mail address: "+emailAddr);
    }

    // Create token
    PasswordRecoveryToken passwordRecoveryToken = new PasswordRecoveryToken();
    {
      passwordRecoveryToken.setEmailAddress(emailAddr);
     
      long now = (new Date()).getTime();
      long expiryPeriodInMs = 1L * 24L * 60L * 60L * 1000L; // 1 day
      long expiryTime = now + expiryPeriodInMs;
      passwordRecoveryToken.setExpiry( new Date(expiryTime) );
     
      passwordRecoveryToken.setVersion( user.getString("_rev").substring(0,5) );
    }
   
    // Encrypt token
    if( null == serverKey ){
      throw new Exception("Server key was not installed. Configuration must be adjusted.");
View Full Code Here

  public JSONObject validatePasswordRecovery(String b64Token) throws Exception {
    byte[] encryptedToken = Base64.decodeBase64(b64Token);
    Token token = TokenEncryptor.decryptToken(serverKey, encryptedToken);
    if( token instanceof PasswordRecoveryToken ){
      PasswordRecoveryToken passwordRecoveryToken = (PasswordRecoveryToken)token;
     
      // Check expiry time
      Date expiry = passwordRecoveryToken.getExpiry();
      if( null != expiry ){
        Date now = new Date();
        if( now.getTime() > expiry.getTime() ){
          throw new TokenExpiredException("Token is expired");
        }
      }
     
      // Check if user already exists
      String emailAddress = passwordRecoveryToken.getEmailAddress();
      if( null == emailAddress ) {
        throw new Exception("Token does not specify an e-mail address");
      }
      JSONObject userDoc = null;
      try {
        logger.error("userRepository: "+userRepository);
        userDoc = userRepository.getUserFromEmailAddress(emailAddress);
      } catch(Exception e) {
        logger.error("Error",e);
        throw new Exception("There is no user associated with the e-mail address: "+emailAddress+
            ". You must first create a user account.",e);
      }
     
      // Verify that user document was not modified since the token was generated
      {
        String rev = userDoc.optString("_rev");
        if( null == rev ){
          throw new Exception("Revision not available from user document");
        }
        String tokenRev = passwordRecoveryToken.getVersion();
        if( null == tokenRev ){
          throw new Exception("Revision not provided in password recovery token");
        }
        if( false == tokenRev.equals( rev.substring(0, tokenRev.length()) ) ){
          throw new UserUpdatedException("Password recovery token refers to an older version of the user document");
        }
      }
     
      JSONObject result = new JSONObject();
      result.put("valid", true);
      result.put("emailAddress", passwordRecoveryToken.getEmailAddress());
      result.put("name", userDoc.getString("name"));
      return result;
    } else {
      throw new Exception("Unexpected token class: "+token.getClass().getName());
    }
View Full Code Here

TOP

Related Classes of ca.carleton.gcrc.couch.user.token.PasswordRecoveryToken

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.