Search in sources :

Example 46 with Credentials

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

the class GroupAdministratorTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    // create a first user
    Principal p = getTestPrincipal();
    UserImpl pUser = (UserImpl) userMgr.createUser(p.getName(), buildPassword(p));
    save(superuser);
    otherUID = pUser.getID();
    // create a second user and make it group-admin
    p = getTestPrincipal();
    String pw = buildPassword(p);
    Credentials creds = buildCredentials(p.getName(), pw);
    User user = userMgr.createUser(p.getName(), pw);
    save(superuser);
    uID = user.getID();
    // make other user a group-administrator:
    Authorizable grAdmin = userMgr.getAuthorizable(UserConstants.GROUP_ADMIN_GROUP_NAME);
    if (grAdmin == null || !grAdmin.isGroup()) {
        throw new NotExecutableException("Cannot execute test. No group-administrator group found.");
    }
    groupAdmin = (Group) grAdmin;
    groupAdmin.addMember(user);
    save(superuser);
    grID = groupAdmin.getID();
    // create a session for the group-admin user.
    uSession = getHelper().getRepository().login(creds);
    groupsPath = (userMgr instanceof UserManagerImpl) ? ((UserManagerImpl) userMgr).getGroupsPath() : UserConstants.GROUPS_PATH;
}
Also used : User(org.apache.jackrabbit.api.security.user.User) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Authorizable(org.apache.jackrabbit.api.security.user.Authorizable) Principal(java.security.Principal) Credentials(javax.jcr.Credentials)

Example 47 with Credentials

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

the class BasicCredentialsProviderTest method testEmptyDefaultHeader.

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

Example 48 with Credentials

use of javax.jcr.Credentials 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 49 with Credentials

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

the class TokenLoginModule method commit.

@Override
public boolean commit() throws LoginException {
    if (tokenCredentials != null && userId != null) {
        Set<? extends Principal> principals = (principal != null) ? getPrincipals(principal) : getPrincipals(userId);
        updateSubject(tokenCredentials, getAuthInfo(tokenInfo, principals), principals);
        return true;
    }
    try {
        if (tokenProvider != null && sharedState.containsKey(SHARED_KEY_CREDENTIALS)) {
            Credentials shared = getSharedCredentials();
            if (shared != null && tokenProvider.doCreateToken(shared)) {
                Root r = getRoot();
                if (r != null) {
                    // refresh root, in case the external login module created users
                    r.refresh();
                }
                TokenInfo ti = tokenProvider.createToken(shared);
                if (ti != null) {
                    TokenCredentials tc = new TokenCredentials(ti.getToken());
                    Map<String, String> attributes = ti.getPrivateAttributes();
                    for (String name : attributes.keySet()) {
                        tc.setAttribute(name, attributes.get(name));
                    }
                    attributes = ti.getPublicAttributes();
                    for (String name : attributes.keySet()) {
                        tc.setAttribute(name, attributes.get(name));
                    }
                    sharedState.put(SHARED_KEY_ATTRIBUTES, attributes);
                    updateSubject(tc, null, null);
                } else {
                    // failed to create token -> fail commit()
                    Object logId = (userId != null) ? userId : sharedState.get(SHARED_KEY_LOGIN_NAME);
                    log.debug("TokenProvider failed to create a login token for user " + logId);
                    throw new LoginException("Failed to create login token for user " + logId);
                }
            }
        }
    } finally {
        // the login attempt on this module did not succeed: clear state
        clearState();
    }
    return false;
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) LoginException(javax.security.auth.login.LoginException) TokenInfo(org.apache.jackrabbit.oak.spi.security.authentication.token.TokenInfo) TokenCredentials(org.apache.jackrabbit.api.security.authentication.token.TokenCredentials) Credentials(javax.jcr.Credentials) TokenCredentials(org.apache.jackrabbit.api.security.authentication.token.TokenCredentials)

Example 50 with Credentials

use of javax.jcr.Credentials 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)

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