Skip to content

Commit

Permalink
API updates
Browse files Browse the repository at this point in the history
  • Loading branch information
SavvasMisaghMoayyed committed Dec 20, 2015
1 parent a013bb8 commit 3a7a161
Show file tree
Hide file tree
Showing 37 changed files with 292 additions and 382 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ public interface AuthenticationManager {
* successfully authenticated. Failure to authenticate is considered an exceptional case, and
* an AuthenticationException is thrown.
*
* @param credentials One or more credentials to authenticate.
* @param authenticationTransaction Process a single authentication transaction
*
* @return Authentication object on success that contains metadata about credentials that were authenticated.
*
* @throws AuthenticationException On authentication failure. The exception contains details
* on each of the credentials that failed to authenticate.
*/
Authentication authenticate(Credential... credentials) throws AuthenticationException;
Authentication authenticate(AuthenticationTransaction authenticationTransaction) throws AuthenticationException;
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.jasig.cas.authentication;

/**
* This is {@link AuthenticationObjectsRepository}, that holds the authentication machinery objects.
* This is {@link AuthenticationSystemSupport}, that holds the authentication machinery objects.
* This component is to be injected into others where access to authentication object is required, and
* simply serves as a holder.
*
* @author Misagh Moayyed
* @since 4.2.0
*/
public interface AuthenticationObjectsRepository {
public interface AuthenticationSystemSupport {

/**
* Gets authentication transaction manager.
Expand All @@ -17,13 +17,6 @@ public interface AuthenticationObjectsRepository {
*/
AuthenticationTransactionManager getAuthenticationTransactionManager();

/**
* Gets authentication transaction factory.
*
* @return the authentication transaction factory
*/
AuthenticationTransactionFactory getAuthenticationTransactionFactory();

/**
* Gets principal election strategy.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,61 @@
package org.jasig.cas.authentication;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

/**
* This is {@link AuthenticationTransaction}.
*
* @author Misagh Moayyed
* @since 4.2.0
*/
public interface AuthenticationTransaction {
public final class AuthenticationTransaction implements Serializable {

private static final long serialVersionUID = 6213904009424725484L;

private final Collection<Credential> credentials;

/**
* Gets credentials.
* Instantiates a new Default authentication transaction.
*
* @return the credentials
* @param credentials the credentials
*/
Collection<Credential> getCredentials();
private AuthenticationTransaction(final Collection<Credential> credentials) {
this.credentials = credentials;
}

public Collection<Credential> getCredentials() {
return this.credentials;
}

/**
* Wrap credentials into an authentication transaction, as a factory method,
* and return the final result.
*
* @param credentials the credentials
* @return the authentication transaction
*/
public static AuthenticationTransaction wrap(final Credential... credentials) {
return new AuthenticationTransaction(sanitizeCredentials(credentials));
}

private static Set<Credential> sanitizeCredentials(final Credential[] credentials) {
if (credentials != null && credentials.length > 0) {
final Set<Credential> set = new HashSet<>(Arrays.asList(credentials));
final Iterator<Credential> it = set.iterator();
while (it.hasNext()) {
if (it.next() == null) {
it.remove();
}
}
return set;
}
return Collections.emptySet();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@ public void verifyResolverCredential() {
@Test
public void verifyResolverServiceTicket() throws Exception {
final Credential c = TestUtils.getCredentialsWithSameUsernameAndPassword();
final AuthenticationContext ctx = getAuthenticationContext(c);
final AuthenticationContext ctx = TestUtils.getAuthenticationContext(getAuthenticationSystemSupport(), c);

final TicketGrantingTicket ticketId = getCentralAuthenticationService()
.createTicketGrantingTicket(ctx);
final ServiceTicket st = getCentralAuthenticationService().grantServiceTicket(ticketId.getId(),
TestUtils.getService(), ctx);

final TicketOrCredentialPrincipalResolver res =
new TicketOrCredentialPrincipalResolver(getCentralAuthenticationService());
final TicketOrCredentialPrincipalResolver res = new TicketOrCredentialPrincipalResolver(getCentralAuthenticationService());
final JoinPoint jp = mock(JoinPoint.class);

when(jp.getArgs()).thenReturn(new Object[] {st.getId()});
Expand All @@ -69,15 +68,12 @@ public void verifyResolverServiceTicket() throws Exception {
@Test
public void verifyResolverTicketGrantingTicket() throws Exception {
final Credential c = TestUtils.getCredentialsWithSameUsernameAndPassword();
final AuthenticationContext ctx = getAuthenticationContext(c);
final AuthenticationContext ctx = TestUtils.getAuthenticationContext(getAuthenticationSystemSupport(), c);

final TicketGrantingTicket ticketId = getCentralAuthenticationService()
.createTicketGrantingTicket(ctx);
final ServiceTicket st = getCentralAuthenticationService().grantServiceTicket(ticketId.getId(),
TestUtils.getService(), ctx);
final TicketGrantingTicket ticketId = getCentralAuthenticationService().createTicketGrantingTicket(ctx);
final ServiceTicket st = getCentralAuthenticationService().grantServiceTicket(ticketId.getId(), TestUtils.getService(), ctx);

final TicketOrCredentialPrincipalResolver res =
new TicketOrCredentialPrincipalResolver(getCentralAuthenticationService());
final TicketOrCredentialPrincipalResolver res = new TicketOrCredentialPrincipalResolver(getCentralAuthenticationService());
final JoinPoint jp = mock(JoinPoint.class);

when(jp.getArgs()).thenReturn(new Object[] {ticketId.getId()});
Expand All @@ -87,16 +83,5 @@ public void verifyResolverTicketGrantingTicket() throws Exception {
assertEquals(result, c.getId());
}

private AuthenticationContext getAuthenticationContext(final Credential... credentials)
throws AuthenticationException {
final AuthenticationContextBuilder builder = new DefaultAuthenticationContextBuilder(
getAuthenticationObjectsRepository().getPrincipalElectionStrategy());
final AuthenticationTransaction transaction =
getAuthenticationObjectsRepository().getAuthenticationTransactionFactory()
.get(org.jasig.cas.authentication.TestUtils.getCredentialsWithSameUsernameAndPassword());
getAuthenticationObjectsRepository().getAuthenticationTransactionManager()
.handle(transaction, builder);
final AuthenticationContext ctx = builder.build(TestUtils.getService());
return ctx;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,13 @@
import org.springframework.stereotype.Component;

/**
* This is {@link DefaultAuthenticationObjectsRepository}.
* This is {@link DefaultAuthenticationSystemSupport}.
*
* @author Misagh Moayyed
* @since 4.2.0
*/
@Component("defaultAuthenticationObjectsRepository")
public final class DefaultAuthenticationObjectsRepository implements AuthenticationObjectsRepository {

@Autowired(required=false)
@Qualifier("defaultAuthenticationTransactionFactory")
private AuthenticationTransactionFactory authenticationTransactionFactory = new DefaultAuthenticationTransactionFactory();
@Component("defaultAuthenticationSystemSupport")
public final class DefaultAuthenticationSystemSupport implements AuthenticationSystemSupport {

@Autowired(required=false)
@Qualifier("defaultAuthenticationTransactionManager")
Expand All @@ -30,20 +26,11 @@ public AuthenticationTransactionManager getAuthenticationTransactionManager() {
return this.authenticationTransactionManager;
}

@Override
public AuthenticationTransactionFactory getAuthenticationTransactionFactory() {
return this.authenticationTransactionFactory;
}

@Override
public PrincipalElectionStrategy getPrincipalElectionStrategy() {
return this.principalElectionStrategy;
}

public void setAuthenticationTransactionFactory(final AuthenticationTransactionFactory authenticationTransactionFactory) {
this.authenticationTransactionFactory = authenticationTransactionFactory;
}

public void setAuthenticationTransactionManager(final AuthenticationTransactionManager authenticationTransactionManager) {
this.authenticationTransactionManager = authenticationTransactionManager;
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,9 @@ public final class DefaultAuthenticationTransactionManager implements Authentica
public AuthenticationTransactionManager handle(final AuthenticationTransaction authenticationTransaction,
final AuthenticationContextBuilder authenticationContext)
throws AuthenticationException {
final Collection<Credential> sanitizedCredentials = authenticationTransaction.getCredentials();
if (!sanitizedCredentials.isEmpty()) {
final Credential[] sanitizedCredentialsArray = sanitizedCredentials.toArray(new Credential[] {});
final Authentication authentication = this.authenticationManager.authenticate(sanitizedCredentialsArray);
LOGGER.debug("Successful authentication; Collecting authentication result [{}]", authentication);
authenticationContext.collect(authentication);
} else {
LOGGER.info("No credentials were provided for authentication. Authentication event is ignored and nothing is collected into "
+ "the authentication context");
}
final Authentication authentication = this.authenticationManager.authenticate(authenticationTransaction);
LOGGER.debug("Successful authentication; Collecting authentication result [{}]", authentication);
authenticationContext.collect(authentication);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* Provides an authenticaiton manager that is inherently aware of multiple credentials and supports pluggable
Expand Down Expand Up @@ -135,9 +140,9 @@ public PolicyBasedAuthenticationManager(final Map<AuthenticationHandler, Princip
@Timed(name="AUTHENTICATE")
@Metered(name="AUTHENTICATE")
@Counted(name="AUTHENTICATE", monotonic=true)
public final Authentication authenticate(final Credential... credentials) throws AuthenticationException {
public final Authentication authenticate(final AuthenticationTransaction transaction) throws AuthenticationException {

final AuthenticationBuilder builder = authenticateInternal(credentials);
final AuthenticationBuilder builder = authenticateInternal(transaction.getCredentials());
final Authentication authentication = builder.build();
final Principal principal = authentication.getPrincipal();
if (principal instanceof NullPrincipal) {
Expand All @@ -146,10 +151,10 @@ public final Authentication authenticate(final Credential... credentials) throws

addAuthenticationMethodAttribute(builder, authentication);

logger.info("Authenticated {} with credentials {}.", principal, Arrays.asList(credentials));
logger.info("Authenticated {} with credentials {}.", principal, transaction.getCredentials());
logger.debug("Attribute map for {}: {}", principal.getId(), principal.getAttributes());

populateAuthenticationMetadataAttributes(builder, credentials);
populateAuthenticationMetadataAttributes(builder, transaction.getCredentials());

return builder.build();
}
Expand All @@ -160,7 +165,7 @@ public final Authentication authenticate(final Credential... credentials) throws
* @param builder the builder
* @param credentials the credentials
*/
private void populateAuthenticationMetadataAttributes(final AuthenticationBuilder builder, final Credential[] credentials) {
private void populateAuthenticationMetadataAttributes(final AuthenticationBuilder builder, final Collection<Credential> credentials) {
for (final AuthenticationMetaDataPopulator populator : this.authenticationMetaDataPopulators) {
for (final Credential credential : credentials) {
if (populator.supports(credential)) {
Expand Down Expand Up @@ -203,7 +208,7 @@ public void setAuthenticationPolicy(final AuthenticationPolicy policy) {
}

/**
* Follows the same contract as {@link AuthenticationManager#authenticate(Credential...)}.
* Follows the same contract as {@link AuthenticationManager#authenticate(AuthenticationTransaction)}.
*
* @param credentials One or more credentials to authenticate.
*
Expand All @@ -213,7 +218,7 @@ public void setAuthenticationPolicy(final AuthenticationPolicy policy) {
* @throws AuthenticationException When one or more credentials failed authentication such that security policy
* was not satisfied.
*/
protected AuthenticationBuilder authenticateInternal(final Credential... credentials)
protected AuthenticationBuilder authenticateInternal(final Collection<Credential> credentials)
throws AuthenticationException {

final AuthenticationBuilder builder = new DefaultAuthenticationBuilder(NullPrincipal.getInstance());
Expand Down
Loading

0 comments on commit 3a7a161

Please sign in to comment.