use of com.iplanet.sso.SSOTokenManager in project OpenAM by OpenRock.
the class CommandLineSSO method main.
public static void main(String[] args) throws Exception {
String orgName = args[0];
System.out.println("Organization: " + orgName);
SSOTokenManager manager = SSOTokenManager.getInstance();
AuthContext lc = getAuthcontext(orgName);
if (lc.getStatus() == AuthContext.Status.SUCCESS) {
System.out.println("Successful authentication ...");
SSOToken token = lc.getSSOToken();
String userDN = token.getPrincipal().getName();
System.out.println("User Name: " + userDN);
try {
AMIdentity userIdentity = IdUtils.getIdentity(token);
Map attrs = userIdentity.getAttributes();
System.out.println("User Attributes: ");
for (Iterator i = attrs.keySet().iterator(); i.hasNext(); ) {
String attrName = (String) i.next();
Set values = (Set) attrs.get(attrName);
System.out.println(attrName + "=" + values);
}
} catch (IdRepoException e) {
e.printStackTrace();
} finally {
manager.destroyToken(token);
}
} else {
System.out.println("Authentication Failed ....... ");
}
System.exit(0);
}
use of com.iplanet.sso.SSOTokenManager in project OpenAM by OpenRock.
the class SSOTokenSampleServlet method doGet.
public void doGet(HttpServletRequest request, HttpServletResponse response) {
ServletOutputStream out = null;
try {
try {
response.setContentType("text/html");
out = response.getOutputStream();
// create the sso token from http request
SSOTokenManager manager = SSOTokenManager.getInstance();
SSOToken token = manager.createSSOToken(request);
if (manager.isValidToken(token)) {
//print some of the values from the token.
String host = token.getHostName();
java.security.Principal principal = token.getPrincipal();
String authType = token.getAuthType();
int level = token.getAuthLevel();
InetAddress ipAddress = token.getIPAddress();
out.println("SSOToken host name: " + host);
out.println("<br />");
out.println("SSOToken Principal name: " + principal.getName());
out.println("<br />");
out.println("Authentication type used: " + authType);
out.println("<br />");
out.println("IPAddress of the host: " + ipAddress.getHostAddress());
out.println("<br />");
}
/* Validate the token again, with another method.
* if token is invalid, this method throws exception
*/
manager.validateToken(token);
out.println("SSO Token validation test succeeded");
out.println("<br />");
// Get the SSOTokenID associated with the token and print it.
SSOTokenID tokenId = token.getTokenID();
out.println("The token id is " + tokenId.toString());
out.println("<br />");
// Set and get some properties in the token.
token.setProperty("Company", "Sun Microsystems");
token.setProperty("Country", "USA");
String name = token.getProperty("Company");
String country = token.getProperty("Country");
out.println("Property: Company: " + name);
out.println("<br />");
out.println("Property: Country: " + country);
out.println("<br />");
// Retrieve user profile and print them
AMIdentity userIdentity = IdUtils.getIdentity(token);
Map attrs = userIdentity.getAttributes();
out.println("User Attributes: " + attrs);
/* let us add a listener to the SSOToken. Whenever a token
* event arrives, ssoTokenChanged method of the listener will
* get called.
*/
SSOTokenListener myListener = new SampleTokenListener();
token.addSSOTokenListener(myListener);
} catch (SSOException e) {
out.println("SSO Exception: " + e);
out.println("<p>Authenticate to OpenAM server before visiting this page.</p>");
e.printStackTrace();
} catch (IdRepoException e) {
out.println("IdRepo Exception: " + e);
e.printStackTrace();
} catch (IOException e) {
out.println("IO Exception: " + e);
e.printStackTrace();
} finally {
out.flush();
}
} catch (IOException e) {
// ignored
}
}
use of com.iplanet.sso.SSOTokenManager in project OpenAM by OpenRock.
the class IdRepoSampleUtils method realmLogin.
public SSOToken realmLogin(String userid, String password, String realm) throws SSOException, AuthLoginException, Exception {
SSOTokenManager mgr;
String adminDN;
String adminPassword;
SSOToken ssoToken = null;
AuthContext.IndexType authType = AuthContext.IndexType.MODULE_INSTANCE;
try {
lc = new AuthContext(realm);
} catch (AuthLoginException le) {
System.err.println("IdRepoSampleUtils: could not get AuthContext for realm " + realm);
throw le;
}
try {
lc.login();
} catch (AuthLoginException le) {
System.err.println("IdRepoSampleUtils: Failed to start login " + "for default authmodule");
throw le;
}
userID = userid;
Callback[] callbacks = null;
Hashtable values = new Hashtable();
values.put(AuthXMLTags.NAME_CALLBACK, userid);
values.put(AuthXMLTags.PASSWORD_CALLBACK, password);
while (lc.hasMoreRequirements()) {
callbacks = lc.getRequirements();
try {
fillCallbacks(callbacks, values);
lc.submitRequirements(callbacks);
} catch (Exception e) {
System.err.println("Failed to submit callbacks!");
e.printStackTrace();
return null;
}
}
AuthContext.Status istat = lc.getStatus();
if (istat == AuthContext.Status.SUCCESS) {
System.out.println("==>Authentication SUCCESSFUL for user " + userid);
} else if (istat == AuthContext.Status.COMPLETED) {
System.out.println("==>Authentication Status for user " + userid + " = " + istat);
return null;
}
try {
ssoToken = lc.getSSOToken();
} catch (Exception e) {
System.err.println("Failed to get SSO token! " + e.getMessage());
throw e;
}
return ssoToken;
}
use of com.iplanet.sso.SSOTokenManager in project OpenAM by OpenRock.
the class OpenAMResourceOwnerAuthenticator method authenticate.
/**
* {@inheritDoc}
*/
public ResourceOwner authenticate(OAuth2Request request, boolean useSession) throws NotFoundException {
SSOToken token = null;
try {
SSOTokenManager mgr = SSOTokenManager.getInstance();
token = mgr.createSSOToken(ServletUtils.getRequest(request.<Request>getRequest()));
} catch (Exception e) {
logger.warning("No SSO Token in request", e);
}
if (token == null || !useSession) {
final String username = request.getParameter(USERNAME);
final char[] password = request.getParameter(PASSWORD) == null ? null : request.<String>getParameter(PASSWORD).toCharArray();
final String realm = realmNormaliser.normalise(request.<String>getParameter(OAuth2Constants.Custom.REALM));
final String authChain = request.getParameter(AUTH_CHAIN);
return authenticate(username, password, realm, authChain);
} else {
try {
final AMIdentity id = IdUtils.getIdentity(AccessController.doPrivileged(AdminTokenAction.getInstance()), token.getProperty(Constants.UNIVERSAL_IDENTIFIER));
long authTime = stringToDate(token.getProperty(ISAuthConstants.AUTH_INSTANT)).getTime();
return new OpenAMResourceOwner(id.getName(), id, authTime);
} catch (SSOException e) {
logger.error("Unable to create ResourceOwner", e);
} catch (ParseException e) {
logger.error("Unable to create ResourceOwner", e);
} catch (IdRepoException e) {
logger.error("Unable to create ResourceOwner", e);
}
}
return null;
}
use of com.iplanet.sso.SSOTokenManager in project OpenAM by OpenRock.
the class RestUtils method hasPermission.
public static void hasPermission(final Context context) throws SSOException, IdRepoException, ForbiddenException {
SSOTokenManager mgr = SSOTokenManager.getInstance();
SSOToken ssotok = mgr.createSSOToken(getCookieFromServerContext(context));
mgr.validateToken(ssotok);
mgr.refreshSession(ssotok);
AMIdentity amIdentity = new AMIdentity(ssotok);
if (!(amIdentity.equals(AdminUserIdHolder.adminUserId))) {
debug.error("Unauthorized user.");
throw new ForbiddenException("Access Denied");
}
}
Aggregations