LoginTicketManager { public LoginTicket generateNewTicket(String requestingApplicationName, String requestingApplicationUsername) throws ExternalAppNotMappedException { if (getTicketValiditySeconds() <= 0) { logger.warn("WARN! ticket validity is <= 0 [value = " + getTicketValiditySeconds() + "]"); } try { if(tickets.size() >= getTicketCountThreashold()) { this.deleteExpired(); } } catch(Exception e) { logger.error("An error has occurred while trying to remove expired tickets", e); } if(!getUsernameProvider().isAppNameMapped(requestingApplicationName)) { throw new ExternalAppNotMappedException(requestingApplicationName); } LoginTicket ticket = LoginTicket.newTicket(getTicketValiditySeconds(), requestingApplicationName, requestingApplicationUsername); synchronized (this.tickets) { tickets.put(ticket.getIdAsString(), ticket); } return ticket; } LoginTicketManager(); int getTicketCountThreashold(); void setTicketCountThreashold(int ticketCountThreashold); int getTicketValiditySeconds(); void setTicketValiditySeconds(int ticketValiditySeconds); UsernameProvider getUsernameProvider(); void setUsernameProvider(UsernameProvider usernameProvider); LoginTicket generateNewTicket(String requestingApplicationName, String requestingApplicationUsername); LoginTicket removeTicket(String ticketId); void deleteExpired(); long getSize(); void resetTickets(); static final int DEFAULT_TICKET_VALIDITY_SECONDS; static final int TICKETS_CLEANING_THRESHOLD; }
@Test public void testGenerateNewTicket() throws LoginTicketNotFoundException, ExternalAppNotMappedException { assertNotNull(manager); LoginTicket ticket = manager.generateNewTicket("test", "testUser"); assertNotNull(ticket); assertFalse(ticket.isExpired()); LoginTicket removed = manager.removeTicket(ticket.getIdAsString()); assertEquals(ticket.getId(), removed.getId()); assertEquals(ticket.getIdAsString(), removed.getIdAsString()); assertEquals(ticket.getCreationTime(), removed.getCreationTime()); assertEquals(ticket.getRequistingApplication(), removed.getRequistingApplication()); assertEquals(ticket.getRequestingApplicationUsername(), removed.getRequestingApplicationUsername()); assertEquals(ticket.getValidityDuration(), removed.getValidityDuration()); assertEquals(0, manager.getSize()); }
@Test(expected = ExternalAppNotMappedException.class) public void testGenerateTicketWithWrongApp() throws ExternalAppNotMappedException { manager.generateNewTicket("IdoNOTexist", "doesntREALLYmatter"); }