fork(1) download
  1. package kerberos;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.security.PrivilegedActionException;
  7. import java.security.PrivilegedExceptionAction;
  8.  
  9. import javax.security.auth.Subject;
  10. import javax.security.auth.callback.Callback;
  11. import javax.security.auth.callback.CallbackHandler;
  12. import javax.security.auth.callback.PasswordCallback;
  13. import javax.security.auth.callback.TextOutputCallback;
  14. import javax.security.auth.callback.UnsupportedCallbackException;
  15. import javax.security.auth.login.LoginContext;
  16. import javax.security.auth.login.LoginException;
  17.  
  18. import org.ietf.jgss.GSSCredential;
  19. import org.ietf.jgss.GSSException;
  20. import org.ietf.jgss.GSSManager;
  21. import org.ietf.jgss.Oid;
  22.  
  23. public class KerberosLogin {
  24. public GSSCredential getGSSCredentials(GSSManager mgr, String spn, String keytab, int GSSCredentialType ) throws LoginException, GSSException {
  25. LoginContext lc = new LoginContext("Sample", null, null, new LoginConfiguration(spn, keytab, GSSCredentialType));
  26.  
  27. lc.login();
  28.  
  29. try {
  30. return (GSSCredential) Subject.doAs(lc.getSubject(), new SubjectAction(mgr, GSSCredentialType ));
  31. throw (GSSException) e.getCause();
  32. }
  33. }
  34.  
  35. public GSSCredential getGSSCredentials(GSSManager mgr, String spn, int GSSCredentialType ) throws LoginException, GSSException {
  36. LoginContext lc = new LoginContext("Sample", null, new KerberosCallBackHandler(), new LoginConfiguration(spn, GSSCredentialType));
  37.  
  38. lc.login();
  39.  
  40. try {
  41. return (GSSCredential) Subject.doAs(lc.getSubject(), new SubjectAction(mgr, GSSCredentialType));
  42. throw (GSSException) e.getCause();
  43. }
  44. }
  45.  
  46. // Privileged action which runs as the subject to get the credentials and throws the exception thrown by the run() method
  47. private static final class SubjectAction implements PrivilegedExceptionAction<GSSCredential> {
  48. private GSSManager mgr;
  49. private int GSSCredentialType = GSSCredential.INITIATE_AND_ACCEPT;
  50.  
  51. private static final Oid KRB5_MECH = createOid("1.2.840.113554.1.2.2");
  52.  
  53. private static Oid createOid(String rep) {
  54. try {
  55. return new Oid(rep);
  56. } catch (GSSException e) {
  57. return null;
  58. }
  59. }
  60.  
  61. private SubjectAction(GSSManager mgr, int GSSCredentialType) {
  62. this.mgr = mgr;
  63. this.GSSCredentialType = GSSCredentialType;
  64. }
  65.  
  66. public GSSCredential run() throws GSSException {
  67. return mgr.createCredential(null, GSSCredential.INDEFINITE_LIFETIME, KRB5_MECH, GSSCredentialType );
  68. }
  69. }
  70. }
  71.  
  72. class KerberosCallBackHandler implements CallbackHandler {
  73. public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
  74. for (int i = 0; i < callbacks.length; i++) {
  75. // display the message
  76. if (callbacks[i] instanceof TextOutputCallback) {
  77. TextOutputCallback toc = (TextOutputCallback) callbacks[i];
  78.  
  79. switch (toc.getMessageType()) {
  80. case TextOutputCallback.INFORMATION:
  81. System.out.println(toc.getMessage());
  82. break;
  83. case TextOutputCallback.ERROR:
  84. System.out.println("ERROR: " + toc.getMessage());
  85. break;
  86. case TextOutputCallback.WARNING:
  87. System.out.println("WARNING: " + toc.getMessage());
  88. break;
  89. default:
  90. throw new IOException("Unsupported message type: " + toc.getMessageType());
  91. }
  92. } else if (callbacks[i] instanceof PasswordCallback) {
  93. // prompt the user for password
  94. PasswordCallback pc = (PasswordCallback) callbacks[i];
  95. System.err.print(pc.getPrompt());
  96. System.err.flush();
  97. pc.setPassword(readPassword());
  98. } else {
  99. throw new UnsupportedCallbackException(callbacks[i], "Unrecognized Callback");
  100. }
  101. }
  102. }
  103.  
  104. private char[] readPassword() throws IOException {
  105. return br.readLine().trim().toCharArray();
  106. }
  107. }
  108.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:23: error: class KerberosLogin is public, should be declared in a file named KerberosLogin.java
public class KerberosLogin {
       ^
Main.java:25: error: cannot find symbol
		LoginContext lc = new LoginContext("Sample", null, null, new LoginConfiguration(spn, keytab, GSSCredentialType));
		                                                             ^
  symbol:   class LoginConfiguration
  location: class KerberosLogin
Main.java:37: error: cannot find symbol
		LoginContext lc = new LoginContext("Sample", null, new KerberosCallBackHandler(), new LoginConfiguration(spn, GSSCredentialType));
		                                                                                      ^
  symbol:   class LoginConfiguration
  location: class KerberosLogin
3 errors
stdout
Standard output is empty