Search in sources :

Example 21 with GuestCredentials

use of javax.jcr.GuestCredentials in project sling by apache.

the class AbstractSlingRepository2 method login.

/**
     * Logs into the repository at the given {@code workspace} with the given
     * {@code credentials} and returns the session returned from
     * the repository.
     * <p>
     * This method logs in as a guest if {@code null} credentials are provided.
     * The method may be overwritten to implement a different behaviour as
     * indicated by the JCR specification for this method to use external
     * mechanisms to login instead of leveraging provided credentials.
     * <p>
     * This method may be overwritten.
     *
     * @param credentials The {@code Credentials} to use to login. If this is
     *            {@code null} JCR {@code GuestCredentials} are used to login.
     * @param workspace The workspace to access or {@code null} to access the
     *            {@link #getDefaultWorkspace() default workspace}
     * @throws LoginException If login is not possible
     * @throws NoSuchWorkspaceException if the desired workspace is not
     *             available
     * @throws RepositoryException If another error occurrs during login
     * @see #getNamespaceAwareSession(Session)
     */
@Override
public Session login(Credentials credentials, String workspace) throws LoginException, NoSuchWorkspaceException, RepositoryException {
    if (credentials == null) {
        credentials = new GuestCredentials();
    }
    // check the workspace
    if (workspace == null) {
        workspace = this.getDefaultWorkspace();
    }
    try {
        logger.debug("login: Logging in to workspace '" + workspace + "'");
        final Repository repository = this.getRepository();
        if (this.getRepository() == null) {
            throw new RepositoryException("Sling Repository not ready");
        }
        final Session session = repository.login(credentials, workspace);
        return session;
    } catch (final RuntimeException re) {
        // repository has already been shut down ...
        throw new RepositoryException(re.getMessage(), re);
    }
}
Also used : SlingRepository(org.apache.sling.jcr.api.SlingRepository) Repository(javax.jcr.Repository) RepositoryException(javax.jcr.RepositoryException) GuestCredentials(javax.jcr.GuestCredentials) Session(javax.jcr.Session)

Example 22 with GuestCredentials

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

the class BasicCredentialsProviderTest method testGuestCredentialsDefaultHeader.

public void testGuestCredentialsDefaultHeader() throws ServletException, LoginException {
    CredentialsProvider cb = new BasicCredentialsProvider(BasicCredentialsProvider.GUEST_DEFAULT_HEADER_VALUE);
    Credentials creds = cb.getCredentials(new RequestImpl(null));
    assertTrue(creds instanceof GuestCredentials);
}
Also used : GuestCredentials(javax.jcr.GuestCredentials) SimpleCredentials(javax.jcr.SimpleCredentials) Credentials(javax.jcr.Credentials) GuestCredentials(javax.jcr.GuestCredentials)

Example 23 with GuestCredentials

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

the class LoginModuleImpl method getLoginId.

//--------------------------------------------------------------------------
@CheckForNull
private String getLoginId(@CheckForNull PreAuthenticatedLogin preAuthenticatedLogin) {
    if (preAuthenticatedLogin != null) {
        return preAuthenticatedLogin.getUserId();
    }
    String uid = null;
    if (credentials != null) {
        if (credentials instanceof SimpleCredentials) {
            uid = ((SimpleCredentials) credentials).getUserID();
        } else if (credentials instanceof GuestCredentials) {
            uid = getAnonymousId();
        } else if (credentials instanceof ImpersonationCredentials) {
            Credentials bc = ((ImpersonationCredentials) credentials).getBaseCredentials();
            if (bc instanceof SimpleCredentials) {
                uid = ((SimpleCredentials) bc).getUserID();
            }
        } else {
            try {
                NameCallback callback = new NameCallback("User-ID: ");
                callbackHandler.handle(new Callback[] { callback });
                uid = callback.getName();
            } catch (UnsupportedCallbackException e) {
                log.warn("Credentials- or NameCallback must be supported");
            } catch (IOException e) {
                log.error("Name-Callback failed: " + e.getMessage());
            }
        }
    }
    if (uid == null) {
        uid = getSharedLoginName();
    }
    return uid;
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) NameCallback(javax.security.auth.callback.NameCallback) ImpersonationCredentials(org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) IOException(java.io.IOException) GuestCredentials(javax.jcr.GuestCredentials) GuestCredentials(javax.jcr.GuestCredentials) ImpersonationCredentials(org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials) SimpleCredentials(javax.jcr.SimpleCredentials) Credentials(javax.jcr.Credentials) CheckForNull(javax.annotation.CheckForNull)

Example 24 with GuestCredentials

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

the class GuestLoginModule method login.

@Override
public boolean login() {
    if (callbackHandler != null) {
        CredentialsCallback ccb = new CredentialsCallback();
        try {
            callbackHandler.handle(new Callback[] { ccb });
            Credentials credentials = ccb.getCredentials();
            if (credentials == null) {
                guestCredentials = new GuestCredentials();
                sharedState.put(AbstractLoginModule.SHARED_KEY_CREDENTIALS, guestCredentials);
                return true;
            }
        } catch (IOException e) {
            log.debug("Login: Failed to retrieve Credentials from CallbackHandler", e);
        } catch (UnsupportedCallbackException e) {
            log.debug("Login: Failed to retrieve Credentials from CallbackHandler", e);
        }
    }
    // ignore this login module
    return false;
}
Also used : IOException(java.io.IOException) UnsupportedCallbackException(javax.security.auth.callback.UnsupportedCallbackException) GuestCredentials(javax.jcr.GuestCredentials) Credentials(javax.jcr.Credentials) GuestCredentials(javax.jcr.GuestCredentials) CredentialsCallback(org.apache.jackrabbit.oak.spi.security.authentication.callback.CredentialsCallback)

Example 25 with GuestCredentials

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

the class CugOakTest method beforeSuite.

@Override
protected void beforeSuite() throws Exception {
    super.beforeSuite();
    Credentials creds = (runAsAdmin) ? getCredentials() : new GuestCredentials();
    cs = contentRepository.login(creds, null);
    subject = new Subject(true, cs.getAuthInfo().getPrincipals(), Collections.emptySet(), Collections.emptySet());
}
Also used : GuestCredentials(javax.jcr.GuestCredentials) Credentials(javax.jcr.Credentials) GuestCredentials(javax.jcr.GuestCredentials) Subject(javax.security.auth.Subject)

Aggregations

GuestCredentials (javax.jcr.GuestCredentials)39 Test (org.junit.Test)25 SimpleCredentials (javax.jcr.SimpleCredentials)13 Credentials (javax.jcr.Credentials)12 ContentSession (org.apache.jackrabbit.oak.api.ContentSession)12 AbstractSecurityTest (org.apache.jackrabbit.oak.AbstractSecurityTest)10 Session (javax.jcr.Session)8 LoginException (javax.security.auth.login.LoginException)7 TokenCredentials (org.apache.jackrabbit.api.security.authentication.token.TokenCredentials)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Subject (javax.security.auth.Subject)4 AuthInfo (org.apache.jackrabbit.oak.api.AuthInfo)4 ImpersonationCredentials (org.apache.jackrabbit.oak.spi.security.authentication.ImpersonationCredentials)4 ArrayList (java.util.ArrayList)3 Repository (javax.jcr.Repository)3 UnsupportedCallbackException (javax.security.auth.callback.UnsupportedCallbackException)3 Map (java.util.Map)2 RepositoryException (javax.jcr.RepositoryException)2 AccessControlManager (javax.jcr.security.AccessControlManager)2