1 // Copyright 2006 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
5 package org.ibex.crypto;
6 import javax.security.auth.*;
7 import javax.security.auth.login.*;
8 import javax.security.auth.callback.*;
11 import org.ibex.util.*;
16 * Another big, gross hack.
18 public class KerberosAuth {
20 private final String realm;
21 private final String kdc;
23 /** JAAS doesn't know how to do KDC discovery via DNS */
24 public KerberosAuth(String realm, String kdc) {
29 public boolean auth(final String name, final String pass) {
31 synchronized(KerberosAuth.class) {
32 System.setProperty("java.security.krb5.realm", realm);
33 System.setProperty("java.security.krb5.kdc", kdc);
34 Configuration.setConfiguration(new Configuration() {
35 public AppConfigurationEntry[] getAppConfigurationEntry(String appName) {
36 Map map = new HashMap<String,String>();
37 return new AppConfigurationEntry[] {
38 new AppConfigurationEntry("com.sun.security.auth.module.Krb5LoginModule",
39 AppConfigurationEntry.LoginModuleControlFlag.REQUIRED, map)
42 public void refresh() { }
45 new LoginContext(name,
46 new CallbackHandler() {
47 public void handle(Callback[] callbacks)
48 throws IOException, UnsupportedCallbackException {
49 for (int i = 0; i < callbacks.length; i++) {
50 if (callbacks[i] instanceof TextOutputCallback) {
51 TextOutputCallback toc = (TextOutputCallback)callbacks[i];
52 switch (toc.getMessageType()) {
53 case TextOutputCallback.INFORMATION: break;
54 case TextOutputCallback.ERROR: throw new RuntimeException(toc.getMessage());
55 case TextOutputCallback.WARNING:
56 Log.warn(this, toc.getMessage());
59 throw new RuntimeException("Unsupported message type: " +
60 toc.getMessageType());
63 } else if (callbacks[i] instanceof NameCallback) {
64 NameCallback nc = (NameCallback)callbacks[i];
67 } else if (callbacks[i] instanceof PasswordCallback) {
68 PasswordCallback pc = (PasswordCallback)callbacks[i];
69 pc.setPassword(pass.toCharArray());
72 throw new UnsupportedCallbackException
73 (callbacks[i], "Unrecognized Callback");
79 return lc.getSubject()!=null;
81 } catch (Exception e) {