import java.util.HashMap; import java.util.Map; import com.hyperion.css.CSSException; import com.hyperion.css.common.CSSUserIF; /** * Authenticate demonstrates the usage of CSS API to authenticate users * using password, and CSS token. */ public class AuthenticateSample extends Sample { public AuthenticateSample() throws Exception { super(); } /** * CSS Authenticate Sample - Main * * The sample does: * 1. Authenticate using username/password * 2. Authenticate using CSS token */ public static void main(String[] args) { AuthenticateSample ct = null; try { ct = new AuthenticateSample(); ct.printMsg(System.out, "Main methods:"); // Authenticate Using UserName Password ct.printMsg(System.out, "Authenticating admin ..."); CSSUserIF user = ct.authenticate("admin", "password"); ct.printMsg(System.out, "Authenticated."); //Get the CSS token from the User String token = user.getToken(); // Authenticate Using CSS Token ct.printMsg(System.out, "Authenticating admin by token ..."); user = ct.authenticateByToken(token); //Print token for authenticated user String authToken = user.getToken(); ct.dump(System.out, "Authenticated token ", new String[]{authToken}); } catch (Throwable e) { e.printStackTrace(); System.out.println("Could not run sample because of error " + e.getClass().getName() + ": " + e.getMessage()); } finally { if (ct != null) ct.shutdown(); } } /** * Authenticate User */ public CSSUserIF authenticate(String username, String password) { CSSUserIF user = null; System.out.println("Authenticating user: " + username); try { Map context = new HashMap(); user = cssAPI.authenticate(context, username, password); System.out.println("Got User: " + user); } catch(CSSException ce) { ce.printStackTrace(); } return user; } /** * Authenticate User By Token */ public CSSUserIF authenticateByToken(String token) { CSSUserIF user = null; System.out.println("Authenticating user by token: " + token); try { Map context = new HashMap(); user = cssAPI.authenticateToken(context, token); System.out.println("Got User: " + user); } catch(CSSException ce) { ce.printStackTrace(); } return user; } public void cleanup(String[]ids) throws CSSException {} }