Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-factor authentication support #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/main/java/org/jvnet/libpam/PAM.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public class PAM {
private int ret;

/**
* Temporarily stored to pass a value from {@link #authenticate(String, String)}
* Temporarily stored to pass a value from {@link #authenticate(String, String...)}
* to {@link pam_conv}.
*/
private String password;
private String[] factors;

/**
* Creates a new authenticator.
Expand All @@ -73,19 +73,19 @@ public PAM(String serviceName) throws PAMException {
pam_conv conv = new pam_conv(new PamCallback() {
public int callback(int num_msg, Pointer msg, Pointer resp, Pointer _) {
LOGGER.fine("pam_conv num_msg="+num_msg);
if(password==null)
if(factors==null)
return PAM_CONV_ERR;

// allocates pam_response[num_msg]. the caller will free this
Pointer m = libc.calloc(pam_response.SIZE,num_msg);
resp.setPointer(0,m);

for( int i=0; i<num_msg; i++ ) {
for( int i=0; i<factors.length; i++ ) {
pam_message pm = new pam_message(msg.getPointer(POINTER_SIZE*i));
LOGGER.fine(pm.msg_style+":"+pm.msg);
if(pm.msg_style==PAM_PROMPT_ECHO_OFF) {
pam_response r = new pam_response(m.share(pam_response.SIZE*i));
r.setResp(password);
if (pm.msg_style == PAM_PROMPT_ECHO_OFF) {
pam_response r = new pam_response(m.share(pam_response.SIZE * i));
r.setResp(factors[i]);
r.write(); // write to (*resp)[i]
}
}
Expand Down Expand Up @@ -117,8 +117,8 @@ private void check(int ret, String msg) throws PAMException {
* @throws PAMException
* If the authentication fails.
*/
public UnixUser authenticate(String username, String password) throws PAMException {
this.password = password;
public UnixUser authenticate(String username, String... factors) throws PAMException {
this.factors = factors;
try {
check(libpam.pam_set_item(pht,PAM_USER,username),"pam_set_item failed");
check(libpam.pam_authenticate(pht,0),"pam_authenticate failed");
Expand All @@ -134,7 +134,7 @@ public UnixUser authenticate(String username, String password) throws PAMExcepti
throw new PAMException("Authentication succeeded but no user information is available");
return new UnixUser(userName,pwd);
} finally {
this.password = null;
this.factors = null;
}
}

Expand Down Expand Up @@ -180,4 +180,4 @@ protected void finalize() throws Throwable {
}

private static final Logger LOGGER = Logger.getLogger(PAM.class.getName());
}
}