| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: Alessandro Ventura |
| 5 |
*/ |
| 6 |
package org.h2.security.auth.impl; |
| 7 |
|
| 8 |
import java.nio.charset.StandardCharsets; |
| 9 |
import java.util.regex.Pattern; |
| 10 |
|
| 11 |
import org.h2.api.CredentialsValidator; |
| 12 |
import org.h2.security.SHA256; |
| 13 |
import org.h2.security.auth.AuthenticationException; |
| 14 |
import org.h2.security.auth.AuthenticationInfo; |
| 15 |
import org.h2.security.auth.ConfigProperties; |
| 16 |
import org.h2.util.MathUtils; |
| 17 |
import org.h2.util.StringUtils; |
| 18 |
import org.h2.util.Utils; |
| 19 |
|
| 20 |
/** |
| 21 |
* This credentials validator matches the user and password with the configured |
| 22 |
* Usage should be limited to test purposes |
| 23 |
* |
| 24 |
*/ |
| 25 |
public class StaticUserCredentialsValidator implements CredentialsValidator { |
| 26 |
|
| 27 |
private Pattern userNamePattern; |
| 28 |
private String password; |
| 29 |
private byte[] salt; |
| 30 |
private byte[] hashWithSalt; |
| 31 |
|
| 32 |
public StaticUserCredentialsValidator() { |
| 33 |
} |
| 34 |
|
| 35 |
public StaticUserCredentialsValidator(String userNamePattern,String password) { |
| 36 |
if (userNamePattern!=null) { |
| 37 |
this.userNamePattern=Pattern.compile(userNamePattern.toUpperCase()); |
| 38 |
} |
| 39 |
salt=MathUtils.secureRandomBytes(256); |
| 40 |
hashWithSalt=SHA256.getHashWithSalt(password.getBytes(StandardCharsets.UTF_8), salt); |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public boolean validateCredentials(AuthenticationInfo authenticationInfo) throws AuthenticationException { |
| 45 |
if (userNamePattern!=null) { |
| 46 |
if (!userNamePattern.matcher(authenticationInfo.getUserName()).matches()) { |
| 47 |
return false; |
| 48 |
} |
| 49 |
} |
| 50 |
if (password!=null) { |
| 51 |
return password.equals(authenticationInfo.getPassword()); |
| 52 |
} |
| 53 |
return Utils.compareSecure(hashWithSalt, |
| 54 |
SHA256.getHashWithSalt(authenticationInfo.getPassword().getBytes(StandardCharsets.UTF_8), salt)); |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public void configure(ConfigProperties configProperties) { |
| 59 |
String userNamePatternString=configProperties.getStringValue("userNamePattern",null); |
| 60 |
if (userNamePatternString!=null) { |
| 61 |
userNamePattern = Pattern.compile(userNamePatternString); |
| 62 |
} |
| 63 |
password=configProperties.getStringValue("password",password); |
| 64 |
String saltString =configProperties.getStringValue("salt",null); |
| 65 |
if (saltString!=null) { |
| 66 |
salt=StringUtils.convertHexToBytes(saltString); |
| 67 |
} |
| 68 |
String hashString=configProperties.getStringValue("hash", null); |
| 69 |
if (hashString!=null) { |
| 70 |
hashWithSalt = SHA256.getHashWithSalt(StringUtils.convertHexToBytes(hashString), salt); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
} |