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

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


    return result;
  }

  public JSONObject validateUserCreation(String b64Token) throws Exception {
    byte[] encryptedToken = Base64.decodeBase64(b64Token);
    Token token = TokenEncryptor.decryptToken(serverKey, encryptedToken);
    if( token instanceof CreationToken ){
      CreationToken creationToken = (CreationToken)token;
     
      Date expiry = creationToken.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 = creationToken.getEmailAddress();
      if( null == emailAddress ) {
        throw new Exception("Token does not specify e-mail address");
      }
      JSONObject user = null;
      try {
        user = userRepository.getUserFromEmailAddress(emailAddress);
      } catch(Exception e) {
        // OK
      }
      if( null != user ) {
        throw new Exception("User with e-mail "+emailAddress+
            " already exists. Attempt password recovery.");
      }
     
      JSONObject result = new JSONObject();
      result.put("valid", true);
      result.put("emailAddress", creationToken.getEmailAddress());
      return result;
    } else {
      throw new Exception("Unexpected token class: "+token.getClass().getName());
    }
  }
View Full Code Here


    return result;
  }

  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

    return result;
  }

  public JSONObject validateUserCreation(String b64Token) throws Exception {
    byte[] encryptedToken = Base64.decodeBase64(b64Token);
    Token token = TokenEncryptor.decryptToken(serverKey, encryptedToken);
    if( token instanceof CreationToken ){
      CreationToken creationToken = (CreationToken)token;
     
      Date expiry = creationToken.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 = creationToken.getEmailAddress();
      if( null == emailAddress ) {
        throw new Exception("Token does not specify e-mail address");
      }
      JSONObject user = null;
      try {
        user = userRepository.getUserFromEmailAddress(emailAddress);
      } catch(Exception e) {
        // OK
      }
      if( null != user ) {
        throw new Exception("User with e-mail "+emailAddress+
            " already exists. Attempt password recovery.");
      }
     
      JSONObject result = new JSONObject();
      result.put("valid", true);
      result.put("emailAddress", creationToken.getEmailAddress());
      return result;
    } else {
      throw new Exception("Unexpected token class: "+token.getClass().getName());
    }
  }
View Full Code Here

    return result;
  }

  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.Token

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.