1 17 package org.alfresco.repo.security.authentication; 18 19 import java.util.Date ; 20 import java.util.HashMap ; 21 import java.util.HashSet ; 22 import java.util.Set ; 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 GRANTED_AUTHORITY_TICKET_PREFIX = "TICKET_"; 29 30 private boolean ticketsExpire; 31 32 private Duration validDuration; 33 34 private boolean oneOff; 35 36 private HashMap <String , Ticket> tickets = new HashMap <String , Ticket>(); 37 38 public InMemoryTicketComponentImpl() 39 { 40 super(); 41 } 42 43 public String getTicket(String userName) throws AuthenticationException 44 { 45 Date expiryDate = null; 46 if (ticketsExpire) 47 { 48 expiryDate = Duration.add(new Date (), 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 validateTicket(String 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 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 if(oneOff) 76 { 77 tickets.remove(key); 78 } 79 return ticket.getUserName(); 80 } 81 82 public void invalidateTicketById(String ticketString) 83 { 84 String key = ticketString.substring(GRANTED_AUTHORITY_TICKET_PREFIX.length()); 85 tickets.remove(key); 86 } 87 88 public void invalidateTicketByUser(String userName) 89 { 90 Set <String > toRemove = new HashSet <String >(); 91 92 for(String 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 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 expiryDate; 114 115 private String userName; 116 117 private String ticketId; 118 119 Ticket(boolean expires, Date expiryDate, String userName) 120 { 121 this.expires = expires; 122 this.expiryDate = expiryDate; 123 this.userName = userName; 124 this.ticketId = GUID.generate(); 125 } 126 127 132 boolean hasExpired() 133 { 134 if (expires && (expiryDate != null) && (expiryDate.compareTo(new Date ()) < 0)) 135 { 136 return true; 137 } 138 else 139 { 140 return false; 141 } 142 } 143 144 public boolean equals(Object 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 getExpiryDate() 169 { 170 return expiryDate; 171 } 172 173 protected String getTicketId() 174 { 175 return ticketId; 176 } 177 178 protected String 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 validDuration) 200 { 201 this.validDuration = new Duration(validDuration); 202 } 203 204 } 205 | Popular Tags |