Skip to content

Commit

Permalink
Updated Password regex
Browse files Browse the repository at this point in the history
Signed-off-by: Claudio Mezzasalma <claudio.mezzasalma@eurotech.com>
  • Loading branch information
Claudio Mezzasalma authored and Coduz committed Dec 12, 2018
1 parent 3cd22cf commit 36ecd2c
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 118 deletions.
Expand Up @@ -33,7 +33,7 @@ public enum CommonsValidationRegex implements ValidationRegex {
/**
* ^.*(?=.{12,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&amp;+=!\~\|]).*$
*/
PASSWORD_REGEXP("^.*(?=.{12,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=!\\~\\|]).*$"),
PASSWORD_REGEXP("^.*(?=.{12,})(?=.*\\p{Digit})(?=.*\\p{Lower})(?=.*\\p{Upper})(?=.*\\p{Punct}).*$"),

/**
* ^(\w+)([-+.][\w]+)*@(\w[-\w]*\.){1,5}([A-Za-z]){2,4}$
Expand Down Expand Up @@ -83,4 +83,4 @@ public enum CommonsValidationRegex implements ValidationRegex {
public Pattern getPattern() {
return pattern;
}
}
}

This file was deleted.

Expand Up @@ -63,7 +63,7 @@ public enum FieldType {
SNAPSHOT_FILE("snapshot_file", "^([a-zA-Z0-9\\:\\_\\-\\\\]){1,255}(\\.xml)"),
NAME("name", "^[a-zA-Z0-9\\_\\-]{3,}$"),
NAME_SPACE("name_space", "^[a-zA-Z0-9\\ \\_\\-]{3,}$"),
PASSWORD("password", "^.*(?=.{12,})(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=!\\~\\|]).*$"),
PASSWORD("password", "^.*(?=.{12,})(?=.*\\p{Digit})(?=.*\\p{Lower})(?=.*\\p{Upper})(?=.*\\p{Punct}).*$"),
EMAIL("email", "^(\\w+)([-+.][\\w]+)*@(\\w[-\\w]*\\.){1,5}([A-Za-z]){2,4}$"),
PHONE("phone",
"^\\+? ?[0-9_]+( [0-9_]+)*$"),
Expand Down
Expand Up @@ -12,6 +12,7 @@
package org.eclipse.kapua.service.authentication.credential.shiro;

import org.apache.shiro.codec.Base64;

import org.eclipse.kapua.KapuaEntityNotFoundException;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.KapuaIllegalArgumentException;
Expand All @@ -20,6 +21,7 @@
import org.eclipse.kapua.commons.model.query.predicate.AndPredicateImpl;
import org.eclipse.kapua.commons.model.query.predicate.AttributePredicateImpl;
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.commons.util.CommonsValidationRegex;
import org.eclipse.kapua.commons.util.KapuaExceptionUtils;
import org.eclipse.kapua.event.ServiceEvent;
import org.eclipse.kapua.locator.KapuaLocator;
Expand All @@ -44,6 +46,7 @@
import org.eclipse.kapua.service.authentication.shiro.setting.KapuaAuthenticationSettingKeys;
import org.eclipse.kapua.service.authorization.AuthorizationService;
import org.eclipse.kapua.service.authorization.permission.PermissionFactory;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -86,6 +89,10 @@ public Credential create(CredentialCreator credentialCreator)
throw new KapuaExistingCredentialException(CredentialType.PASSWORD);
}
}

//
// Validate Password regex
ArgumentValidator.match(credentialCreator.getCredentialPlainKey(), CommonsValidationRegex.PASSWORD_REGEXP, "credentialCreator.credentialKey");
}

//
Expand All @@ -106,35 +113,35 @@ public Credential create(CredentialCreator credentialCreator)
// Do pre persist magic on key values
String fullKey = null;
switch (credentialCreator.getCredentialType()) {
case API_KEY: // Generate new api key
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
case API_KEY: // Generate new api key
SecureRandom random = SecureRandom.getInstance("SHA1PRNG");

KapuaAuthenticationSetting setting = KapuaAuthenticationSetting.getInstance();
int preLength = setting.getInt(KapuaAuthenticationSettingKeys.AUTHENTICATION_CREDENTIAL_APIKEY_PRE_LENGTH);
int keyLength = setting.getInt(KapuaAuthenticationSettingKeys.AUTHENTICATION_CREDENTIAL_APIKEY_KEY_LENGTH);
KapuaAuthenticationSetting setting = KapuaAuthenticationSetting.getInstance();
int preLength = setting.getInt(KapuaAuthenticationSettingKeys.AUTHENTICATION_CREDENTIAL_APIKEY_PRE_LENGTH);
int keyLength = setting.getInt(KapuaAuthenticationSettingKeys.AUTHENTICATION_CREDENTIAL_APIKEY_KEY_LENGTH);

byte[] bPre = new byte[preLength];
random.nextBytes(bPre);
String pre = Base64.encodeToString(bPre).substring(0, preLength);
byte[] bPre = new byte[preLength];
random.nextBytes(bPre);
String pre = Base64.encodeToString(bPre).substring(0, preLength);

byte[] bKey = new byte[keyLength];
random.nextBytes(bKey);
String key = Base64.encodeToString(bKey);
byte[] bKey = new byte[keyLength];
random.nextBytes(bKey);
String key = Base64.encodeToString(bKey);

fullKey = pre + key;
fullKey = pre + key;

credentialCreator = new CredentialCreatorImpl(credentialCreator.getScopeId(),
credentialCreator.getUserId(),
credentialCreator.getCredentialType(),
fullKey,
credentialCreator.getCredentialStatus(),
credentialCreator.getExpirationDate());
credentialCreator = new CredentialCreatorImpl(credentialCreator.getScopeId(),
credentialCreator.getUserId(),
credentialCreator.getCredentialType(),
fullKey,
credentialCreator.getCredentialStatus(),
credentialCreator.getExpirationDate());

break;
case PASSWORD:
default:
// Don't do nothing special
break;
break;
case PASSWORD:
default:
// Don't do nothing special
break;

}

Expand All @@ -146,12 +153,12 @@ public Credential create(CredentialCreator credentialCreator)
//
// Do post persist magic on key values
switch (credentialCreator.getCredentialType()) {
case API_KEY:
credential.setCredentialKey(fullKey);
break;
case PASSWORD:
default:
credential.setCredentialKey(fullKey);
case API_KEY:
credential.setCredentialKey(fullKey);
break;
case PASSWORD:
default:
credential.setCredentialKey(fullKey);
}
} catch (Exception pe) {
em.rollback();
Expand Down

0 comments on commit 36ecd2c

Please sign in to comment.