use of org.apache.karaf.jaas.boot.principal.ClientPrincipal in project karaf by apache.
the class JaasAuthenticator method authenticate.
public Subject authenticate(Object credentials) throws SecurityException {
if (!(credentials instanceof String[])) {
throw new IllegalArgumentException("Expected String[2], got " + (credentials != null ? credentials.getClass().getName() : null));
}
final String[] params = (String[]) credentials;
if (params.length != 2) {
throw new IllegalArgumentException("Expected String[2] but length was " + params.length);
}
try {
Subject subject = new Subject();
try {
subject.getPrincipals().add(new ClientPrincipal("jmx", RemoteServer.getClientHost()));
} catch (Throwable t) {
// Ignore
}
LoginContext loginContext = new LoginContext(realm, subject, callbacks -> {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
((NameCallback) callback).setName(params[0]);
} else if (callback instanceof PasswordCallback) {
((PasswordCallback) callback).setPassword((params[1].toCharArray()));
} else {
throw new UnsupportedCallbackException(callback);
}
}
});
loginContext.login();
int roleCount = 0;
for (Principal principal : subject.getPrincipals()) {
if (principal instanceof RolePrincipal) {
roleCount++;
}
}
if (roleCount == 0) {
throw new FailedLoginException("User doesn't have role defined");
}
return subject;
} catch (LoginException e) {
throw new SecurityException("Authentication failed", e);
}
}
use of org.apache.karaf.jaas.boot.principal.ClientPrincipal in project karaf by apache.
the class AbstractAuditLoginModuleTest method getPrincipalInfo.
@Test
public void getPrincipalInfo() {
LogAuditLoginModule module = new LogAuditLoginModule();
Map<String, String> options = new HashMap<>();
options.put("logger", "test");
Subject subject = new Subject();
subject.getPrincipals().add(new ClientPrincipal("ssh", "/127.0.0.1"));
subject.getPrincipals().add(new ClientPrincipal("ssh", "/127.0.0.2"));
subject.getPrincipals().add((UserPrincipal) () -> "noexist");
module.initialize(subject, new NamePasswordCallbackHandler("myuser", "mypassword"), null, options);
Assert.assertEquals("ssh(/127.0.0.1), ssh(/127.0.0.2)", module.getPrincipalInfo());
}
use of org.apache.karaf.jaas.boot.principal.ClientPrincipal in project karaf by apache.
the class LocalConsoleManager method createLocalKarafSubject.
private Subject createLocalKarafSubject() {
String userName = System.getProperty(KARAF_LOCAL_USER);
if (userName == null) {
userName = "karaf";
}
final Subject subject = new Subject();
subject.getPrincipals().add(new UserPrincipal(userName));
subject.getPrincipals().add(new ClientPrincipal("local", "localhost"));
String roles = System.getProperty(KARAF_LOCAL_ROLES, KARAF_LOCAL_ROLES_DEFAULT);
if (roles != null) {
for (String role : roles.split("[,]")) {
subject.getPrincipals().add(new RolePrincipal(role.trim()));
}
}
return subject;
}
use of org.apache.karaf.jaas.boot.principal.ClientPrincipal in project karaf by apache.
the class Buffer method format.
public Buffer format(Subject subject) throws IOException {
String up = null;
String cp = null;
for (Principal p : subject.getPrincipals()) {
if (p instanceof UserPrincipal) {
up = p.getName();
} else if (p instanceof ClientPrincipal) {
cp = p.getName();
}
}
if (up != null) {
append(up);
} else {
append('?');
}
if (cp != null) {
append('@');
append(cp);
}
return this;
}
use of org.apache.karaf.jaas.boot.principal.ClientPrincipal in project karaf by apache.
the class JaasSecurityProvider method doAuthenticate.
public Subject doAuthenticate(final String address, final String username, final String password) {
try {
Subject subject = new Subject();
subject.getPrincipals().add(new ClientPrincipal("webconsole", address));
LoginContext loginContext = new LoginContext(realm, subject, callbacks -> {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
((NameCallback) callback).setName(username);
} else if (callback instanceof PasswordCallback) {
((PasswordCallback) callback).setPassword(password.toCharArray());
} else {
throw new UnsupportedCallbackException(callback);
}
}
});
loginContext.login();
if (role != null && role.length() > 0) {
String clazz = "org.apache.karaf.jaas.boot.principal.RolePrincipal";
String name = role;
int idx = role.indexOf(':');
if (idx > 0) {
clazz = role.substring(0, idx);
name = role.substring(idx + 1);
}
boolean found = false;
for (Principal p : subject.getPrincipals()) {
if (p.getClass().getName().equals(clazz) && p.getName().equals(name)) {
found = true;
break;
}
}
if (!found) {
throw new FailedLoginException("User does not have the required role " + role);
}
}
return subject;
} catch (FailedLoginException e) {
LOG.debug("Login failed", e);
return null;
} catch (AccountException e) {
LOG.warn("Account failure", e);
return null;
} catch (GeneralSecurityException e) {
LOG.error("General Security Exception", e);
return null;
}
}
Aggregations