KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > webservice > axis > TicketCallbackHandler


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.webservice.axis;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.security.auth.callback.Callback JavaDoc;
22 import javax.security.auth.callback.CallbackHandler JavaDoc;
23 import javax.security.auth.callback.UnsupportedCallbackException JavaDoc;
24
25 import org.alfresco.repo.security.authentication.AuthenticationException;
26 import org.alfresco.repo.webservice.authentication.AuthenticationFault;
27 import org.alfresco.service.cmr.security.AuthenticationService;
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.ws.security.WSPasswordCallback;
31
32 /**
33  * CallbackHandler that verifies the given ticket in the password element of the UsernameToken
34  * header is still a valid ticket
35  *
36  * @author gavinc
37  */

38 public class TicketCallbackHandler implements CallbackHandler JavaDoc
39 {
40    private static final Log logger = LogFactory.getLog(TicketCallbackHandler.class);
41       
42    private AuthenticationService authenticationService;
43    
44    /**
45     * Sets the AuthenticationService instance to use
46     *
47     * @param authenticationService The AuthenticationService
48     */

49    public void setAuthenticationService(AuthenticationService authenticationService)
50    {
51       this.authenticationService = authenticationService;
52    }
53
54    /**
55     * @see javax.security.auth.callback.CallbackHandler#handle(javax.security.auth.callback.Callback[])
56     */

57    public void handle(Callback JavaDoc[] callbacks) throws IOException JavaDoc, UnsupportedCallbackException JavaDoc
58    {
59       for (int i = 0; i < callbacks.length; i++)
60       {
61          if (callbacks[i] instanceof WSPasswordCallback)
62          {
63             WSPasswordCallback pc = (WSPasswordCallback)callbacks[i];
64             String JavaDoc ticket = pc.getPassword();
65             
66             if (logger.isDebugEnabled())
67             {
68                logger.debug("Verifying ticket for: " + pc.getIdentifer());
69                logger.debug("Ticket: " + ticket);
70             }
71
72             // ensure the ticket is valid
73
try
74             {
75                this.authenticationService.validate(ticket);
76             }
77             catch (AuthenticationException ae)
78             {
79                if (logger.isDebugEnabled())
80                   logger.debug("Ticket validation failed: " + ae.getMessage());
81                
82                // NOTE: Throwing AuthenticationFault just gets consumed and the ws-security handler
83
// reports a missing password; we would need to modify the WSS4J code to let
84
// the exception bubble up so for now just let the default message get thrown
85
throw new AuthenticationFault(701, "Authentication failed due to an invalid ticket");
86             }
87             
88             if (logger.isDebugEnabled())
89                logger.debug("Ticket validated successfully");
90             
91             // if all is well set the password to return as the given ticket
92
pc.setPassword(pc.getPassword());
93          }
94          else
95          {
96             throw new UnsupportedCallbackException JavaDoc(callbacks[i], "Unrecognized Callback");
97          }
98       }
99    }
100 }
101
Popular Tags