mirror of
https://github.com/google/nomulus.git
synced 2025-05-16 09:27:16 +02:00
Centralize token generation and formats
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=135313726
This commit is contained in:
parent
886d6f8e17
commit
79387f5d1e
21 changed files with 131 additions and 39 deletions
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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<String> generateTokens(int count) {
|
||||
final ImmutableSet<String> candidates =
|
||||
ImmutableSet.copyOf(stringGenerator.createStrings(TOKEN_LENGTH, count));
|
||||
ImmutableSet.copyOf(TokenUtils.createTokens(LRP, stringGenerator, count));
|
||||
ImmutableSet<Key<LrpToken>> existingTokenKeys = FluentIterable.from(candidates)
|
||||
.transform(new Function<String, Key<LrpToken>>() {
|
||||
@Override
|
||||
|
|
|
@ -1,44 +0,0 @@
|
|||
// 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.tools;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
|
||||
import java.util.Random;
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
/** Random string generator. */
|
||||
class RandomStringGenerator extends StringGenerator {
|
||||
|
||||
private final Random random;
|
||||
|
||||
@Inject
|
||||
RandomStringGenerator(@Named("alphabet") String alphabet, Random random) {
|
||||
super(alphabet);
|
||||
this.random = random;
|
||||
}
|
||||
|
||||
/** Generates a random string of a specified length. */
|
||||
@Override
|
||||
public String createString(int length) {
|
||||
checkArgument(length > 0);
|
||||
char[] password = new char[length];
|
||||
for (int i = 0; i < length; ++i) {
|
||||
password[i] = alphabet.charAt(random.nextInt(alphabet.length()));
|
||||
}
|
||||
return new String(password);
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
// 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.tools;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static com.google.common.base.Strings.isNullOrEmpty;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import java.util.Collection;
|
||||
|
||||
/** String generator. */
|
||||
abstract class StringGenerator {
|
||||
|
||||
/** A class containing different alphabets used to generate strings. */
|
||||
public static class Alphabets {
|
||||
|
||||
/** A URL-safe Base64 alphabet (alphanumeric, hyphen, underscore). */
|
||||
public static final String BASE_64 =
|
||||
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-";
|
||||
|
||||
/** An alphanumeric alphabet that omits visually similar characters. */
|
||||
public static final String BASE_58 =
|
||||
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
||||
}
|
||||
|
||||
protected String alphabet;
|
||||
|
||||
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);
|
||||
|
||||
/** Batch-generates an {@link ImmutableList} of strings of a specified length. */
|
||||
public Collection<String> createStrings(int length, int count) {
|
||||
ImmutableList.Builder<String> listBuilder = new ImmutableList.Builder<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
listBuilder.add(createString(length));
|
||||
}
|
||||
return listBuilder.build();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue