-
Notifications
You must be signed in to change notification settings - Fork 2
/
JaasAuth.java
55 lines (50 loc) · 2.06 KB
/
JaasAuth.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.att.research.RCloud;
// Verify user credentials using JAAS and PAM
import java.io.IOException;
import javax.security.auth.callback.Callback;
import javax.security.auth.callback.CallbackHandler;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.LoginContext;
import javax.security.auth.login.LoginException;
/**
* This JaasAcn application attempts to authenticate a user and reports whether
* or not the authentication was successful.
*/
public class JaasAuth {
public static boolean jaasLogin(String user, char[] pass, String jaasModule) {
LoginContext lc = null;
try {
lc = new LoginContext(jaasModule,
new UserNamePasswordCallbackHandler(user, pass));
lc.login();
} catch (LoginException le) {
System.out.println("jaasLogin: FAILED, LoginException: " + le.getMessage());
return false;
} catch (SecurityException se) {
System.out.println("jaasLogin: FAILED, SecurityException: " + se.getMessage());
return false;
}
return true;
}
public static class UserNamePasswordCallbackHandler implements CallbackHandler {
private String _userName;
private char[] _password;
public UserNamePasswordCallbackHandler(String userName, char[] password) {
_userName = userName;
_password = password;
}
public void handle(Callback[] callbacks) throws IOException,
UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback && _userName != null) {
((NameCallback) callback).setName(_userName);
} else if (callback instanceof PasswordCallback
&& _password != null) {
((PasswordCallback) callback).setPassword(_password);
}
}
}
}
}