Search in sources :

Example 1 with GuestCredentials

use of javax.jcr.GuestCredentials 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());
    }
}
Also used : ServletException(javax.servlet.ServletException) SimpleCredentials(javax.jcr.SimpleCredentials) LoginException(javax.jcr.LoginException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) GuestCredentials(javax.jcr.GuestCredentials)

Example 2 with GuestCredentials

use of javax.jcr.GuestCredentials 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
        }
    }
}
Also used : SimpleCredentials(javax.jcr.SimpleCredentials) LoginException(javax.security.auth.login.LoginException) GuestCredentials(javax.jcr.GuestCredentials) GuestCredentials(javax.jcr.GuestCredentials) SimpleCredentials(javax.jcr.SimpleCredentials) Credentials(javax.jcr.Credentials) Test(org.junit.Test)

Example 3 with GuestCredentials

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

the class DefaultLdapLoginModuleTest method testGuestLogin.

/**
     * Login with {@link javax.jcr.GuestCredentials} must succeed and result in
     * an guest session as the SUFFICIENT
     * {@link org.apache.jackrabbit.oak.security.authentication.user.LoginModuleImpl}
     * handles the guest login (in contrast to the ExternalLoginModule).
     *
     * @throws Exception
     */
@Test
public void testGuestLogin() throws Exception {
    ContentSession cs = login(new GuestCredentials());
    assertEquals(UserConstants.DEFAULT_ANONYMOUS_ID, cs.getAuthInfo().getUserID());
    cs.close();
}
Also used : ContentSession(org.apache.jackrabbit.oak.api.ContentSession) GuestCredentials(javax.jcr.GuestCredentials) Test(org.junit.Test)

Example 4 with GuestCredentials

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

the class LdapDefaultLoginModuleTest method testGuestLogin.

/**
     * Login with {@link javax.jcr.GuestCredentials} must succeed and result in
     * an guest session as the SUFFICIENT
     * {@link org.apache.jackrabbit.oak.security.authentication.user.LoginModuleImpl}
     * handles the guest login (in contrast to the ExternalLoginModule).
     *
     * @throws Exception
     */
@Test
public void testGuestLogin() throws Exception {
    ContentSession cs = login(new GuestCredentials());
    assertEquals(UserConstants.DEFAULT_ANONYMOUS_ID, cs.getAuthInfo().getUserID());
    cs.close();
}
Also used : ContentSession(org.apache.jackrabbit.oak.api.ContentSession) GuestCredentials(javax.jcr.GuestCredentials) Test(org.junit.Test)

Example 5 with GuestCredentials

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

the class LdapLoginStandaloneTest method testGuestLogin.

@Test
public void testGuestLogin() throws Exception {
    try {
        ContentSession sc = login(new GuestCredentials());
        sc.close();
        fail("Guest login must fail.");
    } catch (LoginException e) {
    // success
    }
}
Also used : ContentSession(org.apache.jackrabbit.oak.api.ContentSession) LoginException(javax.security.auth.login.LoginException) GuestCredentials(javax.jcr.GuestCredentials) Test(org.junit.Test)

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