<commit_before>package stroom.authentication.oauth2;

import stroom.security.impl.OpenIdClientDetailsFactory;

import javax.inject.Inject;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Optional;

public class OpenIdClientDetailsFactoryImpl implements OpenIdClientDetailsFactory {
    private static final String INTERNAL_STROOM_CLIENT = "Stroom Client Internal";

    private final String clientId;
    private final String clientSecret;

    @Inject
    public OpenIdClientDetailsFactoryImpl(final OAuth2ClientDao dao) {
        Optional<OAuth2Client> optionalClient = dao.getClientByName(INTERNAL_STROOM_CLIENT);
        if (optionalClient.isEmpty()) {
            final OAuth2Client client = new OAuth2Client(INTERNAL_STROOM_CLIENT, createRandomCode(40), createRandomCode(20), ".*");
            dao.create(client);
            optionalClient = dao.getClientByName(INTERNAL_STROOM_CLIENT);
        }

        if (optionalClient.isEmpty()) {
            throw new NullPointerException("Unable to get or create internal client details");
        }

        clientId = optionalClient.get().getClientId();
        clientSecret = optionalClient.get().getClientSecret();
    }

    @Override
    public String getClientId() {
        return clientId;
    }

    @Override
    public String getClientSecret() {
        return clientSecret;
    }

    private static String createRandomCode(final int length) {
        final SecureRandom secureRandom = new SecureRandom();
        final byte[] bytes = new byte[length];
        secureRandom.nextBytes(bytes);
        return Base64.getUrlEncoder().encodeToString(bytes);
    }
}<commit_msg>gh-1465 Format code, add suffix to client id<commit_after>package stroom.authentication.oauth2;

import org.apache.commons.codec.binary.Hex;
import stroom.security.impl.OpenIdClientDetailsFactory;

import javax.inject.Inject;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Optional;

public class OpenIdClientDetailsFactoryImpl implements OpenIdClientDetailsFactory {
    private static final String INTERNAL_STROOM_CLIENT = "Stroom Client Internal";

    private final String clientId;
    private final String clientSecret;

    @Inject
    public OpenIdClientDetailsFactoryImpl(final OAuth2ClientDao dao) {
        Optional<OAuth2Client> optionalClient = dao.getClientByName(INTERNAL_STROOM_CLIENT);
        if (optionalClient.isEmpty()) {
            // Generate new randomised client details and persist them
            final OAuth2Client client = new OAuth2Client(
                    INTERNAL_STROOM_CLIENT,
                    createRandomCode(40) + ".apps.stroom-idp",
                    createRandomCode(20),
                    ".*");
            dao.create(client);
            optionalClient = dao.getClientByName(INTERNAL_STROOM_CLIENT);
        }

        if (optionalClient.isEmpty()) {
            throw new NullPointerException("Unable to get or create internal client details");
        }

        clientId = optionalClient.get().getClientId();
        clientSecret = optionalClient.get().getClientSecret();
    }

    @Override
    public String getClientId() {
        return clientId;
    }

    @Override
    public String getClientSecret() {
        return clientSecret;
    }

    private static String createRandomCode(final int length) {
        final SecureRandom secureRandom = new SecureRandom();
        final byte[] bytes = new byte[length];
        secureRandom.nextBytes(bytes);
        return Base64.getUrlEncoder().encodeToString(bytes);
    }

    private static String createRandomHexCode(final int length) {
        final SecureRandom secureRandom = new SecureRandom();
        final byte[] bytes = new byte[length];
        secureRandom.nextBytes(bytes);
        return Hex.encodeHexString(bytes, true);
    }
}
{'fraction_non_alphanumeric': 0.08186070686070686, 'fraction_numerical': 0.007796257796257797, 'mean_word_length': 3.1655844155844157, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6895429', 'n_tokens_mistral': 1025, 'n_tokens_neox': 1000, 'n_words': 239}