<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.core.security.authentication.token;

import java.util.UUID;
import javax.jcr.Credentials;
import javax.jcr.ItemNotFoundException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.SimpleCredentials;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.version.VersionException;

import org.apache.jackrabbit.api.security.authentication.token.TokenCredentials;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.core.SessionImpl;
import org.apache.jackrabbit.test.AbstractJCRTest;

/**
 * <code>TokenBasedAuthenticationOakTest</code>...
 */
public class TokenBasedAuthenticationTest extends AbstractJCRTest {

    private SessionImpl adminSession;
    private User testUser;

    private String token;
    private Node tokenNode;
    private TokenCredentials tokenCreds;

    private String expiredToken;
    private Node expiredNode;
    private TokenCredentials expiredCreds;


    private TokenBasedAuthentication nullTokenAuth;
    private TokenBasedAuthentication validTokenAuth;

    private Credentials simpleCreds = new SimpleCredentials("uid", "pw".toCharArray());
    private Credentials creds = new Credentials() {};

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        adminSession = (SessionImpl) getHelper().getSuperuserSession("security");
        testUser = adminSession.getUserManager().createUser(UUID.randomUUID().toString(), "pw");
        adminSession.save();

        SimpleCredentials sc = new SimpleCredentials(testUser.getID(), "pw".toCharArray());
        sc.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE, "");
        sc.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE+".any", "correct");
        sc.setAttribute("informative", "value");

        TokenProvider tp = new TokenProvider(adminSession, TokenBasedAuthentication.TOKEN_EXPIRATION);
        TokenInfo ti = tp.createToken(testUser, sc);
        tokenCreds = ti.getCredentials();
        token = tokenCreds.getToken();
        tokenNode = TokenProvider.getTokenNode(token, adminSession);

        tp = new TokenProvider(adminSession, 1);
        TokenInfo expired = tp.createToken(testUser, sc);
        expiredCreds = expired.getCredentials();
        expiredToken = expiredCreds.getToken();
        expiredNode = TokenProvider.getTokenNode(expiredToken, adminSession);

        nullTokenAuth = new TokenBasedAuthentication(null, -1, adminSession);
        validTokenAuth = new TokenBasedAuthentication(token, 7200, adminSession);

    }

    @Override
    protected void tearDown() throws Exception {
        try {
            testUser.remove();
            adminSession.save();
            adminSession.logout();
        } finally {
            super.tearDown();
        }<commit_msg>JCR-3710: occasional test failures in TokenBasedAuthenticationTest<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.core.security.authentication.token;

import java.util.UUID;
import javax.jcr.Credentials;
import javax.jcr.ItemNotFoundException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.SimpleCredentials;
import javax.jcr.lock.LockException;
import javax.jcr.nodetype.ConstraintViolationException;
import javax.jcr.version.VersionException;

import org.apache.jackrabbit.api.security.authentication.token.TokenCredentials;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.core.SessionImpl;
import org.apache.jackrabbit.test.AbstractJCRTest;

/**
 * <code>TokenBasedAuthenticationOakTest</code>...
 */
public class TokenBasedAuthenticationTest extends AbstractJCRTest {

    private SessionImpl adminSession;
    private User testUser;

    private String token;
    private Node tokenNode;
    private TokenCredentials tokenCreds;

    private String expiredToken;
    private Node expiredNode;
    private TokenCredentials expiredCreds;


    private TokenBasedAuthentication nullTokenAuth;
    private TokenBasedAuthentication validTokenAuth;

    private Credentials simpleCreds = new SimpleCredentials("uid", "pw".toCharArray());
    private Credentials creds = new Credentials() {};

    @Override
    protected void setUp() throws Exception {
        super.setUp();

        adminSession = (SessionImpl) getHelper().getSuperuserSession("security");
        testUser = adminSession.getUserManager().createUser(UUID.randomUUID().toString(), "pw");
        adminSession.save();

        SimpleCredentials sc = new SimpleCredentials(testUser.getID(), "pw".toCharArray());
        sc.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE, "");
        sc.setAttribute(TokenBasedAuthentication.TOKEN_ATTRIBUTE+".any", "correct");
        sc.setAttribute("informative", "value");

        TokenProvider tp = new TokenProvider(adminSession, TokenBasedAuthentication.TOKEN_EXPIRATION);
        TokenInfo ti = tp.createToken(testUser, sc);
        tokenCreds = ti.getCredentials();
        token = tokenCreds.getToken();
        tokenNode = TokenProvider.getTokenNode(token, adminSession);

        long ttl = 1; // 1ms expiration
        tp = new TokenProvider(adminSession, ttl);
        TokenInfo expired = tp.createToken(testUser, sc);
        expiredCreds = expired.getCredentials();
        expiredToken = expiredCreds.getToken();
        long tokenWillExpireAfter = System.currentTimeMillis() + ttl;
        expiredNode = TokenProvider.getTokenNode(expiredToken, adminSession);

        nullTokenAuth = new TokenBasedAuthentication(null, -1, adminSession);
        validTokenAuth = new TokenBasedAuthentication(token, 7200, adminSession);

        while (System.currentTimeMillis() <= tokenWillExpireAfter) {
            // wait until the token is actually expired
        }
    }

    @Override
    protected void tearDown() throws Exception {
        try {
            testUser.remove();
            adminSession.save();
            adminSession.logout();
        } finally {
            super.tearDown();
        }
{'fraction_non_alphanumeric': 0.08871808233454924, 'fraction_numerical': 0.0032569046378322043, 'mean_word_length': 3.846590909090909, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7256023', 'n_tokens_mistral': 1980, 'n_tokens_neox': 1955, 'n_words': 554}