use of com.sun.identity.idm.AMIdentity in project OpenAM by OpenRock.
the class IdRepoSampleUtils method selectFromSet.
/*
* print the objects (String or AMIdentity.getName()) in the
* specified Set, and return the object of the one selected.
* null if none selected.
*/
public Object selectFromSet(Set itemSet) {
Object[] objs = itemSet.toArray();
AMIdentity amid = null;
AMIdentity amid2 = null;
int setsize = itemSet.size();
int i;
boolean isAMId = false;
boolean isString = false;
String str = null;
if (setsize <= 0) {
return null;
}
String objclass = objs[0].getClass().getName();
if (objclass.indexOf("AMIdentity") >= 0) {
isAMId = true;
} else if (objclass.indexOf("String") >= 0) {
isString = true;
}
if (setsize > 0) {
System.out.println("Available selections:");
for (i = 0; i < setsize; i++) {
if (isAMId) {
amid = (AMIdentity) objs[i];
System.out.println("\t" + i + ": " + amid.getName());
} else if (isString) {
System.out.println("\t" + i + ": " + (String) objs[i]);
} else {
System.out.println("\t" + i + ": Class = " + objclass);
}
}
System.out.println("\t" + i + ": No selection");
String answer = getLine("Select identity: [0.." + setsize + "]: ");
int ians = getIntValue(answer);
if ((ians >= 0) && (ians < setsize)) {
return (objs[ians]);
} else if (ians == setsize) {
} else {
System.err.println("'" + answer + "' is invalid.");
}
}
return null;
}
use of com.sun.identity.idm.AMIdentity 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.AMIdentity in project OpenAM by OpenRock.
the class UserProfileServlet method doGet.
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// Get query parameters
String orgname = request.getParameter("orgname");
if (orgname == null || orgname.length() == 0) {
orgname = "/";
}
String username = request.getParameter("username");
String password = request.getParameter("password");
response.setContentType("text/html");
// Get the output stream
PrintWriter out = response.getWriter();
out.println(SampleConstants.HTML_HEADER);
if (username == null || password == null) {
out.println("Value for user name and password are required.");
out.println("</body></html>");
return;
}
out.println("<br><h3>Username:</h3> " + username);
try {
// Authenticate the user and obtain SSO Token
AuthContext lc = authenticate(orgname, username, password, out);
if (lc != null) {
// Obtain the SSO Token
SSOToken token = lc.getSSOToken();
out.println("<br><h3>SSOToken:</h3> " + token.getTokenID());
out.println("<br><h3>User DN:</h3> " + token.getPrincipal().getName());
out.println("<p>");
AMIdentity amid = IdUtils.getIdentity(token);
Map attrs = amid.getAttributes();
out.println("User Attributes: ");
for (Iterator i = attrs.keySet().iterator(); i.hasNext(); ) {
String attrName = (String) i.next();
Set values = (Set) attrs.get(attrName);
out.println(attrName + "=" + values);
}
}
} catch (Exception e) {
e.printStackTrace(out);
out.println("</body></html>");
}
}
use of com.sun.identity.idm.AMIdentity 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.AMIdentity 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.");
}
}
Aggregations