KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > security > authentication > InMemoryTicketComponentImpl


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.security.authentication;
18
19 import java.util.Date JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Set JavaDoc;
23
24 import org.alfresco.service.cmr.repository.datatype.Duration;
25 import org.alfresco.util.GUID;
26 public class InMemoryTicketComponentImpl implements TicketComponent
27 {
28     public static final String JavaDoc GRANTED_AUTHORITY_TICKET_PREFIX = "TICKET_";
29
30     private boolean ticketsExpire;
31
32     private Duration validDuration;
33     
34     private boolean oneOff;
35
36     private HashMap JavaDoc<String JavaDoc, Ticket> tickets = new HashMap JavaDoc<String JavaDoc, Ticket>();
37
38     public InMemoryTicketComponentImpl()
39     {
40         super();
41     }
42
43     public String JavaDoc getTicket(String JavaDoc userName) throws AuthenticationException
44     {
45             Date JavaDoc expiryDate = null;
46             if (ticketsExpire)
47             {
48                 expiryDate = Duration.add(new Date JavaDoc(), validDuration);
49             }
50             Ticket ticket = new Ticket(ticketsExpire, expiryDate, userName);
51             tickets.put(ticket.getTicketId(), ticket);
52        
53             return GRANTED_AUTHORITY_TICKET_PREFIX + ticket.getTicketId();
54     }
55
56     public String JavaDoc validateTicket(String JavaDoc ticketString) throws AuthenticationException
57     {
58         if (ticketString.length() < GRANTED_AUTHORITY_TICKET_PREFIX.length())
59         {
60            throw new AuthenticationException(ticketString + " is an invalid ticket format");
61         }
62         
63         String JavaDoc key = ticketString.substring(GRANTED_AUTHORITY_TICKET_PREFIX.length());
64         Ticket ticket = tickets.get(key);
65         if (ticket == null)
66         {
67             throw new AuthenticationException("Missing ticket for " + ticketString);
68         }
69         if (ticket.hasExpired())
70         {
71             throw new TicketExpiredException("Ticket expired for " + ticketString);
72         }
73         // TODO: Recheck the user details here
74
// TODO: Strengthen ticket as GUID is predicatble
75
if(oneOff)
76         {
77             tickets.remove(key);
78         }
79         return ticket.getUserName();
80     }
81     
82     public void invalidateTicketById(String JavaDoc ticketString)
83     {
84         String JavaDoc key = ticketString.substring(GRANTED_AUTHORITY_TICKET_PREFIX.length());
85         tickets.remove(key);
86     }
87     
88     public void invalidateTicketByUser(String JavaDoc userName)
89     {
90         Set JavaDoc<String JavaDoc> toRemove = new HashSet JavaDoc<String JavaDoc>();
91         
92         for(String JavaDoc key: tickets.keySet())
93         {
94             Ticket ticket = tickets.get(key);
95             if(ticket.getUserName().equals(userName))
96             {
97                 toRemove.add(ticket.getTicketId());
98             }
99         }
100         
101         for(String JavaDoc id: toRemove)
102         {
103             tickets.remove(id);
104         }
105     }
106     
107     
108     
109     private static class Ticket
110     {
111         private boolean expires;
112
113         private Date JavaDoc expiryDate;
114
115         private String JavaDoc userName;
116
117         private String JavaDoc ticketId;
118
119         Ticket(boolean expires, Date JavaDoc expiryDate, String JavaDoc userName)
120         {
121             this.expires = expires;
122             this.expiryDate = expiryDate;
123             this.userName = userName;
124             this.ticketId = GUID.generate();
125         }
126
127         /**
128          * Has the tick expired
129          *
130          * @return
131          */

132         boolean hasExpired()
133         {
134             if (expires && (expiryDate != null) && (expiryDate.compareTo(new Date JavaDoc()) < 0))
135             {
136                 return true;
137             }
138             else
139             {
140                 return false;
141             }
142         }
143
144         public boolean equals(Object JavaDoc o)
145         {
146             if (o == this)
147             {
148                 return true;
149             }
150             if (!(o instanceof Ticket))
151             {
152                 return false;
153             }
154             Ticket t = (Ticket) o;
155             return (this.expires == t.expires) && this.expiryDate.equals(t.expiryDate) && this.userName.equals(t.userName) && this.ticketId.equals(t.ticketId);
156         }
157
158         public int hashCode()
159         {
160             return ticketId.hashCode();
161         }
162
163         protected boolean getExpires()
164         {
165             return expires;
166         }
167
168         protected Date JavaDoc getExpiryDate()
169         {
170             return expiryDate;
171         }
172
173         protected String JavaDoc getTicketId()
174         {
175             return ticketId;
176         }
177
178         protected String JavaDoc getUserName()
179         {
180             return userName;
181         }
182
183     }
184
185
186
187     public void setOneOff(boolean oneOff)
188     {
189         this.oneOff = oneOff;
190     }
191     
192
193     public void setTicketsExpire(boolean ticketsExpire)
194     {
195         this.ticketsExpire = ticketsExpire;
196     }
197     
198
199     public void setValidDuration(String JavaDoc validDuration)
200     {
201         this.validDuration = new Duration(validDuration);
202     }
203     
204 }
205
Popular Tags