| 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.io.IOException; |
| 9 |
|
| 10 |
import javax.security.auth.callback.Callback; |
| 11 |
import javax.security.auth.callback.CallbackHandler; |
| 12 |
import javax.security.auth.callback.NameCallback; |
| 13 |
import javax.security.auth.callback.PasswordCallback; |
| 14 |
import javax.security.auth.callback.UnsupportedCallbackException; |
| 15 |
import javax.security.auth.login.LoginContext; |
| 16 |
|
| 17 |
import org.h2.api.CredentialsValidator; |
| 18 |
import org.h2.security.auth.AuthenticationInfo; |
| 19 |
import org.h2.security.auth.ConfigProperties; |
| 20 |
|
| 21 |
/** |
| 22 |
* Validate credentials by using standard Java Authentication and Authorization Service |
| 23 |
* |
| 24 |
* <p> |
| 25 |
* Configuration parameters: |
| 26 |
* </p> |
| 27 |
* <ul> |
| 28 |
* <li>appName inside the JAAS configuration (by default h2)</li> |
| 29 |
* </ul> |
| 30 |
* |
| 31 |
*/ |
| 32 |
public class JaasCredentialsValidator implements CredentialsValidator { |
| 33 |
|
| 34 |
public static final String DEFAULT_APPNAME="h2"; |
| 35 |
|
| 36 |
private String appName; |
| 37 |
|
| 38 |
public JaasCredentialsValidator() { |
| 39 |
this(DEFAULT_APPNAME); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Create the validator with the given name of JAAS configuration |
| 44 |
* @param appName = name of JAAS configuration |
| 45 |
*/ |
| 46 |
public JaasCredentialsValidator(String appName) { |
| 47 |
this.appName=appName; |
| 48 |
} |
| 49 |
|
| 50 |
@Override |
| 51 |
public void configure(ConfigProperties configProperties) { |
| 52 |
appName=configProperties.getStringValue("appName",appName); |
| 53 |
} |
| 54 |
|
| 55 |
static class AuthenticationInfoCallbackHandler implements CallbackHandler { |
| 56 |
|
| 57 |
AuthenticationInfo authenticationInfo; |
| 58 |
|
| 59 |
AuthenticationInfoCallbackHandler(AuthenticationInfo authenticationInfo) { |
| 60 |
this.authenticationInfo = authenticationInfo; |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException { |
| 65 |
for (int i = 0; i < callbacks.length; i++) { |
| 66 |
if (callbacks[i] instanceof NameCallback) { |
| 67 |
((NameCallback) callbacks[i]).setName(authenticationInfo.getUserName()); |
| 68 |
} else if (callbacks[i] instanceof PasswordCallback) { |
| 69 |
((PasswordCallback) callbacks[i]).setPassword(authenticationInfo.getPassword().toCharArray()); |
| 70 |
} |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
} |
| 75 |
|
| 76 |
@Override |
| 77 |
public boolean validateCredentials(AuthenticationInfo authenticationInfo) throws Exception { |
| 78 |
LoginContext loginContext = new LoginContext(appName, |
| 79 |
new AuthenticationInfoCallbackHandler(authenticationInfo)); |
| 80 |
loginContext.login(); |
| 81 |
authenticationInfo.setNestedIdentity(loginContext.getSubject()); |
| 82 |
return true; |
| 83 |
} |
| 84 |
|
| 85 |
} |