Search in sources :

Example 11 with Credentials

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;
}
Also used : TokenCredentials(org.apache.jackrabbit.api.security.authentication.token.TokenCredentials) Credentials(javax.jcr.Credentials) TokenCredentials(org.apache.jackrabbit.api.security.authentication.token.TokenCredentials)

Example 12 with Credentials

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;
}
Also used : TokenInfo(org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo) TokenCredentials(org.apache.jackrabbit.api.security.authentication.token.TokenCredentials) ImpersonationCredentials(org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials) Credentials(javax.jcr.Credentials) CheckForNull(javax.annotation.CheckForNull)

Example 13 with Credentials

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);
        }
    };
}
Also used : BeanMap(org.apache.commons.collections.BeanMap) Repository(javax.jcr.Repository) ProxyRepository(org.apache.jackrabbit.commons.repository.ProxyRepository) ProxyRepository(org.apache.jackrabbit.commons.repository.ProxyRepository) RepositoryException(javax.jcr.RepositoryException) RepositoryFactory(org.apache.jackrabbit.commons.repository.RepositoryFactory) Map(java.util.Map) BeanMap(org.apache.commons.collections.BeanMap) Credentials(javax.jcr.Credentials)

Example 14 with Credentials

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);
    }
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) NoSuchWorkspaceException(javax.jcr.NoSuchWorkspaceException) AccessDeniedException(javax.jcr.AccessDeniedException) HttpServletResponse(javax.servlet.http.HttpServletResponse) LoginException(javax.jcr.LoginException) RepositoryException(javax.jcr.RepositoryException) Credentials(javax.jcr.Credentials) Session(javax.jcr.Session)

Example 15 with Credentials

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()));
        }
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) HashMap(java.util.HashMap) GuestCredentials(javax.jcr.GuestCredentials) SimpleCredentials(javax.jcr.SimpleCredentials) Credentials(javax.jcr.Credentials)

Aggregations

Credentials (javax.jcr.Credentials)86 SimpleCredentials (javax.jcr.SimpleCredentials)53 Test (org.junit.Test)33 GuestCredentials (javax.jcr.GuestCredentials)26 Session (javax.jcr.Session)17 TokenCredentials (org.apache.jackrabbit.api.security.authentication.token.TokenCredentials)14 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)13 RepositoryException (javax.jcr.RepositoryException)12 User (org.apache.jackrabbit.api.security.user.User)12 ImpersonationCredentials (org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials)12 LoginException (javax.security.auth.login.LoginException)8 ArrayList (java.util.ArrayList)7 LoginException (javax.jcr.LoginException)6 Subject (javax.security.auth.Subject)6 IOException (java.io.IOException)5 HashMap (java.util.HashMap)5 Repository (javax.jcr.Repository)5 ContentSession (org.apache.jackrabbit.oak.api.ContentSession)5 Principal (java.security.Principal)4 Map (java.util.Map)4