diff --git a/java/google/registry/tools/CreateAnchorTenantCommand.java b/java/google/registry/tools/CreateAnchorTenantCommand.java index 9cc6a9660..39b8a3a9c 100644 --- a/java/google/registry/tools/CreateAnchorTenantCommand.java +++ b/java/google/registry/tools/CreateAnchorTenantCommand.java @@ -18,6 +18,8 @@ import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Strings.isNullOrEmpty; import static google.registry.model.registry.Registries.findTldForNameOrThrow; import static google.registry.pricing.PricingEngineProxy.getDomainCreateCost; +import static google.registry.util.TokenUtils.TokenType.ANCHOR_TENANT; +import static google.registry.util.TokenUtils.createToken; import static org.joda.time.DateTimeZone.UTC; import com.beust.jcommander.Parameter; @@ -25,6 +27,7 @@ import com.beust.jcommander.Parameters; import com.google.common.net.InternetDomainName; import com.google.template.soy.data.SoyMapData; import google.registry.tools.soy.CreateAnchorTenantSoyInfo; +import google.registry.util.StringGenerator; import javax.inject.Inject; import org.joda.money.Money; import org.joda.time.DateTime; @@ -33,7 +36,6 @@ import org.joda.time.DateTime; @Parameters(separators = " =", commandDescription = "Provision a domain for an anchor tenant.") final class CreateAnchorTenantCommand extends MutatingEppToolCommand { - private static final int PASSWORD_LENGTH = 16; private static final int DEFAULT_ANCHOR_TENANT_PERIOD_YEARS = 2; @Parameter( @@ -78,7 +80,7 @@ final class CreateAnchorTenantCommand extends MutatingEppToolCommand { checkArgument(superuser, "This command must be run as a superuser."); findTldForNameOrThrow(InternetDomainName.from(domainName)); // Check that the tld exists. if (isNullOrEmpty(password)) { - password = passwordGenerator.createString(PASSWORD_LENGTH); + password = createToken(ANCHOR_TENANT, passwordGenerator); } Money cost = null; diff --git a/java/google/registry/tools/CreateContactCommand.java b/java/google/registry/tools/CreateContactCommand.java index b765ff001..a741cb085 100644 --- a/java/google/registry/tools/CreateContactCommand.java +++ b/java/google/registry/tools/CreateContactCommand.java @@ -22,6 +22,7 @@ import com.beust.jcommander.Parameters; import com.google.template.soy.data.SoyMapData; import google.registry.tools.params.PhoneNumberParameter; import google.registry.tools.soy.ContactCreateSoyInfo; +import google.registry.util.StringGenerator; import java.util.List; import javax.inject.Inject; diff --git a/java/google/registry/tools/CreateDomainCommand.java b/java/google/registry/tools/CreateDomainCommand.java index e72610d59..a23dfd40b 100644 --- a/java/google/registry/tools/CreateDomainCommand.java +++ b/java/google/registry/tools/CreateDomainCommand.java @@ -21,6 +21,7 @@ import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameters; import com.google.template.soy.data.SoyMapData; import google.registry.tools.soy.DomainCreateSoyInfo; +import google.registry.util.StringGenerator; import java.util.List; import javax.inject.Inject; diff --git a/java/google/registry/tools/CreateLrpTokensCommand.java b/java/google/registry/tools/CreateLrpTokensCommand.java index 292d3f9f7..206167bc0 100644 --- a/java/google/registry/tools/CreateLrpTokensCommand.java +++ b/java/google/registry/tools/CreateLrpTokensCommand.java @@ -19,6 +19,7 @@ import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.common.collect.Sets.difference; import static google.registry.model.ofy.ObjectifyService.ofy; import static google.registry.model.registry.Registries.assertTldExists; +import static google.registry.util.TokenUtils.TokenType.LRP; import static java.nio.charset.StandardCharsets.UTF_8; import com.beust.jcommander.Parameter; @@ -34,6 +35,8 @@ import com.googlecode.objectify.Work; import google.registry.model.domain.LrpToken; import google.registry.tools.Command.RemoteApiCommand; import google.registry.tools.params.PathParameter; +import google.registry.util.StringGenerator; +import google.registry.util.TokenUtils; import java.io.StringReader; import java.nio.file.Path; import java.util.Collection; @@ -70,7 +73,6 @@ public final class CreateLrpTokensCommand implements RemoteApiCommand { @Inject StringGenerator stringGenerator; - private static final int TOKEN_LENGTH = 16; private static final int BATCH_SIZE = 20; @Override @@ -125,7 +127,7 @@ public final class CreateLrpTokensCommand implements RemoteApiCommand { */ private ImmutableSet generateTokens(int count) { final ImmutableSet candidates = - ImmutableSet.copyOf(stringGenerator.createStrings(TOKEN_LENGTH, count)); + ImmutableSet.copyOf(TokenUtils.createTokens(LRP, stringGenerator, count)); ImmutableSet> existingTokenKeys = FluentIterable.from(candidates) .transform(new Function>() { @Override diff --git a/java/google/registry/tools/RegistryToolComponent.java b/java/google/registry/tools/RegistryToolComponent.java index 921e0369c..9926fa2e2 100644 --- a/java/google/registry/tools/RegistryToolComponent.java +++ b/java/google/registry/tools/RegistryToolComponent.java @@ -51,6 +51,7 @@ interface RegistryToolComponent { void inject(CreateAnchorTenantCommand command); void inject(CreateContactCommand command); void inject(CreateDomainCommand command); + void inject(CreateLrpTokensCommand command); void inject(CreateTldCommand command); void inject(EncryptEscrowDepositCommand command); void inject(GenerateApplicationsReportCommand command); diff --git a/java/google/registry/tools/RegistryToolModule.java b/java/google/registry/tools/RegistryToolModule.java index e8eb4cff9..4c0a6a809 100644 --- a/java/google/registry/tools/RegistryToolModule.java +++ b/java/google/registry/tools/RegistryToolModule.java @@ -17,6 +17,8 @@ package google.registry.tools; import dagger.Binds; import dagger.Module; import dagger.Provides; +import google.registry.util.RandomStringGenerator; +import google.registry.util.StringGenerator; import java.security.NoSuchAlgorithmException; import java.security.ProviderException; import java.security.SecureRandom; diff --git a/java/google/registry/tools/SetupOteCommand.java b/java/google/registry/tools/SetupOteCommand.java index a16ed25ee..3e53c9ced 100644 --- a/java/google/registry/tools/SetupOteCommand.java +++ b/java/google/registry/tools/SetupOteCommand.java @@ -28,6 +28,7 @@ import google.registry.model.registrar.Registrar; import google.registry.model.registry.Registry.TldState; import google.registry.tools.Command.RemoteApiCommand; import google.registry.tools.params.PathParameter; +import google.registry.util.StringGenerator; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; diff --git a/java/google/registry/tools/RandomStringGenerator.java b/java/google/registry/util/RandomStringGenerator.java similarity index 93% rename from java/google/registry/tools/RandomStringGenerator.java rename to java/google/registry/util/RandomStringGenerator.java index f41802a74..8932a2066 100644 --- a/java/google/registry/tools/RandomStringGenerator.java +++ b/java/google/registry/util/RandomStringGenerator.java @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package google.registry.tools; +package google.registry.util; import static com.google.common.base.Preconditions.checkArgument; @@ -21,7 +21,7 @@ import javax.inject.Inject; import javax.inject.Named; /** Random string generator. */ -class RandomStringGenerator extends StringGenerator { +public class RandomStringGenerator extends StringGenerator { private final Random random; diff --git a/java/google/registry/tools/StringGenerator.java b/java/google/registry/util/StringGenerator.java similarity index 91% rename from java/google/registry/tools/StringGenerator.java rename to java/google/registry/util/StringGenerator.java index c67cab3e2..fb0895bc0 100644 --- a/java/google/registry/tools/StringGenerator.java +++ b/java/google/registry/util/StringGenerator.java @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package google.registry.tools; +package google.registry.util; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.base.Strings.isNullOrEmpty; @@ -21,7 +21,7 @@ import com.google.common.collect.ImmutableList; import java.util.Collection; /** String generator. */ -abstract class StringGenerator { +public abstract class StringGenerator { /** A class containing different alphabets used to generate strings. */ public static class Alphabets { @@ -37,13 +37,13 @@ abstract class StringGenerator { protected String alphabet; - StringGenerator(String alphabet) { + protected StringGenerator(String alphabet) { checkArgument(!isNullOrEmpty(alphabet), "Alphabet cannot be null or empty."); this.alphabet = alphabet; } /** Generates a string of a specified length. */ - abstract String createString(int length); + public abstract String createString(int length); /** Batch-generates an {@link ImmutableList} of strings of a specified length. */ public Collection createStrings(int length, int count) { diff --git a/java/google/registry/util/TokenUtils.java b/java/google/registry/util/TokenUtils.java new file mode 100644 index 000000000..b265c3f00 --- /dev/null +++ b/java/google/registry/util/TokenUtils.java @@ -0,0 +1,73 @@ +// Copyright 2016 The Domain Registry Authors. All Rights Reserved. +// +// 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 google.registry.util; + +import com.google.common.base.Function; +import com.google.common.collect.FluentIterable; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Iterables; + +/** + * A utility class for generating various auth tokens using a common prefixed-based format. + * These tokens are generally of the form [TYPE]_[randomstring]. + */ +public final class TokenUtils { + + /** An enum containing definitions (prefix and length) for token types. */ + public enum TokenType { + ANCHOR_TENANT("ANCHOR", 16), + LRP("LRP", 16); + + private final String prefix; + private final int length; + + private TokenType(String prefix, int length) { + this.prefix = prefix; + this.length = length; + } + + /** Returns the prefix for a given type. */ + public String getPrefix() { + return prefix; + } + + /** Returns the set token length for a given type (not including the prefix). */ + public int getLength() { + return length; + } + } + + /** Generates a single token of a given {@link TokenType}. */ + public static String createToken(TokenType type, StringGenerator generator) { + return Iterables.getOnlyElement(createTokens(type, generator, 1)); + } + + /** Generates a {@link Collection} of tokens of a given {@link TokenType}. */ + public static ImmutableSet createTokens( + final TokenType type, + StringGenerator generator, + int count) { + return FluentIterable.from(generator.createStrings(type.getLength(), count)) + .transform(new Function() { + @Override + public String apply(String token) { + return String.format("%s_%s", type.getPrefix(), token); + }}) + .toSet(); + } + + private TokenUtils() {} +} + diff --git a/javatests/google/registry/tools/DeterministicStringGenerator.java b/javatests/google/registry/testing/DeterministicStringGenerator.java similarity index 92% rename from javatests/google/registry/tools/DeterministicStringGenerator.java rename to javatests/google/registry/testing/DeterministicStringGenerator.java index 5911a21cb..04a1e0860 100644 --- a/javatests/google/registry/tools/DeterministicStringGenerator.java +++ b/javatests/google/registry/testing/DeterministicStringGenerator.java @@ -12,12 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -package google.registry.tools; +package google.registry.testing; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.Lists.charactersOf; import com.google.common.collect.Iterators; +import google.registry.util.RandomStringGenerator; +import google.registry.util.StringGenerator; import java.util.Iterator; import javax.inject.Named; @@ -29,14 +31,14 @@ import javax.inject.Named; *

Note that consecutive calls to createString will continue where the last call left off in * the alphabet. */ -class DeterministicStringGenerator extends StringGenerator { +public class DeterministicStringGenerator extends StringGenerator { private Iterator iterator; private final Rule rule; private int counter = 0; /** String generation rules. */ - enum Rule { + public enum Rule { /** * Simple string generation, cycling through sequential letters in the alphabet. May produce diff --git a/javatests/google/registry/tools/CreateAnchorTenantCommandTest.java b/javatests/google/registry/tools/CreateAnchorTenantCommandTest.java index f83d2b0d3..304200e4b 100644 --- a/javatests/google/registry/tools/CreateAnchorTenantCommandTest.java +++ b/javatests/google/registry/tools/CreateAnchorTenantCommandTest.java @@ -20,6 +20,7 @@ import static google.registry.testing.DatastoreHelper.persistResource; import com.beust.jcommander.ParameterException; import google.registry.model.registry.Registry; +import google.registry.testing.DeterministicStringGenerator; import org.junit.Before; import org.junit.Test; diff --git a/javatests/google/registry/tools/CreateContactCommandTest.java b/javatests/google/registry/tools/CreateContactCommandTest.java index d582894e1..aa20c4159 100644 --- a/javatests/google/registry/tools/CreateContactCommandTest.java +++ b/javatests/google/registry/tools/CreateContactCommandTest.java @@ -15,6 +15,7 @@ package google.registry.tools; import com.beust.jcommander.ParameterException; +import google.registry.testing.DeterministicStringGenerator; import org.junit.Before; import org.junit.Test; diff --git a/javatests/google/registry/tools/CreateDomainCommandTest.java b/javatests/google/registry/tools/CreateDomainCommandTest.java index 819f86798..a2bc7f639 100644 --- a/javatests/google/registry/tools/CreateDomainCommandTest.java +++ b/javatests/google/registry/tools/CreateDomainCommandTest.java @@ -15,6 +15,7 @@ package google.registry.tools; import com.beust.jcommander.ParameterException; +import google.registry.testing.DeterministicStringGenerator; import org.junit.Before; import org.junit.Test; diff --git a/javatests/google/registry/tools/CreateLrpTokensCommandTest.java b/javatests/google/registry/tools/CreateLrpTokensCommandTest.java index f83a7fa3d..dd02fb9a6 100644 --- a/javatests/google/registry/tools/CreateLrpTokensCommandTest.java +++ b/javatests/google/registry/tools/CreateLrpTokensCommandTest.java @@ -26,7 +26,8 @@ import com.google.common.io.Files; import com.googlecode.objectify.Key; import google.registry.model.domain.LrpToken; import google.registry.model.reporting.HistoryEntry; -import google.registry.tools.DeterministicStringGenerator.Rule; +import google.registry.testing.DeterministicStringGenerator; +import google.registry.testing.DeterministicStringGenerator.Rule; import java.io.File; import java.io.IOException; import java.util.Set; @@ -53,30 +54,32 @@ public class CreateLrpTokensCommandTest extends CommandTestCasejd1234 jd1234 - abcdefghijklmnop + ANCHOR_abcdefghijklmnop diff --git a/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_fee_premium.xml b/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_fee_premium.xml index a8baf0c38..1b216712c 100644 --- a/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_fee_premium.xml +++ b/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_fee_premium.xml @@ -9,7 +9,7 @@ jd1234 jd1234 - abcdefghijklmnop + ANCHOR_abcdefghijklmnop diff --git a/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_fee_standard.xml b/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_fee_standard.xml index 08030ce7f..eecda28f3 100644 --- a/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_fee_standard.xml +++ b/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_fee_standard.xml @@ -9,7 +9,7 @@ jd1234 jd1234 - abcdefghijklmnop + ANCHOR_abcdefghijklmnop diff --git a/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_multiple_word_reason.xml b/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_multiple_word_reason.xml index 3479f42a4..7bd691abf 100644 --- a/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_multiple_word_reason.xml +++ b/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_multiple_word_reason.xml @@ -9,7 +9,7 @@ jd1234 jd1234 - abcdefghijklmnop + ANCHOR_abcdefghijklmnop diff --git a/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_no_reason.xml b/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_no_reason.xml index 8e460d390..780a12f9d 100644 --- a/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_no_reason.xml +++ b/javatests/google/registry/tools/testdata/domain_create_anchor_tenant_no_reason.xml @@ -9,7 +9,7 @@ jd1234 jd1234 - abcdefghijklmnop + ANCHOR_abcdefghijklmnop