Skip to content

Instantly share code, notes, and snippets.

@mainframed
Created January 27, 2026 10:09
Show Gist options
  • Select an option

  • Save mainframed/f09efbba20794359d939db8c52916ac4 to your computer and use it in GitHub Desktop.

Select an option

Save mainframed/f09efbba20794359d939db8c52916ac4 to your computer and use it in GitHub Desktop.
LTAP Token Generator
import java.io.FileInputStream;
import java.util.Properties;
import javax.security.auth.Subject;
import java.security.Principal;
import com.ibm.websphere.security.ltpa.LTPAToken2;
import com.ibm.websphere.security.ltpa.LTPATokenFactory;
public class LtpaTokenGenerator {
public static void main(String[] args) throws Exception {
if (args.length < 4) {
System.err.println(
"Usage: java LtpaTokenGenerator <ltpa.keys> <password> <username> <realm>"
);
System.exit(1);
}
String keyPath = args[0];
String password = args[1];
String username = args[2];
String realm = args[3];
// Load ltpa.keys
Properties ltpaProps = new Properties();
try (FileInputStream fis = new FileInputStream(keyPath)) {
ltpaProps.load(fis);
}
// Create Subject
Subject subject = new Subject();
subject.getPrincipals().add(new SimplePrincipal(username));
// Get LTPA factory
LTPATokenFactory factory = LTPATokenFactory.getInstance();
// Generate LTPA2 token
LTPAToken2 token = factory.createLTPAToken2(
subject,
realm,
ltpaProps,
password
);
if (token == null) {
throw new RuntimeException("Token generation failed");
}
System.out.println("LtpaToken2=" + token.getEncodedToken());
}
// Minimal Principal implementation
static class SimplePrincipal implements Principal {
private final String name;
SimplePrincipal(String name) { this.name = name; }
public String getName() { return name; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment