Skip to content

Commit

Permalink
added factories for ST and PT objects
Browse files Browse the repository at this point in the history
  • Loading branch information
SavvasMisaghMoayyed committed Nov 29, 2015
1 parent f18d257 commit 6a5bb0a
Show file tree
Hide file tree
Showing 31 changed files with 340 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.jasig.cas.authentication.Authentication;
import org.jasig.cas.authentication.principal.Service;
import org.jasig.cas.ticket.proxy.ProxyGrantingTicket;

/**
* Interface for a Service Ticket. A service ticket is used to grant access to a
Expand Down Expand Up @@ -51,5 +52,5 @@ public interface ServiceTicket extends Ticket {
* @since 4.2
*/
ProxyGrantingTicket grantProxyGrantingTicket(String id,
Authentication authentication, ExpirationPolicy expirationPolicy);
Authentication authentication, ExpirationPolicy expirationPolicy);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,24 @@
import org.jasig.cas.authentication.principal.Service;

/**
* The {@link TicketGrantingTicketFactory} is responsible for
* creating instances of {@link TicketGrantingTicket}.
* The {@link ServiceTicketFactory} is responsible for
* creating instances of {@link ServiceTicket}.
*
* @author Misagh Moayyed
* @param <T> the type parameter
* @since 4.2
*/
public interface ServiceTicketFactory<T extends Ticket> extends TicketFactory {
public interface ServiceTicketFactory extends TicketFactory {

/**
* Create the ticket object.
*
* @param <T> the type parameter
* @param service the service
* @param expirationPolicy the expiration policy
* @param credentialsProvided the credentials provided
* @param onlyTrackMostRecentSession the only track most recent session
* @param <T> the type parameter
* @param ticketGrantingTicket the ticket granting ticket
* @param service the service
* @param credentialsProvided the credentials provided
* @return the t
*/
<T extends Ticket> T create(Service service,
ExpirationPolicy expirationPolicy,
boolean credentialsProvided,
boolean onlyTrackMostRecentSession);
<T extends Ticket> T create(TicketGrantingTicket ticketGrantingTicket,
Service service,
boolean credentialsProvided);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jasig.cas.ticket;
package org.jasig.cas.ticket.proxy;

import org.jasig.cas.authentication.principal.Service;
import org.jasig.cas.ticket.ExpirationPolicy;
import org.jasig.cas.ticket.TicketGrantingTicket;

/**
* Interface for a proxy granting ticket. A proxy-granting ticket is an opaque string that is
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jasig.cas.ticket;
package org.jasig.cas.ticket.proxy;

import org.jasig.cas.authentication.Authentication;
import org.jasig.cas.ticket.ServiceTicket;
import org.jasig.cas.ticket.TicketFactory;

/**
* The {@link ProxyGrantingTicketFactory} is responsible for
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package org.jasig.cas.ticket;
package org.jasig.cas.ticket.proxy;

import org.jasig.cas.ticket.ServiceTicket;

/**
* The {@link ProxyTicket} represents a CAS proxy ticket. A proxy ticket is an opaque string that a
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.jasig.cas.ticket.proxy;

import org.jasig.cas.authentication.principal.Service;
import org.jasig.cas.ticket.Ticket;
import org.jasig.cas.ticket.TicketFactory;

/**
* The {@link ProxyTicketFactory} is responsible for
* creating instances of {@link ProxyTicket}.
*
* @author Misagh Moayyed
* @since 4.2
*/
public interface ProxyTicketFactory extends TicketFactory {

/**
* Create the ticket object.
*
* @param <T> the type parameter
* @param ticketGrantingTicket the ticket granting ticket
* @param service the service
* @return the t
*/
<T extends Ticket> T create(ProxyGrantingTicket ticketGrantingTicket,
Service service);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import org.jasig.cas.logout.LogoutRequest;
import org.jasig.cas.ticket.AbstractTicketException;
import org.jasig.cas.ticket.InvalidTicketException;
import org.jasig.cas.ticket.ProxyGrantingTicket;
import org.jasig.cas.ticket.ProxyTicket;
import org.jasig.cas.ticket.proxy.ProxyGrantingTicket;
import org.jasig.cas.ticket.proxy.ProxyTicket;
import org.jasig.cas.ticket.ServiceTicket;
import org.jasig.cas.ticket.Ticket;
import org.jasig.cas.ticket.TicketGrantingTicket;
Expand Down Expand Up @@ -118,7 +118,7 @@ ServiceTicket grantServiceTicket(
throws AuthenticationException, AbstractTicketException;

/**
* Grant a {@link org.jasig.cas.ticket.ProxyTicket} that may be used to access the given service
* Grant a {@link ProxyTicket} that may be used to access the given service
* by authenticating the given credentials.
* The details of the security policy around credential authentication and the definition
* of authentication success are dependent on the implementation, but it SHOULD be safe to assume
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.jasig.cas.support.events;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.jasig.cas.ticket.ProxyGrantingTicket;
import org.jasig.cas.ticket.ProxyTicket;
import org.jasig.cas.ticket.proxy.ProxyGrantingTicket;
import org.jasig.cas.ticket.proxy.ProxyTicket;

/**
* Concrete subclass of {@code AbstractCasEvent} representing granting of a
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.slf4j.impl;

import org.apache.commons.lang3.StringUtils;
import org.jasig.cas.ticket.ProxyGrantingTicket;
import org.jasig.cas.ticket.proxy.ProxyGrantingTicket;
import org.jasig.cas.ticket.TicketGrantingTicket;
import org.slf4j.Logger;
import org.slf4j.Marker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.jasig.cas.ticket.InvalidTicketException;
import org.jasig.cas.ticket.ProxyGrantingTicket;
import org.jasig.cas.ticket.proxy.ProxyGrantingTicket;
import org.jasig.cas.ticket.TicketGrantingTicket;
import org.junit.After;
import org.junit.Before;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.jasig.cas.ticket;

import org.jasig.cas.authentication.Authentication;
import org.jasig.cas.ticket.proxy.ProxyGrantingTicket;
import org.jasig.cas.ticket.proxy.ProxyGrantingTicketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.jasig.cas.ticket;

import org.jasig.cas.authentication.principal.Service;
import org.jasig.cas.ticket.proxy.ProxyGrantingTicket;
import org.jasig.cas.ticket.proxy.ProxyTicket;
import org.jasig.cas.ticket.proxy.ProxyTicketFactory;
import org.jasig.cas.util.DefaultUniqueTicketIdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
* The {@link DefaultProxyTicketFactory} is responsible for
* creating {@link ProxyTicket} objects.
*
* @author Misagh Moayyed
* @since 4.2
*/
@Component("defaultProxyTicketFactory")
public class DefaultProxyTicketFactory implements ProxyTicketFactory {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());

/** Default instance for the ticket id generator. */
@NotNull
protected final UniqueTicketIdGenerator defaultServiceTicketIdGenerator = new DefaultUniqueTicketIdGenerator();

/** Map to contain the mappings of service to {@link UniqueTicketIdGenerator}s. */
@NotNull
@Resource(name="uniqueIdGeneratorsMap")
protected Map<String, UniqueTicketIdGenerator> uniqueTicketIdGeneratorsForService;

/** Whether we should track the most recent session by keeping the latest service ticket. */
@Value("${tgt.onlyTrackMostRecentSession:true}")
protected boolean onlyTrackMostRecentSession = true;

/** ExpirationPolicy for Service Tickets. */
@NotNull
@Resource(name="serviceTicketExpirationPolicy")
protected ExpirationPolicy serviceTicketExpirationPolicy;

@Override
public <T extends Ticket> T create(final ProxyGrantingTicket proxyGrantingTicket,
final Service service) {
final String uniqueTicketIdGenKey = service.getClass().getName();
logger.debug("Looking up service ticket id generator for [{}]", uniqueTicketIdGenKey);
UniqueTicketIdGenerator serviceTicketUniqueTicketIdGenerator =
this.uniqueTicketIdGeneratorsForService.get(uniqueTicketIdGenKey);
if (serviceTicketUniqueTicketIdGenerator == null) {
serviceTicketUniqueTicketIdGenerator = this.defaultServiceTicketIdGenerator;
logger.debug("Service ticket id generator not found for [{}]. Using the default generator...",
uniqueTicketIdGenKey);
}

final String ticketId = serviceTicketUniqueTicketIdGenerator.getNewTicketId(ProxyTicket.PROXY_TICKET_PREFIX);
final ProxyTicket serviceTicket = proxyGrantingTicket.grantProxyTicket(
ticketId,
service,
this.serviceTicketExpirationPolicy,
this.onlyTrackMostRecentSession);
return (T) serviceTicket;
}

@Override
public <T extends TicketFactory> T get(final Class<? extends Ticket> clazz) {
return (T) this;
}

public final boolean isOnlyTrackMostRecentSession() {
return onlyTrackMostRecentSession;
}

public final void setOnlyTrackMostRecentSession(final boolean onlyTrackMostRecentSession) {
this.onlyTrackMostRecentSession = onlyTrackMostRecentSession;
}

public void setUniqueTicketIdGeneratorsForService(final Map<String, UniqueTicketIdGenerator> uniqueTicketIdGeneratorsForService) {
this.uniqueTicketIdGeneratorsForService = uniqueTicketIdGeneratorsForService;
}

public void setServiceTicketExpirationPolicy(final ExpirationPolicy serviceTicketExpirationPolicy) {
this.serviceTicketExpirationPolicy = serviceTicketExpirationPolicy;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package org.jasig.cas.ticket;

import org.jasig.cas.authentication.principal.Service;
import org.jasig.cas.util.DefaultUniqueTicketIdGenerator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.Map;

/**
* The {@link DefaultServiceTicketFactory} is responsible for
* creating {@link ServiceTicket} objects.
*
* @author Misagh Moayyed
* @since 4.2
*/
@Component("defaultServiceTicketFactory")
public class DefaultServiceTicketFactory implements ServiceTicketFactory {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());

/** Default instance for the ticket id generator. */
@NotNull
protected UniqueTicketIdGenerator defaultServiceTicketIdGenerator = new DefaultUniqueTicketIdGenerator();


/** Map to contain the mappings of service to {@link UniqueTicketIdGenerator}s. */
@NotNull
@Resource(name="uniqueIdGeneratorsMap")
protected Map<String, UniqueTicketIdGenerator> uniqueTicketIdGeneratorsForService;

/** Whether we should track the most recent session by keeping the latest service ticket. */
@Value("${tgt.onlyTrackMostRecentSession:true}")
protected boolean onlyTrackMostRecentSession = true;

/** ExpirationPolicy for Service Tickets. */
@NotNull
@Resource(name="serviceTicketExpirationPolicy")
protected ExpirationPolicy serviceTicketExpirationPolicy;

@Override
public <T extends Ticket> T create(final TicketGrantingTicket ticketGrantingTicket,
final Service service,
final boolean credentialsProvided) {

final String uniqueTicketIdGenKey = service.getClass().getName();
UniqueTicketIdGenerator serviceTicketUniqueTicketIdGenerator = null;
if (this.uniqueTicketIdGeneratorsForService != null && !uniqueTicketIdGeneratorsForService.isEmpty()) {
logger.debug("Looking up service ticket id generator for [{}]", uniqueTicketIdGenKey);
serviceTicketUniqueTicketIdGenerator = this.uniqueTicketIdGeneratorsForService.get(uniqueTicketIdGenKey);
}
if (serviceTicketUniqueTicketIdGenerator == null) {
serviceTicketUniqueTicketIdGenerator = this.defaultServiceTicketIdGenerator;
logger.debug("Service ticket id generator not found for [{}]. Using the default generator...",
uniqueTicketIdGenKey);
}

final String ticketId = serviceTicketUniqueTicketIdGenerator.getNewTicketId(ServiceTicket.PREFIX);
final ServiceTicket serviceTicket = ticketGrantingTicket.grantServiceTicket(
ticketId,
service,
this.serviceTicketExpirationPolicy,
credentialsProvided,
this.onlyTrackMostRecentSession);
return (T) serviceTicket;
}

@Override
public <T extends TicketFactory> T get(final Class<? extends Ticket> clazz) {
return (T) this;
}

public final boolean isOnlyTrackMostRecentSession() {
return onlyTrackMostRecentSession;
}

public final void setOnlyTrackMostRecentSession(final boolean onlyTrackMostRecentSession) {
this.onlyTrackMostRecentSession = onlyTrackMostRecentSession;
}

public void setUniqueTicketIdGeneratorsForService(final Map<String, UniqueTicketIdGenerator> uniqueTicketIdGeneratorsForService) {
this.uniqueTicketIdGeneratorsForService = uniqueTicketIdGeneratorsForService;
}

public void setServiceTicketExpirationPolicy(final ExpirationPolicy serviceTicketExpirationPolicy) {
this.serviceTicketExpirationPolicy = serviceTicketExpirationPolicy;
}

public void setDefaultServiceTicketIdGenerator(final UniqueTicketIdGenerator defaultServiceTicketIdGenerator) {
this.defaultServiceTicketIdGenerator = defaultServiceTicketIdGenerator;
}
}
Loading

0 comments on commit 6a5bb0a

Please sign in to comment.