KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > security > auth > impl > JAASAuthenticationService


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.security.auth.impl;
18
19 import java.io.IOException JavaDoc;
20 import java.security.GeneralSecurityException JavaDoc;
21 import java.security.cert.X509Certificate JavaDoc;
22
23 import javax.security.auth.Subject JavaDoc;
24 import javax.security.auth.callback.Callback JavaDoc;
25 import javax.security.auth.callback.CallbackHandler JavaDoc;
26 import javax.security.auth.callback.NameCallback JavaDoc;
27 import javax.security.auth.callback.PasswordCallback JavaDoc;
28 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
29 import javax.security.auth.login.LoginContext JavaDoc;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33 import org.apache.servicemix.jbi.security.auth.AuthenticationService;
34 import org.apache.servicemix.jbi.security.login.CertificateCallback;
35
36 /**
37  * Implementation of the authentication service using JAAS.
38  *
39  * @org.apache.xbean.XBean element="authenticationService"
40  */

41 public class JAASAuthenticationService implements AuthenticationService {
42
43     private static final Log log = LogFactory.getLog(JAASAuthenticationService.class);
44     
45     public void authenticate(Subject JavaDoc subject,
46                              String JavaDoc domain,
47                              final String JavaDoc user,
48                              final Object JavaDoc credentials) throws GeneralSecurityException JavaDoc {
49         if (log.isDebugEnabled()) {
50             log.debug("Authenticating '" + user + "' with '" + credentials + "'");
51         }
52         LoginContext JavaDoc loginContext = new LoginContext JavaDoc(domain, subject, new CallbackHandler JavaDoc() {
53             public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc {
54                 for (int i = 0; i < callbacks.length; i++) {
55                     if (callbacks[i] instanceof NameCallback JavaDoc) {
56                         ((NameCallback JavaDoc) callbacks[i]).setName(user);
57                     } else if (callbacks[i] instanceof PasswordCallback JavaDoc && credentials instanceof String JavaDoc) {
58                         ((PasswordCallback JavaDoc) callbacks[i]).setPassword(((String JavaDoc) credentials).toCharArray());
59                     } else if (callbacks[i] instanceof CertificateCallback && credentials instanceof X509Certificate JavaDoc) {
60                         ((CertificateCallback) callbacks[i]).setCertificate((X509Certificate JavaDoc) credentials);
61                     } else {
62                         throw new UnsupportedCallbackException JavaDoc(callbacks[i]);
63                     }
64                 }
65             }
66         });
67         loginContext.login();
68     }
69
70 }
71
Popular Tags