use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.
the class UserPrivilegeTest method createSessionToken.
private SSOToken createSessionToken(String orgName, String userId, String password, String module, int level) throws Exception {
AuthContext ac = null;
try {
ac = new AuthContext(orgName);
if (module != null) {
ac.login(AuthContext.IndexType.MODULE_INSTANCE, module);
} else if (level != -1) {
ac.login(AuthContext.IndexType.LEVEL, String.valueOf(level));
} else {
ac.login();
}
} catch (LoginException le) {
le.printStackTrace();
return null;
}
try {
Callback[] callbacks = null;
// Get the information requested by the plug-ins
if (ac.hasMoreRequirements()) {
callbacks = ac.getRequirements();
if (callbacks != null) {
addLoginCallbackMessage(callbacks, userId, password);
ac.submitRequirements(callbacks);
if (ac.getStatus() == AuthContext.Status.SUCCESS) {
//System.out.println("Auth success");
Subject authSubject = ac.getSubject();
if (authSubject != null) {
Iterator principals = (authSubject.getPrincipals()).iterator();
Principal principal;
while (principals.hasNext()) {
principal = (Principal) principals.next();
}
}
} else if (ac.getStatus() == AuthContext.Status.FAILED) {
//System.out.println("Authentication has FAILED");
} else {
}
} else {
}
}
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println(ac.getSSOToken().getPrincipal().getName());
return ac.getSSOToken();
}
use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.
the class PolicyEvaluatorTest method login.
private SSOToken login() throws Exception {
lc = new AuthContext("/");
AuthContext.IndexType indexType = AuthContext.IndexType.MODULE_INSTANCE;
lc.login(indexType, "DataStore");
Callback[] callbacks = null;
// get information requested from module
while (lc.hasMoreRequirements()) {
callbacks = lc.getRequirements();
if (callbacks != null) {
addLoginCallbackMessage(callbacks);
lc.submitRequirements(callbacks);
}
}
return (lc.getStatus() == AuthContext.Status.SUCCESS) ? lc.getSSOToken() : null;
}
use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.
the class ClientAuthenticatorImpl method authenticate.
/**
* Perform the authentication of the client using the specified client credentials.
*
* @param clientId The client's id.
* @param clientSecret The client's secret.
* @param realm The realm the client exists in.
* @return {@code true} if the client was authenticated successfully.
* @throws InvalidClientException If the authentication configured for the client is not completed by the
* specified client credentials.
*/
private boolean authenticate(OAuth2Request request, String clientId, char[] clientSecret, String realm) throws InvalidClientException {
try {
AuthContext lc = new AuthContext(realm);
lc.login(AuthContext.IndexType.MODULE_INSTANCE, "Application");
while (lc.hasMoreRequirements()) {
Callback[] callbacks = lc.getRequirements();
List<Callback> missing = new ArrayList<Callback>();
// loop through the requires setting the needs..
for (final Callback callback : callbacks) {
if (callback instanceof NameCallback) {
NameCallback nc = (NameCallback) callback;
nc.setName(clientId);
} else if (callback instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback) callback;
pc.setPassword(clientSecret);
} else {
missing.add(callback);
}
}
// there's missing requirements not filled by this
if (missing.size() > 0) {
lc.logout();
throw failureFactory.getException(request, "Missing requirements");
}
lc.submitRequirements(callbacks);
}
// validate the password..
if (lc.getStatus() == AuthContext.Status.SUCCESS) {
lc.logout();
return true;
} else {
throw failureFactory.getException(request, "Client authentication failed");
}
} catch (AuthLoginException le) {
logger.error("ClientVerifierImpl::authContext AuthException", le);
throw failureFactory.getException(request, "Client authentication failed");
}
}
use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.
the class OpenAMResourceOwnerAuthenticator method authenticate.
private ResourceOwner authenticate(String username, char[] password, String realm, String service) {
ResourceOwner ret = null;
AuthContext lc = null;
try {
lc = new AuthContext(realm);
if (service != null) {
lc.login(AuthContext.IndexType.SERVICE, service, null, ServletUtils.getRequest(Request.getCurrent()), ServletUtils.getResponse(Response.getCurrent()));
} else {
lc.login(ServletUtils.getRequest(Request.getCurrent()), ServletUtils.getResponse(Response.getCurrent()));
}
while (lc.hasMoreRequirements()) {
Callback[] callbacks = lc.getRequirements();
ArrayList missing = new ArrayList();
// loop through the requires setting the needs..
for (int i = 0; i < callbacks.length; i++) {
if (callbacks[i] instanceof NameCallback) {
NameCallback nc = (NameCallback) callbacks[i];
nc.setName(username);
} else if (callbacks[i] instanceof PasswordCallback) {
PasswordCallback pc = (PasswordCallback) callbacks[i];
pc.setPassword(password);
} else {
missing.add(callbacks[i]);
}
}
// there's missing requirements not filled by this
if (missing.size() > 0) {
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, "Missing requirements");
}
lc.submitRequirements(callbacks);
}
// validate the password..
if (lc.getStatus() == AuthContext.Status.SUCCESS) {
try {
// package up the token for transport..
ret = createResourceOwner(lc);
} catch (Exception e) {
logger.error("Unable to get SSOToken", e);
// because the system is likely down..
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, e);
}
}
} catch (AuthLoginException le) {
logger.error("AuthException", le);
throw new ResourceException(Status.SERVER_ERROR_INTERNAL, le);
} finally {
if (lc != null && AuthContext.Status.SUCCESS.equals(lc.getStatus())) {
try {
lc.logout();
logger.message("Logged user out.");
} catch (AuthLoginException e) {
logger.error("Exception caught logging out of AuthContext after successful login", e);
}
}
}
return ret;
}
use of com.sun.identity.authentication.AuthContext in project OpenAM by OpenRock.
the class TokenUtils method getSessionToken.
public static SSOToken getSessionToken(String orgName, String userId, String password, String module, int level) throws Exception {
AuthContext ac = null;
try {
//System.out.println("TokenUtils:orgName=" + orgName);
ac = new AuthContext(orgName);
if (module != null) {
ac.login(AuthContext.IndexType.MODULE_INSTANCE, module);
} else if (level != -1) {
ac.login(AuthContext.IndexType.LEVEL, String.valueOf(level));
} else {
//System.out.println("TokenUtils:calling login()");
ac.login();
}
//System.out.println("TokenUtils:after ac.login()");
} catch (LoginException le) {
le.printStackTrace();
return null;
}
try {
Callback[] callbacks = null;
// Get the information requested by the plug-ins
if (ac.hasMoreRequirements()) {
callbacks = ac.getRequirements();
if (callbacks != null) {
addLoginCallbackMessage(callbacks, userId, password);
ac.submitRequirements(callbacks);
if (ac.getStatus() == AuthContext.Status.SUCCESS) {
//System.out.println("Auth success");
Subject authSubject = ac.getSubject();
if (authSubject != null) {
Iterator principals = (authSubject.getPrincipals()).iterator();
Principal principal;
while (principals.hasNext()) {
principal = (Principal) principals.next();
}
}
} else if (ac.getStatus() == AuthContext.Status.FAILED) {
//System.out.println("Authentication has FAILED");
} else {
}
} else {
}
}
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println(ac.getSSOToken().getPrincipal().getName());
return ac.getSSOToken();
}
Aggregations