<commit_before>/*
 * Copyright 2015 <NAME>.
 *
 * Licensed 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 tech.aroma.banana.authentication.service.operations;

import java.time.Duration;
import java.time.Instant;
import java.util.function.Function;
import javax.inject.Inject;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.aroma.banana.authentication.service.data.TokenCreator;
import tech.aroma.banana.data.TokenRepository;
import tech.aroma.banana.thrift.LengthOfTime;
import tech.aroma.banana.thrift.TimeUnit;
import tech.aroma.banana.thrift.authentication.AuthenticationToken;
import tech.aroma.banana.thrift.authentication.service.CreateTokenRequest;
import tech.aroma.banana.thrift.authentication.service.CreateTokenResponse;
import tech.aroma.banana.thrift.exceptions.OperationFailedException;
import tech.sirwellington.alchemy.annotations.access.Internal;
import tech.sirwellington.alchemy.thrift.operations.ThriftOperation;

import static java.time.Instant.now;
import static tech.aroma.banana.thrift.assertions.BananaAssertions.checkRequestNotNull;
import static tech.aroma.banana.thrift.assertions.BananaAssertions.withMessage;
import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;
import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;
import static tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString;
import static tech.sirwellington.alchemy.arguments.assertions.StringAssertions.stringWithLengthGreaterThanOrEqualTo;
import static tech.sirwellington.alchemy.arguments.assertions.StringAssertions.validUUID;

/**
 *
 * @author SirWellington
 */
@Internal
final class CreateTokenOperation implements ThriftOperation<CreateTokenRequest, CreateTokenResponse>
{

    private final static Logger LOG = LoggerFactory.getLogger(CreateTokenOperation.class);
    private final static LengthOfTime DEFAULT_LIFETIME = new LengthOfTime(TimeUnit.DAYS, 60);

    private final Function<LengthOfTime, Duration> lengthOfTimeConverter;
    private final TokenCreator tokenCreator;
    private final TokenRepository repository;

    @Inject
    CreateTokenOperation(Function<LengthOfTime, Duration> lengthOfTimeConverter,
                         TokenCreator tokenCreator,
                         TokenRepository repository)
    {
        checkThat(lengthOfTimeConverter, tokenCreator, repository)
            .are(notNull());

        this.lengthOfTimeConverter = lengthOfTimeConverter;
        this.tokenCreator = tokenCreator;
        this.repository = repository;
    }

    @Override
    public CreateTokenResponse process(CreateTokenRequest request) throws TException
    {
        LOG.debug("Received request to create an  Token: {}", request);

        checkRequestNotNull(request);

        checkThat(request.desiredTokenType)
            .throwing(withMessage("Token Type is required"))
            .is(notNull());

        checkThat(request.ownerId)
            .throwing(withMessage("bad owner ID"))
            .is(nonEmptyString())
            .usingMessage("ownerId must be >= 3")
            .is(stringWithLengthGreaterThanOrEqualTo(3))
            .usingMessage("ownerId must be a UUID type")
            .is(validUUID());

        if (!request.isSetLifetime())
        {
            LOG.info("Token Lifetime not set. Defaulting to {}", DEFAULT_LIFETIME);
            request.setLifetime(DEFAULT_LIFETIME);
        }

        String tokenId = tokenCreator.create();
        Instant timeOfCreation = now();
        checkThat(tokenId)
            .throwing(OperationFailedException.class)
            .is(nonEmptyString());

        Instant timeOfExpiration = determineExpirationTimeFrom(timeOfCreation, request);

        AuthenticationToken token = new AuthenticationToken()
            .setTokenId(tokenId)
            .setOwnerId(request.ownerId)
            .setOwnerName(request.ownerName)
            .setTokenType(request.desiredTokenType)
            .setTimeOfCreation(timeOfCreation.toEpochMilli())
            .setTimeOfExpiration(timeOfExpiration.toEpochMilli());

        repository.saveToken(token);
        LOG.debug("Saved token to repository: {}", token);

        return new CreateTokenResponse().setToken(token);
    }

    private Instant determineExpirationTimeFrom(Instant timeOfCreation, CreateTokenRequest request)
    {
        Duration tokenLifetime = lengthOfTimeConverter.apply(request.lifetime);

        Instant expirationTime = timeOfCreation.plus(tokenLifetime);

        return expirationTime;
    }

}
<commit_msg>CreateTokenOperation: Saving additional info with Token<commit_after>/*
 * Copyright 2015 Aroma Tech.
 *
 * Licensed 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 tech.aroma.banana.authentication.service.operations;

import java.time.Duration;
import java.time.Instant;
import java.util.function.Function;
import javax.inject.Inject;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tech.aroma.banana.authentication.service.data.TokenCreator;
import tech.aroma.banana.data.TokenRepository;
import tech.aroma.banana.thrift.LengthOfTime;
import tech.aroma.banana.thrift.TimeUnit;
import tech.aroma.banana.thrift.authentication.AuthenticationToken;
import tech.aroma.banana.thrift.authentication.service.CreateTokenRequest;
import tech.aroma.banana.thrift.authentication.service.CreateTokenResponse;
import tech.aroma.banana.thrift.exceptions.OperationFailedException;
import tech.sirwellington.alchemy.annotations.access.Internal;
import tech.sirwellington.alchemy.thrift.operations.ThriftOperation;

import static java.time.Instant.now;
import static tech.aroma.banana.thrift.assertions.BananaAssertions.checkRequestNotNull;
import static tech.aroma.banana.thrift.assertions.BananaAssertions.withMessage;
import static tech.sirwellington.alchemy.arguments.Arguments.checkThat;
import static tech.sirwellington.alchemy.arguments.assertions.Assertions.notNull;
import static tech.sirwellington.alchemy.arguments.assertions.StringAssertions.nonEmptyString;
import static tech.sirwellington.alchemy.arguments.assertions.StringAssertions.stringWithLengthGreaterThanOrEqualTo;
import static tech.sirwellington.alchemy.arguments.assertions.StringAssertions.validUUID;

/**
 *
 * @author SirWellington
 */
@Internal
final class CreateTokenOperation implements ThriftOperation<CreateTokenRequest, CreateTokenResponse>
{

    private final static Logger LOG = LoggerFactory.getLogger(CreateTokenOperation.class);
    private final static LengthOfTime DEFAULT_LIFETIME = new LengthOfTime(TimeUnit.DAYS, 60);

    private final Function<LengthOfTime, Duration> lengthOfTimeConverter;
    private final TokenCreator tokenCreator;
    private final TokenRepository repository;

    @Inject
    CreateTokenOperation(Function<LengthOfTime, Duration> lengthOfTimeConverter,
                         TokenCreator tokenCreator,
                         TokenRepository repository)
    {
        checkThat(lengthOfTimeConverter, tokenCreator, repository)
            .are(notNull());

        this.lengthOfTimeConverter = lengthOfTimeConverter;
        this.tokenCreator = tokenCreator;
        this.repository = repository;
    }

    @Override
    public CreateTokenResponse process(CreateTokenRequest request) throws TException
    {
        LOG.debug("Received request to create an  Token: {}", request);

        checkRequestNotNull(request);

        checkThat(request.desiredTokenType)
            .throwing(withMessage("Token Type is required"))
            .is(notNull());

        checkThat(request.ownerId)
            .throwing(withMessage("bad owner ID"))
            .is(nonEmptyString())
            .usingMessage("ownerId must be >= 3")
            .is(stringWithLengthGreaterThanOrEqualTo(3))
            .usingMessage("ownerId must be a UUID type")
            .is(validUUID());

        if (!request.isSetLifetime())
        {
            LOG.info("Token Lifetime not set. Defaulting to {}", DEFAULT_LIFETIME);
            request.setLifetime(DEFAULT_LIFETIME);
        }

        String tokenId = tokenCreator.create();
        Instant timeOfCreation = now();
        checkThat(tokenId)
            .throwing(OperationFailedException.class)
            .is(nonEmptyString());

        Instant timeOfExpiration = determineExpirationTimeFrom(timeOfCreation, request);

        AuthenticationToken token = new AuthenticationToken()
            .setTokenId(tokenId)
            .setOwnerId(request.ownerId)
            .setOwnerName(request.ownerName)
            .setOrganizationId(request.organizationId)
            .setOrganizationName(request.ownerName)
            .setTokenType(request.desiredTokenType)
            .setTimeOfCreation(timeOfCreation.toEpochMilli())
            .setTimeOfExpiration(timeOfExpiration.toEpochMilli());

        repository.saveToken(token);
        LOG.debug("Saved token to repository: {}", token);

        return new CreateTokenResponse().setToken(token);
    }

    private Instant determineExpirationTimeFrom(Instant timeOfCreation, CreateTokenRequest request)
    {
        Duration tokenLifetime = lengthOfTimeConverter.apply(request.lifetime);

        Instant expirationTime = timeOfCreation.plus(tokenLifetime);

        return expirationTime;
    }

}

{'fraction_non_alphanumeric': 0.08501399208723343, 'fraction_numerical': 0.0027019202933513463, 'mean_word_length': 3.7563102340523176, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 12, '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': '6545862', 'n_tokens_mistral': 2815, 'n_tokens_neox': 2746, 'n_words': 588}