Search in sources :

Example 6 with Credentials

use of javax.jcr.Credentials in project jackrabbit-oak by apache.

the class OpenAuthenticationConfiguration method getLoginContextProvider.

@Nonnull
@Override
public LoginContextProvider getLoginContextProvider(ContentRepository contentRepository) {
    return new LoginContextProvider() {

        @Nonnull
        @Override
        public LoginContext getLoginContext(final Credentials credentials, String workspaceName) {
            return new LoginContext() {

                @Override
                public Subject getSubject() {
                    Subject subject = new Subject();
                    if (credentials != null) {
                        subject.getPrivateCredentials().add(credentials);
                    }
                    subject.setReadOnly();
                    return subject;
                }

                @Override
                public void login() {
                // do nothing
                }

                @Override
                public void logout() {
                // do nothing
                }
            };
        }
    };
}
Also used : Credentials(javax.jcr.Credentials) Subject(javax.security.auth.Subject) Nonnull(javax.annotation.Nonnull)

Example 7 with Credentials

use of javax.jcr.Credentials in project jackrabbit-oak by apache.

the class AbstractLoginModule method getCredentials.

/**
     * Tries to retrieve valid (supported) Credentials:
     * <ol>
     * <li>using a {@link CredentialsCallback},</li>
     * <li>looking for a {@link #SHARED_KEY_CREDENTIALS} entry in the
     * shared state (see also {@link #getSharedCredentials()} and finally by</li>
     * <li>searching for valid credentials in the subject.</li>
     * </ol>
     *
     * @return Valid (supported) credentials or {@code null}.
     */
@CheckForNull
protected Credentials getCredentials() {
    Set<Class> supported = getSupportedCredentials();
    if (callbackHandler != null) {
        log.debug("Login: retrieving Credentials using callback.");
        try {
            CredentialsCallback callback = new CredentialsCallback();
            callbackHandler.handle(new Callback[] { callback });
            Credentials creds = callback.getCredentials();
            if (creds != null && supported.contains(creds.getClass())) {
                log.debug("Login: Credentials '{}' obtained from callback", creds);
                return creds;
            } else {
                log.debug("Login: No supported credentials obtained from callback; trying shared state.");
            }
        } catch (UnsupportedCallbackException e) {
            log.warn(e.getMessage());
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }
    Credentials creds = getSharedCredentials();
    if (creds != null && supported.contains(creds.getClass())) {
        log.debug("Login: Credentials obtained from shared state.");
        return creds;
    } else {
        log.debug("Login: No supported credentials found in shared state; looking for credentials in subject.");
        for (Class clz : getSupportedCredentials()) {
            Set<Credentials> cds = subject.getPublicCredentials(clz);
            if (!cds.isEmpty()) {
                log.debug("Login: Credentials found in subject.");
                return cds.iterator().next();
            }
        }
    }
    log.debug("No credentials found.");
    return null;
}
Also used : UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) Credentials(javax.jcr.Credentials) CredentialsCallback(org.apache.jackrabbit.oak.spi.security.authentication.callback.CredentialsCallback) CheckForNull(javax.annotation.CheckForNull)

Example 8 with Credentials

use of javax.jcr.Credentials in project jackrabbit by apache.

the class ConcurrentLoginTest method testLogin.

/**
     * Tests concurrent logins on the Repository.
     */
public void testLogin() throws RepositoryException {
    final Exception[] exception = new Exception[1];
    List testRunner = new ArrayList();
    for (int i = 0; i < NUM_THREADS; i++) {
        testRunner.add(new Thread(new Runnable() {

            public void run() {
                Credentials cred = getHelper().getSuperuserCredentials();
                for (int i = 0; i < NUM_LOGINS_PER_THREAD; i++) {
                    try {
                        Session s = getHelper().getRepository().login(cred);
                        // immediately logout
                        s.logout();
                    } catch (Exception e) {
                        exception[0] = e;
                        break;
                    }
                }
            }
        }));
    }
    // start threads
    for (Iterator it = testRunner.iterator(); it.hasNext(); ) {
        ((Thread) it.next()).start();
    }
    // join threads
    for (Iterator it = testRunner.iterator(); it.hasNext(); ) {
        try {
            ((Thread) it.next()).join();
        } catch (InterruptedException e) {
            fail(e.toString());
        }
    }
    if (exception[0] != null) {
        fail(exception[0].toString());
    }
}
Also used : ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) Credentials(javax.jcr.Credentials) Session(javax.jcr.Session)

Example 9 with Credentials

use of javax.jcr.Credentials in project jackrabbit by apache.

the class UserTest method testUserHasCredentials.

public void testUserHasCredentials() throws RepositoryException, NotExecutableException {
    User user = getTestUser(superuser);
    Credentials creds = user.getCredentials();
    assertTrue(creds != null);
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) Credentials(javax.jcr.Credentials)

Example 10 with Credentials

use of javax.jcr.Credentials in project jackrabbit-oak by apache.

the class LoginModuleImpl method createAuthInfo.

private AuthInfo createAuthInfo(@Nonnull Set<? extends Principal> principals) {
    Credentials creds;
    if (credentials instanceof ImpersonationCredentials) {
        creds = ((ImpersonationCredentials) credentials).getBaseCredentials();
    } else {
        creds = credentials;
    }
    Map<String, Object> attributes = new HashMap<String, Object>();
    Object shared = sharedState.get(SHARED_KEY_ATTRIBUTES);
    if (shared instanceof Map) {
        for (Object key : ((Map) shared).keySet()) {
            attributes.put(key.toString(), ((Map) shared).get(key));
        }
    } else if (creds instanceof SimpleCredentials) {
        SimpleCredentials sc = (SimpleCredentials) creds;
        for (String attrName : sc.getAttributeNames()) {
            attributes.put(attrName, sc.getAttribute(attrName));
        }
    }
    return new AuthInfoImpl(userId, attributes, principals);
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) AuthInfoImpl(org.apache.jackrabbit.oak.spi.security.authentication.AuthInfoImpl) ImpersonationCredentials(org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) GuestCredentials(javax.jcr.GuestCredentials) ImpersonationCredentials(org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials) 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