import cyrus-sasl-2.1.23
[cyrus-sasl.git] / java / CyrusSasl / Sasl.java
1 package CyrusSasl;
2
3 import java.util.Hashtable;
4 import javax.security.auth.callback.*;
5
6 public class Sasl
7 {
8
9     private static SaslClientFactory client_factory = null;
10     private static SaslServerFactory server_factory = null;
11
12     /*
13    Creates a SaslClient using the parameters supplied. It returns null
14    if no SaslClient can be created using the parameters supplied. Throws
15    SaslException if it cannot create a SaslClient because of an error.
16
17    The algorithm for selection is as follows:
18
19    1. If a factory has been installed via setSaslClientFactory(), try it
20       first. If non-null answer produced, return it.
21    2. Use the packages listed in the javax.security.sasl.client.pkgs
22       property from props to load in a factory and try to create a
23       SaslClient, by looking for a class named ClientFactory. Repeat
24       this for each package on the list until a non-null answer is
25       produced. If non-null answer produced, return it.
26    3. Repeat previous step using the javax.security.sasl.client.pkgs
27       System property.
28    4. If no non-null answer produced, return null.
29
30    Parameters are:
31
32       mechanisms     The non-null list of mechanism names to try. Each
33                      is the IANA-registered name of a SASL mechanism.
34                      (e.g. "GSSAPI", "CRAM-MD5").
35
36
37
38       authorizationID The possibly null protocol-dependent
39                      identification to be used for authorization, e.g.
40                      user name or distinguished name. When the SASL
41                      authentication completes successfully, the entity
42                      named by authorizationId is granted access. If
43                      null, access is granted to a protocol-dependent
44                      default (for example, in LDAP this is the DN in
45                      the bind request).
46
47       protocol       The non-null string name of the protocol for
48                      which the authentication is being performed, e.g
49                      "pop", "ldap".
50
51       serverName     The non-null fully qualified host name of the
52                      server to authenticate to.
53
54       props          The possibly null additional configuration
55                      properties for the session, e.g.
56
57     */
58
59     public static SaslClient
60         createSaslClient(String[] mechanisms,
61                          String authorizationID,
62                          String protocol,
63                          String serverName,
64                          Hashtable props,
65                          javax.security.auth.callback.CallbackHandler cbh)    throws SaslException
66     {
67         if (client_factory == null)
68         {
69             client_factory = new ClientFactory();
70         }
71
72         return client_factory.createSaslClient(mechanisms,
73                                                authorizationID,
74                                                protocol,
75                                                serverName,
76                                                props,
77                                                cbh);
78     }
79
80     public static void setSaslClientFactory(SaslClientFactory fac) {
81         client_factory = fac;
82     }
83
84     public static void setSaslServerFactory(SaslServerFactory fac) {
85         server_factory = fac;
86     }
87
88
89     public static SaslServer CreateSaslServer(String mechanism,
90                                               String protocol,
91                                               String serverName,
92                                               Hashtable props,
93                                               javax.security.auth.callback.CallbackHandler cbh)
94                                               throws SaslException
95     {
96         if (server_factory == null)
97         {
98             server_factory = new ServerFactory();
99         }
100
101         return server_factory.createSaslServer(mechanism,
102                                                protocol,
103                                                serverName,
104                                                props,
105                                                cbh);
106     }
107
108     public static String[] getMechanismNames()
109     {
110         if (server_factory == null)
111         {
112             server_factory = new ServerFactory();
113         }
114
115         return server_factory.getMechanismNames();      
116     }
117
118
119
120
121
122 }