Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions core/src/main/java/google/registry/bsa/api/BsaCredential.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public class BsaCredential {

private final Keyring keyring;

private final Gson gson;

private final Clock clock;

@Nullable private String authToken;
Expand All @@ -79,11 +81,13 @@ public class BsaCredential {
@Config("bsaAuthUrl") String authUrl,
@Config("bsaAuthTokenExpiry") Duration authTokenExpiry,
Keyring keyring,
Gson gson,
Clock clock) {
this.urlConnectionService = urlConnectionService;
this.authUrl = authUrl;
this.authTokenExpiry = authTokenExpiry;
this.keyring = keyring;
this.gson = gson;
this.clock = clock;
}

Expand Down Expand Up @@ -143,8 +147,7 @@ String fetchNewAuthToken() throws IOException, GeneralSecurityException {
// TODO: catch json syntax exception
@SuppressWarnings("unchecked")
String idToken =
new Gson()
.fromJson(new String(getResponseBytes(connection), UTF_8), Map.class)
gson.fromJson(new String(getResponseBytes(connection), UTF_8), Map.class)
.getOrDefault(ID_TOKEN, "")
.toString();
if (idToken.isEmpty()) {
Expand Down
9 changes: 9 additions & 0 deletions core/src/main/java/google/registry/request/Modules.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.gson.Gson;
import dagger.Module;
import dagger.Provides;
import google.registry.tools.GsonUtils;
import jakarta.inject.Singleton;
import java.net.HttpURLConnection;
import javax.net.ssl.HttpsURLConnection;
Expand Down Expand Up @@ -49,9 +51,16 @@ static UrlConnectionService provideUrlConnectionService() {
@Module
public static final class GsonModule {
@Provides
@Singleton
static JsonFactory provideJsonFactory() {
return GsonFactory.getDefaultInstance();
}

@Provides
@Singleton
public static Gson provideGson() {
return GsonUtils.provideGson();
}
}

/** Dagger module that provides standard {@link NetHttpTransport}. */
Expand Down
8 changes: 0 additions & 8 deletions core/src/main/java/google/registry/request/RequestModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
import google.registry.request.auth.AuthResult;
import google.registry.request.lock.LockHandler;
import google.registry.request.lock.LockHandlerImpl;
import google.registry.tools.GsonUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
Expand Down Expand Up @@ -74,13 +73,6 @@ public RequestModule(
this.authResult = authResult;
}

@RequestScope
@VisibleForTesting
@Provides
public static Gson provideGson() {
return GsonUtils.provideGson();
}

@Provides
@Parameter(RequestParameters.PARAM_TLD)
static String provideTld(HttpServletRequest req) {
Expand Down
16 changes: 8 additions & 8 deletions core/src/main/java/google/registry/tools/AuthModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,20 +146,20 @@ public static GoogleClientSecrets provideClientSecrets(
@LocalCredentialJson
public static String provideLocalCredentialJson(
Lazy<GoogleClientSecrets> clientSecrets,
Gson gson,
@StoredCredential Lazy<Credential> credential,
@Nullable @Config("credentialFilePath") String credentialFilePath) {
try {
if (credentialFilePath != null) {
return Files.readString(Paths.get(credentialFilePath));
} else {
return new Gson()
.toJson(
ImmutableMap.<String, String>builder()
.put("type", "authorized_user")
.put("client_id", clientSecrets.get().getDetails().getClientId())
.put("client_secret", clientSecrets.get().getDetails().getClientSecret())
.put("refresh_token", credential.get().getRefreshToken())
.build());
return gson.toJson(
ImmutableMap.<String, String>builder()
.put("type", "authorized_user")
.put("client_id", clientSecrets.get().getDetails().getClientId())
.put("client_secret", clientSecrets.get().getDetails().getClientSecret())
.put("refresh_token", credential.get().getRefreshToken())
.build());
}
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.google.gson.Gson;
import google.registry.keyring.api.Keyring;
import google.registry.request.UrlConnectionService;
import google.registry.testing.FakeClock;
import google.registry.tools.GsonUtils;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -49,6 +51,7 @@
class BsaCredentialTest {

private static final Duration AUTH_TOKEN_EXPIRY = Duration.ofMinutes(30);
private static final Gson GSON = GsonUtils.provideGson();

@Mock OutputStream connectionOutputStream;
@Mock HttpsURLConnection connection;
Expand All @@ -60,7 +63,8 @@ class BsaCredentialTest {
@BeforeEach
void setup() throws Exception {
credential =
new BsaCredential(connectionService, "https://authUrl", AUTH_TOKEN_EXPIRY, keyring, clock);
new BsaCredential(
connectionService, "https://authUrl", AUTH_TOKEN_EXPIRY, keyring, GSON, clock);
}

void setupHttp() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import com.google.common.collect.ImmutableList;
import google.registry.groups.GmailClient;
import google.registry.model.console.User;
import google.registry.request.RequestModule;
import google.registry.request.Modules;
import google.registry.request.auth.AuthResult;
import google.registry.security.XsrfTokenManager;
import google.registry.ui.server.SendEmailUtils;
Expand Down Expand Up @@ -50,6 +50,6 @@ public static ConsoleApiParams createFake(AuthResult authResult) {
authResult,
sendEmailUtils,
xsrfTokenManager,
RequestModule.provideGson());
Modules.GsonModule.provideGson());
}
}
9 changes: 7 additions & 2 deletions core/src/test/java/google/registry/tools/AuthModuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ class AuthModuleTest {
private static final String ACCESS_TOKEN = "FakeAccessToken";
private static final String REFRESH_TOKEN = "FakeReFreshToken";

private static final Gson GSON = GsonUtils.provideGson();

@SuppressWarnings("WeakerAccess")
@TempDir
Path folder;
Expand Down Expand Up @@ -166,7 +168,7 @@ private static GoogleClientSecrets getSecrets() {
void test_provideLocalCredentialJson() {
String credentialJson =
AuthModule.provideLocalCredentialJson(
AuthModuleTest::getSecrets, this::getCredential, null);
AuthModuleTest::getSecrets, GSON, this::getCredential, null);
Map<String, String> jsonMap =
new Gson().fromJson(credentialJson, new TypeToken<Map<String, String>>() {}.getType());
assertThat(jsonMap.get("type")).isEqualTo("authorized_user");
Expand All @@ -182,7 +184,10 @@ void test_provideExternalCredentialJson() throws Exception {
Files.writeString(credentialFile.toPath(), "{some_field: some_value}");
String credentialJson =
AuthModule.provideLocalCredentialJson(
AuthModuleTest::getSecrets, this::getCredential, credentialFile.getCanonicalPath());
AuthModuleTest::getSecrets,
GSON,
this::getCredential,
credentialFile.getCanonicalPath());
assertThat(credentialJson).isEqualTo("{some_field: some_value}");
}

Expand Down
Loading