// Path: src/main/java/net/unicon/cas/addons/authentication/handler/StormpathAuthenticationHandler.java
// @Immutable
// public class StormpathAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler {
// 
// 	private final Application application;
// 
// 	/**
// 	 * Receives the Stormpath admin credentials and applicationId and sets up and instance of a Stormpath's Application resource
// 	 * which will be used to authenticate users.
// 	 *
// 	 * @param stormpathAccessId  accessId provided by Stormpath, for the admin user with the created API key.
// 	 * @param stormpathSecretKey secret key provided by Stormpath, for the admin user with the created API key.
// 	 * @param applicationId      This is application id configured on Stormpath whose login source will be used to authenticate users.
// 	 * @throws BeanCreationException If credentials cannot be verified by Stormpath.
// 	 */
// 	public StormpathAuthenticationHandler(final String stormpathAccessId, final String stormpathSecretKey, final String applicationId) throws BeanCreationException {
// 		final Client client = new Client(new DefaultApiKey(stormpathAccessId, stormpathSecretKey));
// 		try {
// 			this.application = client.getDataStore().getResource(String.format("/applications/%s", applicationId), Application.class);
// 		}
// 		catch (Throwable e) {
// 			throw new BeanCreationException("An exception is caught trying to access Stormpath cloud. " +
// 					"Please verify that your provided Stormpath <accessId>, " +
// 					"<secretKey>, and <applicationId> are correct. Original Stormpath error: " + e.getMessage());
// 		}
// 	}
// 
// 	@Override
// 	protected boolean authenticateUsernamePasswordInternal(final UsernamePasswordCredentials credentials) throws AuthenticationException {
// 		try {
// 			this.log.debug("Attempting to authenticate user [{}] against application [{}] in Stormpath cloud...", credentials.getUsername(), this.application.getName());
// 			this.authenticateAccount(credentials);
// 			this.log.debug("Successfully authenticated user [{}]", credentials.getUsername());
// 			return true;
// 		}
// 		catch (ResourceException e) {
// 			this.log.error(e.getMessage(), e);
// 			throw new BadCredentialsAuthenticationException();
// 		}
// 	}
// 
//     public Account authenticateAccount(final UsernamePasswordCredentials credentials) throws ResourceException {
//         return this.application.authenticateAccount(new UsernamePasswordRequest(credentials.getUsername(), credentials.getPassword())).getAccount();
//     }
// }


import com.stormpath.sdk.account.Account;
import com.stormpath.sdk.resource.ResourceException;
import net.unicon.cas.addons.authentication.handler.StormpathAuthenticationHandler;
import net.unicon.cas.addons.support.ThreadSafe;
import org.jasig.cas.authentication.principal.Credentials;
import org.jasig.cas.authentication.principal.CredentialsToPrincipalResolver;
import org.jasig.cas.authentication.principal.Principal;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
package net.unicon.cas.addons.authentication.principal;


/**
 * An implementation of {@link org.jasig.cas.authentication.principal.CredentialsToPrincipalResolver}
 * that resolves instances of {@link StormpathPrincipal} from provided {@link org.jasig.cas.authentication.principal.UsernamePasswordCredentials}
 * <p/>
 * Note that this implementation makes a remote HTTP call to Stormapth cloud to authenticate the credential and therefore
 * retrieve an instance of an {@link com.stormpath.sdk.account.Account}. Thus, 2 Stormpath remote authentication calls are made -
 * one is during authentication by {@link net.unicon.cas.addons.authentication.handler.StormpathAuthenticationHandler} and
 * one is during <code>Account</code> retrieval by this class.
 *
 * @author Dmitriy Kopylenko
 * @author Unicon, inc.
 * @since 1.4
 */
@ThreadSafe
public class StormpathPrincipalResolver implements CredentialsToPrincipalResolver {

// Path: src/main/java/net/unicon/cas/addons/authentication/handler/StormpathAuthenticationHandler.java
// @Immutable
// public class StormpathAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler {
// 
// 	private final Application application;
// 
// 	/**
// 	 * Receives the Stormpath admin credentials and applicationId and sets up and instance of a Stormpath's Application resource
// 	 * which will be used to authenticate users.
// 	 *
// 	 * @param stormpathAccessId  accessId provided by Stormpath, for the admin user with the created API key.
// 	 * @param stormpathSecretKey secret key provided by Stormpath, for the admin user with the created API key.
// 	 * @param applicationId      This is application id configured on Stormpath whose login source will be used to authenticate users.
// 	 * @throws BeanCreationException If credentials cannot be verified by Stormpath.
// 	 */
// 	public StormpathAuthenticationHandler(final String stormpathAccessId, final String stormpathSecretKey, final String applicationId) throws BeanCreationException {
// 		final Client client = new Client(new DefaultApiKey(stormpathAccessId, stormpathSecretKey));
// 		try {
// 			this.application = client.getDataStore().getResource(String.format("/applications/%s", applicationId), Application.class);
// 		}
// 		catch (Throwable e) {
// 			throw new BeanCreationException("An exception is caught trying to access Stormpath cloud. " +
// 					"Please verify that your provided Stormpath <accessId>, " +
// 					"<secretKey>, and <applicationId> are correct. Original Stormpath error: " + e.getMessage());
// 		}
// 	}
// 
// 	@Override
// 	protected boolean authenticateUsernamePasswordInternal(final UsernamePasswordCredentials credentials) throws AuthenticationException {
// 		try {
// 			this.log.debug("Attempting to authenticate user [{}] against application [{}] in Stormpath cloud...", credentials.getUsername(), this.application.getName());
// 			this.authenticateAccount(credentials);
// 			this.log.debug("Successfully authenticated user [{}]", credentials.getUsername());
// 			return true;
// 		}
// 		catch (ResourceException e) {
// 			this.log.error(e.getMessage(), e);
// 			throw new BadCredentialsAuthenticationException();
// 		}
// 	}
// 
//     public Account authenticateAccount(final UsernamePasswordCredentials credentials) throws ResourceException {
//         return this.application.authenticateAccount(new UsernamePasswordRequest(credentials.getUsername(), credentials.getPassword())).getAccount();
//     }
// }


// Path: src/main/java/net/unicon/cas/addons/authentication/principal/StormpathPrincipalResolver.java
import com.stormpath.sdk.account.Account;
import com.stormpath.sdk.resource.ResourceException;
import net.unicon.cas.addons.authentication.handler.StormpathAuthenticationHandler;
import net.unicon.cas.addons.support.ThreadSafe;
import org.jasig.cas.authentication.principal.Credentials;
import org.jasig.cas.authentication.principal.CredentialsToPrincipalResolver;
import org.jasig.cas.authentication.principal.Principal;
import org.jasig.cas.authentication.principal.UsernamePasswordCredentials;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

package net.unicon.cas.addons.authentication.principal;


/**
 * An implementation of {@link org.jasig.cas.authentication.principal.CredentialsToPrincipalResolver}
 * that resolves instances of {@link StormpathPrincipal} from provided {@link org.jasig.cas.authentication.principal.UsernamePasswordCredentials}
 * <p/>
 * Note that this implementation makes a remote HTTP call to Stormapth cloud to authenticate the credential and therefore
 * retrieve an instance of an {@link com.stormpath.sdk.account.Account}. Thus, 2 Stormpath remote authentication calls are made -
 * one is during authentication by {@link net.unicon.cas.addons.authentication.handler.StormpathAuthenticationHandler} and
 * one is during <code>Account</code> retrieval by this class.
 *
 * @author Dmitriy Kopylenko
 * @author Unicon, inc.
 * @since 1.4
 */
@ThreadSafe
public class StormpathPrincipalResolver implements CredentialsToPrincipalResolver {

    private final StormpathAuthenticationHandler stormpathAuthenticationHandler;