use of com.sun.identity.idm.IdRepoException 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.sun.identity.idm.IdRepoException 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.sun.identity.idm.IdRepoException in project OpenAM by OpenRock.
the class IdRepoSampleSearchIds method processType.
/*
* given a set of AMIdentities of IdType idtype, see
* if any AMIdentity operations are to be performed
* on/with them.
*/
private void processType(IdType idtype, Set idSet) {
Object[] objs = idSet.toArray();
AMIdentity amid = null;
AMIdentity amid2 = null;
int setsize = idSet.size();
int i;
if (setsize > 0) {
System.out.println("Search returns " + setsize + " entries of type " + idtype.getName() + ".");
for (i = 0; i < setsize; i++) {
amid = (AMIdentity) objs[i];
System.out.println("\t" + i + ": " + amid.getName());
}
System.out.println("\t" + i + ": No selection");
String answer = sampleUtils.getLine("Select identity: [0.." + setsize + "]: ");
int ians = sampleUtils.getIntValue(answer);
try {
if ((ians >= 0) && (ians < setsize)) {
amid = (AMIdentity) objs[ians];
} else if (ians == setsize) {
return;
} else {
System.err.println("'" + answer + "' is invalid.");
return;
}
System.out.println(" universalId for " + amid.getName() + " of IdType " + idtype.getName() + " = " + amid.getUniversalId());
/*
* have the AMIdentity to work with in amid
*
* for IdType given, the operations allowed:
*
* GROUP
*/
if (idtype.equals(IdType.GROUP)) {
/*
* can:
* get attributes
* get attribute
* get members (of type User)
*/
System.out.println("Members of IdType User of Group '" + amid.getName() + "':");
printMembers(amid, IdType.USER);
printAttrs(amid);
} else if (idtype.equals(IdType.ROLE)) {
/*
* can:
* get attributes
* get attribute
* get members
*/
printAttrs(amid);
} else if (idtype.equals(IdType.USER)) {
String thisUser = amid.getName();
/*
* can:
* see if active
* set active status
* get attributes
* get attribute
* set attributes
* remove attributes
* store
* get memberships
* see if exists
*/
System.out.println("User '" + thisUser + "' is active: " + amid.isActive());
if (thisUser.equalsIgnoreCase("amadmin") || thisUser.equalsIgnoreCase("dsameuser") || thisUser.equalsIgnoreCase("amService-URLAccessAgent")) {
// don't want to mess too much with these users
// in particular
System.out.println("User '" + amid.getName() + "' exists: " + amid.isExists());
Set idtypes = amid.getType().canBeMemberOf();
System.out.println(amid.getName() + " can have (and has) membership in identities of " + "the following types:");
IdType idTypeToUse = null;
Set memberships = null;
for (Iterator it = idtypes.iterator(); it.hasNext(); ) {
idTypeToUse = (IdType) it.next();
System.out.println(" can be member of " + idTypeToUse.getName());
memberships = amid.getMemberships(idTypeToUse);
printMemberships(amid, idTypeToUse, memberships);
}
printAttrs(amid);
} else {
answer = sampleUtils.getLine("Set user active, inactive, or cancel [a,i,c]: ");
if (answer.startsWith("a")) {
if (amid.isActive()) {
System.out.println("User '" + thisUser + "' already active");
} else {
amid.setActiveStatus(true);
System.out.println("User '" + thisUser + "' is active: " + amid.isActive());
}
} else if (answer.startsWith("i")) {
if (!amid.isActive()) {
System.out.println("User '" + thisUser + "' already inactive");
} else {
amid.setActiveStatus(false);
System.out.println("User '" + thisUser + "' is active: " + amid.isActive());
}
}
System.out.println("User '" + amid.getName() + "' exists: " + amid.isExists());
Set idtypes = amid.getType().canBeMemberOf();
System.out.println(amid.getName() + " can have (and has) membership in identities of " + "the following types:");
IdType idTypeToUse = null;
Set memberships = null;
for (Iterator it = idtypes.iterator(); it.hasNext(); ) {
idTypeToUse = (IdType) it.next();
System.out.println(" can be member of " + idTypeToUse.getName());
memberships = amid.getMemberships(idTypeToUse);
printMemberships(amid, idTypeToUse, memberships);
}
printAttrs(amid);
System.out.println("Operations available on User '" + amid.getName() + "':");
System.out.println("\tl: List groups or roles\n" + "\td: Display attributes\n" + "\ts: Set attribute\n" + "\te: No selection");
answer = sampleUtils.getLine("Enter selection [l, d, s, e]: ");
if (answer.toLowerCase().startsWith("d")) {
printAttrs(amid);
} else if (answer.toLowerCase().startsWith("s")) {
setAttribute(amid);
} else if (answer.toLowerCase().startsWith("l")) {
listGrpOrRoleOfUser(amid);
} else if (answer.toLowerCase().startsWith("e")) {
} else {
System.err.println("'" + answer + "' is invalid.");
}
}
} else if (idtype.equals(IdType.AGENT) || idtype.equals(IdType.AGENTONLY)) {
/*
* can:
* see if exists
* see if active
* set active status
* get attributes
* get attribute
* set attributes
* remove attributes
* store
*
*/
String thisAgent = amid.getName();
System.out.println("Agent '" + thisAgent + "' exists: " + amid.isExists());
System.out.println("Agent '" + thisAgent + "' is active: " + amid.isActive());
answer = sampleUtils.getLine("Set agent active, inactive, or cancel [a,i,c]: ");
if (answer.startsWith("a")) {
if (amid.isActive()) {
System.out.println("Agent '" + thisAgent + "' already active");
} else {
amid.setActiveStatus(true);
System.out.println("Agent '" + thisAgent + "' is active: " + amid.isActive());
}
} else if (answer.startsWith("i")) {
if (!amid.isActive()) {
System.out.println("Agent '" + thisAgent + "' already inactive");
} else {
amid.setActiveStatus(false);
System.out.println("Agent '" + thisAgent + "' is active: " + amid.isActive());
}
}
printAttrs(amid);
setAttribute(amid);
}
} catch (IdRepoException ire) {
System.err.println("processType:IdRepoException: " + ire.getMessage());
} catch (SSOException ssoe) {
System.err.println("processType:SSOException: " + ssoe.getMessage());
}
} else {
System.out.println("No identities of type '" + idtype.getName() + "' found to process.");
}
}
use of com.sun.identity.idm.IdRepoException in project OpenAM by OpenRock.
the class IdRepoSampleSearchIds method listGrpOrRoleOfUser.
/*
* list groups or roles for the specified user
*/
private void listGrpOrRoleOfUser(AMIdentity amid) {
IdType typeToGet = null;
Set tSet = null;
String hdr = null;
String ans = sampleUtils.getLine("Groups, or Roles to list [g, r]: ");
String ans2 = ans.toLowerCase();
try {
String trlr = null;
hdr = "User " + amid.getName();
if (ans2.startsWith("g")) {
// list groups
tSet = amid.getMemberships(IdType.GROUP);
trlr = "group memberships";
} else if (ans2.startsWith("r")) {
// list roles
tSet = amid.getMemberships(IdType.ROLE);
trlr = "role memberships";
}
sampleUtils.printResults(hdr, tSet, trlr);
} catch (IdRepoException ire) {
System.err.println("listGrpOrRoleOfUser:IdRepoException: " + ire.getMessage());
} catch (SSOException ssoe) {
System.err.println("listGrpOrRoleOfUser:SSOException: " + ssoe.getMessage());
}
}
use of com.sun.identity.idm.IdRepoException 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;
}
Aggregations