use of javax.security.auth.Subject in project jdk8u_jdk by JetBrains.
the class TestJMXAuthenticator method authenticate.
public Subject authenticate(Object credentials) {
String credentials_username = "";
String credentials_password = "";
Principal aPrincipal = null;
credentials_username = ((String[]) credentials)[0];
credentials_password = ((String[]) credentials)[1];
String authenticated_username = System.getProperty("susername");
String authenticated_password = System.getProperty("spassword");
String principal = System.getProperty("principal");
System.out.println("TestJMXAuthenticator::authenticate: Start");
System.out.println("TestJMXAuthenticator::authenticate: credentials username = " + credentials_username);
System.out.println("TestJMXAuthenticator::authenticate: credentials password = " + credentials_password);
System.out.println("TestJMXAuthenticator::authenticate: authenticated username = " + authenticated_username);
System.out.println("TestJMXAuthenticator::authenticate: authenticated password = " + authenticated_password);
System.out.println("TestJMXAuthenticator::authenticate: principal used for " + "authorization = " + principal);
if (credentials_username.equals(authenticated_username) && credentials_password.equals(authenticated_password)) {
System.out.println("TestJMXAuthenticator::authenticate: " + "Authenticator should succeed");
} else {
System.out.println("TestJMXAuthenticator::authenticate: " + "Authenticator should reject");
throw new SecurityException("TestJMXAuthenticator throws EXCEPTION");
}
// At this point, authentication has succeeded
// (no SecurityException thrown).
//
// If no authorization is required, the returned subject (empty or not)
// is useless.
// Otherwise, the returned subject must define a principal
// and authorization will be performed against this principal.
//
// Note that this custom JMXAuthenticator is used for test purpose and
// the username used to perform authentication may be different from the
// username used to perform authorization.
//
Subject subject = new Subject();
if (principal != null) {
System.out.println("TestJMXAuthenticator::authenticate: " + "Add " + principal + " principal to the returned subject");
subject.getPrincipals().add(new JMXPrincipal(principal));
}
return subject;
}
use of javax.security.auth.Subject in project jdk8u_jdk by JetBrains.
the class TwoOrThree method main.
public static void main(String[] args) throws Exception {
String server = args[0].equals("-") ? null : args[0];
String target = args[1];
OneKDC kdc = new OneKDC(null);
kdc.addPrincipal("first", "first".toCharArray());
kdc.addPrincipal("second", "second".toCharArray());
kdc.addPrincipal("third", "third".toCharArray());
kdc.writeKtab(OneKDC.KTAB);
Context c = Context.fromUserPass(OneKDC.USER, OneKDC.PASS, false);
// Using keytabs
Subject sub4s = new Subject();
Context.fromUserKtab(sub4s, "first", OneKDC.KTAB, true);
Context s = Context.fromUserKtab(sub4s, "second", OneKDC.KTAB, true);
c.startAsClient(target, GSSUtil.GSS_KRB5_MECH_OID);
s.startAsServer(server, GSSUtil.GSS_KRB5_MECH_OID);
Context.handshake(c, s);
// Using keys
sub4s = new Subject();
Context.fromUserPass(sub4s, "first", "first".toCharArray(), true);
s = Context.fromUserPass(sub4s, "second", "second".toCharArray(), true);
c.startAsClient(target, GSSUtil.GSS_KRB5_MECH_OID);
s.startAsServer(server, GSSUtil.GSS_KRB5_MECH_OID);
Context.handshake(c, s);
s.dispose();
c.dispose();
}
use of javax.security.auth.Subject in project opennms by OpenNMS.
the class OpenNMSJaasAuthenticationBroker method authenticate.
@Override
public SecurityContext authenticate(String username, String password, X509Certificate[] certificates) throws SecurityException {
SecurityContext result = null;
JassCredentialCallbackHandler callback = new JassCredentialCallbackHandler(username, password);
try {
LoginContext lc = new LoginContext(JAAS_CONTEXT_NAME, callback);
lc.login();
Subject subject = lc.getSubject();
result = new JaasSecurityContext(username, subject);
} catch (Exception ex) {
throw new SecurityException("User name [" + username + "] or password is invalid.", ex);
}
return result;
}
use of javax.security.auth.Subject in project opennms by OpenNMS.
the class KarafTestCase method executeCommand.
/**
* Executes a shell command and returns output as a String.
* Commands have a default timeout of 10 seconds.
*
* @param command
* @return
*/
protected String executeCommand(final String command) {
try (final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final PrintStream printStream = new PrintStream(byteArrayOutputStream)) {
Subject subject = new Subject();
subject.getPrincipals().add(new RolePrincipal("admin"));
return Subject.doAs(subject, new PrivilegedExceptionAction<String>() {
@Override
public String run() throws Exception {
final CommandProcessor commandProcessor = getOsgiService(CommandProcessor.class);
final CommandSession commandSession = commandProcessor.createSession(System.in, printStream, System.err);
LOG.info("{}", command);
Object response = commandSession.execute(command);
LOG.info("Response: {}", response);
printStream.flush();
return byteArrayOutputStream.toString();
}
});
} catch (Exception e) {
LOG.error("Error while executing command", e);
throw new RuntimeException(e);
}
}
use of javax.security.auth.Subject in project OpenAM by OpenRock.
the class UpdateApplicationPrivilege method handleRequest.
/**
* Services a Commandline Request.
*
* @param rc Request Context.
* @throws CLIException if the request cannot serviced.
*/
@Override
public void handleRequest(RequestContext rc) throws CLIException {
super.handleRequest(rc);
String realm = getStringOptionValue(IArgument.REALM_NAME);
String name = getStringOptionValue(PARAM_NAME);
String[] params = { realm, name };
String description = getStringOptionValue(PARAM_DESCRIPTION);
boolean hasDescription = (description != null) && description.trim().length() > 0;
String actions = getStringOptionValue(PARAM_ACTIONS);
ApplicationPrivilege.PossibleAction action = (actions != null) ? getActions() : null;
if (!hasDescription && (action == null)) {
throw new CLIException(getResourceString("update-application-privilege-invalid"), ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
}
Subject userSubject = SubjectUtils.createSubject(getAdminSSOToken());
ApplicationPrivilegeManager apm = ApplicationPrivilegeManager.getInstance(realm, userSubject);
writeLog(LogWriter.LOG_ACCESS, Level.INFO, "ATTEMPT_UPDATE_APPLICATION_PRIVILEGE", params);
try {
ApplicationPrivilege appPrivilege = apm.getPrivilege(name);
if (hasDescription) {
appPrivilege.setDescription(description);
}
if (action != null) {
appPrivilege.setActionValues(action);
}
apm.replacePrivilege(appPrivilege);
Object[] msgParam = { name };
getOutputWriter().printlnMessage(MessageFormat.format(getResourceString("update-application-privilege-succeeded"), msgParam));
writeLog(LogWriter.LOG_ACCESS, Level.INFO, "SUCCEEDED_UPDATE_APPLICATION_PRIVILEGE", params);
} catch (EntitlementException ex) {
String[] paramExs = { realm, name, ex.getMessage() };
writeLog(LogWriter.LOG_ACCESS, Level.INFO, "FAILED_UPDATE_APPLICATION_PRIVILEGE", paramExs);
throw new CLIException(ex, ExitCodes.REQUEST_CANNOT_BE_PROCESSED);
}
}
Aggregations