use of javax.jcr.Credentials in project jackrabbit-oak by apache.
the class TokenLoginModule method login.
//--------------------------------------------------------< LoginModule >---
@Override
public boolean login() throws LoginException {
tokenProvider = getTokenProvider();
if (tokenProvider == null) {
return false;
}
Credentials credentials = getCredentials();
if (credentials instanceof TokenCredentials) {
TokenCredentials tc = (TokenCredentials) credentials;
TokenAuthentication authentication = new TokenAuthentication(tokenProvider);
if (authentication.authenticate(tc)) {
tokenCredentials = tc;
tokenInfo = authentication.getTokenInfo();
userId = authentication.getUserId();
principal = authentication.getUserPrincipal();
log.debug("Login: adding login name to shared state.");
sharedState.put(SHARED_KEY_LOGIN_NAME, userId);
return true;
}
}
return false;
}
use of javax.jcr.Credentials in project jackrabbit-oak by apache.
the class TokenProviderImpl method createToken.
/**
* Create a separate token node underneath a dedicated token store within
* the user home node. That token node contains the hashed token, the
* expiration time and additional mandatory attributes that will be verified
* during login.
*
* @param credentials The current credentials.
* @return A new {@code TokenInfo} or {@code null} if the token could not
* be created.
*/
@CheckForNull
@Override
public TokenInfo createToken(@Nonnull Credentials credentials) {
Credentials creds = extractCredentials(credentials);
String uid = (creds != null) ? credentialsSupport.getUserId(creds) : null;
TokenInfo tokenInfo = null;
if (uid != null) {
Map<String, ?> attributes = credentialsSupport.getAttributes(creds);
tokenInfo = createToken(uid, attributes);
if (tokenInfo != null) {
// also set the new token to the credentials.
if (!credentialsSupport.setAttributes(creds, ImmutableMap.of(TOKEN_ATTRIBUTE, tokenInfo.getToken()))) {
log.debug("Cannot set token attribute to " + creds);
}
}
}
return tokenInfo;
}
use of javax.jcr.Credentials in project jackrabbit by apache.
the class TestRepository method getIntegratedInstance.
/**
* Attempts to retrieve the test repository instance used by the
* Jackrabbit main test suite without having a direct dependency to any
* of the classes in src/test/java. This method assumes that we are
* running within the Jackrabbit main test suite if the AbstractJCRTest
* class is available. The initialized RepositoryHelper instance is
* retrieved from the static "helper" field of the AbstractJCRTest class,
* and the underlying repository and configured superuser credentials are
* extracted from the helper instance. This information is in turn used
* to create a custom Repository adapter that delegates calls to the
* underlying repository and uses the superuser credentials for the login
* methods where no credentials are passed by the client.
*
* @return test repository instance
* @throws Exception if the test repository could not be retrieved
*/
private static Repository getIntegratedInstance() throws Exception {
Class test = Class.forName("org.apache.jackrabbit.test.AbstractJCRTest");
Map helper = new BeanMap(test.getField("helper").get(null));
final Repository repository = (Repository) helper.get("repository");
final Credentials superuser = (Credentials) helper.get("superuserCredentials");
return new ProxyRepository(new RepositoryFactory() {
public Repository getRepository() throws RepositoryException {
return repository;
}
}) {
public Session login(String workspace) throws RepositoryException {
return repository.login(superuser, workspace);
}
public Session login() throws RepositoryException {
return repository.login(superuser);
}
};
}
use of javax.jcr.Credentials in project jackrabbit by apache.
the class AbstractLoginFilter method doFilter.
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpServletResponse httpResponse = (HttpServletResponse) response;
try {
Credentials credentials = getCredentials(httpRequest);
Session session = repository.login(credentials, workspace);
try {
request.setAttribute(sessionAttribute, session);
request.setAttribute(nodeAttribute, session.getRootNode());
chain.doFilter(request, response);
if (session.hasPendingChanges()) {
session.save();
}
} finally {
session.logout();
}
} catch (ServletException e) {
Throwable cause = e.getRootCause();
if (cause instanceof AccessDeniedException) {
httpResponse.sendError(HttpServletResponse.SC_FORBIDDEN, cause.getMessage());
} else {
throw e;
}
} catch (LoginException e) {
httpResponse.sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
} catch (NoSuchWorkspaceException e) {
throw new ServletException("Workspace " + workspace + " not found in the content repository", e);
} catch (RepositoryException e) {
throw new ServletException("Unable to access the content repository", e);
}
}
use of javax.jcr.Credentials in project jackrabbit by apache.
the class BasicCredentialsProviderTest method testDefaultPassword.
public void testDefaultPassword() throws ServletException, LoginException {
Map<String, char[]> m = new HashMap<String, char[]>();
m.put("userId", new char[0]);
m.put("userId:", new char[0]);
m.put("userId:pw", "pw".toCharArray());
for (String uid : m.keySet()) {
char[] pw = m.get(uid);
CredentialsProvider cb = new BasicCredentialsProvider(uid);
Credentials creds = cb.getCredentials(new RequestImpl(null));
assertNotNull(creds);
assertTrue(creds instanceof SimpleCredentials);
assertEquals("userId", ((SimpleCredentials) creds).getUserID());
if (pw.length == 0) {
assertEquals(0, ((SimpleCredentials) creds).getPassword().length);
} else {
assertEquals(new String(pw), new String(((SimpleCredentials) creds).getPassword()));
}
}
}
Aggregations