use of javax.jcr.SimpleCredentials in project camel by apache.
the class JcrAuthTestBase method createJndiContext.
@Override
protected Context createJndiContext() throws Exception {
Context context = super.createJndiContext();
repository = new TransientRepository(new File(REPO_PATH));
// set up a user to authenticate
SessionImpl session = (SessionImpl) repository.login(new SimpleCredentials("admin", "admin".toCharArray()));
UserManager userManager = session.getUserManager();
User user = (User) userManager.getAuthorizable("test");
if (user == null) {
user = userManager.createUser("test", "quatloos");
}
// set up permissions
String path = session.getRootNode().getPath();
AccessControlManager accessControlManager = session.getAccessControlManager();
AccessControlPolicyIterator acls = accessControlManager.getApplicablePolicies(path);
AccessControlList acl = null;
if (acls.hasNext()) {
acl = (AccessControlList) acls.nextAccessControlPolicy();
} else {
acl = (AccessControlList) accessControlManager.getPolicies(path)[0];
}
acl.addAccessControlEntry(user.getPrincipal(), accessControlManager.getSupportedPrivileges(path));
accessControlManager.setPolicy(path, acl);
session.save();
session.logout();
context.bind("repository", repository);
return context;
}
use of javax.jcr.SimpleCredentials in project jackrabbit by apache.
the class SessionImpl method impersonate.
/**
* {@inheritDoc}
*/
@Override
public Session impersonate(Credentials otherCredentials) throws LoginException, RepositoryException {
// check sanity of this session
sanityCheck();
if (!(otherCredentials instanceof SimpleCredentials)) {
String msg = "impersonate failed: incompatible credentials, SimpleCredentials expected";
log.debug(msg);
throw new RepositoryException(msg);
}
// set IMPERSONATOR_ATTRIBUTE attribute of given credentials
// with subject of current session
SimpleCredentials creds = (SimpleCredentials) otherCredentials;
creds.setAttribute(SecurityConstants.IMPERSONATOR_ATTRIBUTE, subject);
try {
return getRepository().login(otherCredentials, getWorkspace().getName());
} catch (NoSuchWorkspaceException nswe) {
// should never get here...
String msg = "impersonate failed";
log.error(msg, nswe);
throw new RepositoryException(msg, nswe);
} finally {
// make sure IMPERSONATOR_ATTRIBUTE is removed
creds.removeAttribute(SecurityConstants.IMPERSONATOR_ATTRIBUTE);
}
}
use of javax.jcr.SimpleCredentials in project jackrabbit by apache.
the class BasicCredentialsProvider method getCredentials.
/**
* {@inheritDoc}
*
* Build a {@link Credentials} object for the given authorization header.
* The creds may be used to login to the repository. If the specified header
* string is <code>null</code> the behaviour depends on the
* {@link #defaultHeaderValue} field:<br>
* <ul>
* <li> if this field is <code>null</code>, a LoginException is thrown.
* This is suitable for clients (eg. webdav clients) for with
* sending a proper authorization header is not possible, if the
* server never send a 401.
* <li> if this an empty string, null-credentials are returned, thus
* forcing an null login on the repository
* <li> if this field has a 'user:password' value, the respective
* simple credentials are generated.
* </ul>
* <p>
* If the request header is present but cannot be parsed a
* <code>ServletException</code> is thrown.
*
* @param request the servlet request
* @return credentials or <code>null</code>.
* @throws ServletException If the Authorization header cannot be decoded.
* @throws LoginException if no suitable auth header and missing-auth-mapping
* is not present
*/
public Credentials getCredentials(HttpServletRequest request) throws LoginException, ServletException {
try {
String authHeader = request.getHeader(DavConstants.HEADER_AUTHORIZATION);
if (authHeader != null) {
String[] authStr = authHeader.split(" ");
if (authStr.length >= 2 && authStr[0].equalsIgnoreCase(HttpServletRequest.BASIC_AUTH)) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
Base64.decode(authStr[1].toCharArray(), out);
String decAuthStr = out.toString("ISO-8859-1");
int pos = decAuthStr.indexOf(':');
String userid = decAuthStr.substring(0, pos);
String passwd = decAuthStr.substring(pos + 1);
return new SimpleCredentials(userid, passwd.toCharArray());
}
throw new ServletException("Unable to decode authorization.");
} else {
// check special handling
if (defaultHeaderValue == null) {
throw new LoginException();
} else if (EMPTY_DEFAULT_HEADER_VALUE.equals(defaultHeaderValue)) {
return null;
} else if (GUEST_DEFAULT_HEADER_VALUE.equals(defaultHeaderValue)) {
return new GuestCredentials();
} else {
int pos = defaultHeaderValue.indexOf(':');
if (pos < 0) {
return new SimpleCredentials(defaultHeaderValue, new char[0]);
} else {
return new SimpleCredentials(defaultHeaderValue.substring(0, pos), defaultHeaderValue.substring(pos + 1).toCharArray());
}
}
}
} catch (IOException e) {
throw new ServletException("Unable to decode authorization: " + e.toString());
}
}
use of javax.jcr.SimpleCredentials in project jackrabbit by apache.
the class Login method execute.
/**
* {@inheritDoc}
*/
public boolean execute(Context ctx) throws Exception {
String anon = "anonymous";
String user = (String) ctx.get(this.userKey);
String password = (String) ctx.get(this.passwordKey);
String workspace = (String) ctx.get(this.workspaceKey);
if (user == null) {
user = anon;
}
if (password == null || (password.equals(anon) && !user.equals(anon))) {
ConsoleReader reader = new ConsoleReader();
password = reader.readLine("Password: ", (char) 0);
}
if (log.isDebugEnabled()) {
log.debug("logging in as " + user);
}
Session session = null;
Repository repo = CommandHelper.getRepository(ctx);
Credentials credentials = new SimpleCredentials(user, password.toCharArray());
if (workspace == null) {
session = repo.login(credentials);
} else {
session = repo.login(credentials, workspace);
}
CommandHelper.setSession(ctx, session);
CommandHelper.setCurrentNode(ctx, session.getRootNode());
return false;
}
use of javax.jcr.SimpleCredentials in project jackrabbit-oak by apache.
the class CustomCredentialsSupportTest method testLoginWithUnsupportedCredentials.
@Test
public void testLoginWithUnsupportedCredentials() throws Exception {
List<Credentials> creds = ImmutableList.of(new SimpleCredentials("testUser", new char[0]), new GuestCredentials());
for (Credentials c : creds) {
try {
login(c).close();
fail("login must fail for credentials " + c);
} catch (LoginException e) {
// success
}
}
}
Aggregations