Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2026 DBeaver Corp
*
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of DBeaver Corp and its suppliers, if any.
* The intellectual and technical concepts contained
* herein are proprietary to DBeaver Corp and its suppliers
* and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from DBeaver Corp.
*/
package com.dbeaver.datadam.mcp.api.model;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.annotations.JsonAdapter;
import com.google.gson.annotations.SerializedName;
import org.jkiss.code.NotNull;
import org.jkiss.code.Nullable;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;

public final class DDMcpDatabaseAuthentication {
public static final String CLIENT_BINDING_FIELD = "clientBinding";

private DDMcpDatabaseAuthentication() {
}

@JsonAdapter(value = RequestDeserializer.class, nullSafe = false)
public record Request(
@SerializedName("functionId") @NotNull String functionId,
@SerializedName("requestId") @NotNull UUID requestId,
@SerializedName("username") @Nullable String username,
@SerializedName("password") @Nullable String password
) {
public Request {
Objects.requireNonNull(functionId, "functionId");
Objects.requireNonNull(requestId, "requestId");
if ((username == null) != (password == null)) {
throw new IllegalArgumentException("Username and password must be supplied together");
}
if (functionId.isBlank() || functionId.length() > 256) {
throw new IllegalArgumentException("Invalid database authentication function ID");
}
if (username != null && (username.isBlank() || username.length() > 1024 || password.length() > 4096)) {
throw new IllegalArgumentException("Invalid database credentials");
}
}

@Override
@NotNull
public String toString() {
return "Database authentication request";
}
}

// Gson's reflective record adapter includes constructor arguments in failure messages.
private static final class RequestDeserializer implements JsonDeserializer<Request> {
@Override
@NotNull
public Request deserialize(@NotNull JsonElement json, @NotNull Type type, @NotNull JsonDeserializationContext context) {
if (!json.isJsonObject()) {
throw new JsonParseException("Expected a database authentication request object");
}
var fields = json.getAsJsonObject();
String text = readString(fields.get("requestId"), "requestId");
return new Request(
readString(fields.get("functionId"), "functionId"),
text == null ? null : requireRequestId(text),
readString(fields.get("username"), "username"),
readString(fields.get("password"), "password")
);
}

}

@Nullable
private static String readString(@Nullable JsonElement value, @NotNull String name) {
if (value == null || value.isJsonNull()) {
return null;
}
if (value instanceof JsonPrimitive primitive && primitive.isString()) {
return primitive.getAsString();
}
throw new JsonParseException("Expected a string for " + name);
}

@JsonAdapter(value = ResponseDeserializer.class, nullSafe = false)
public record Response(
@SerializedName(value = "status", alternate = "Status") @NotNull Status status,
@SerializedName(value = "connectionName", alternate = {"connection_name", "ConnectionName"}) @Nullable String connectionName
) {
public Response {
Objects.requireNonNull(status, "status");
}
}

private static final class ResponseDeserializer implements JsonDeserializer<Response> {
private record Fields(
@SerializedName(value = "status", alternate = "Status") Status status,
@SerializedName(value = "connectionName", alternate = {"connection_name", "ConnectionName"}) JsonElement connectionName
) {
}

@Override
@NotNull
public Response deserialize(@NotNull JsonElement json, @NotNull Type type, @NotNull JsonDeserializationContext context) {
if (!json.isJsonObject()) {
throw new JsonParseException("Expected a database authentication response object");
}
Fields fields = context.deserialize(json, Fields.class);
return new Response(fields.status(), readString(fields.connectionName(), "connectionName"));
}
}

public record Challenge(
@JsonAdapter(RequestIdDeserializer.class)
@SerializedName("databaseAuthenticationRequestId") @NotNull UUID databaseAuthenticationRequestId
) {
public Challenge {
Objects.requireNonNull(databaseAuthenticationRequestId, "databaseAuthenticationRequestId");
}
}

private static final class RequestIdDeserializer implements JsonDeserializer<UUID> {
@Override
@NotNull
public UUID deserialize(@NotNull JsonElement json, @NotNull Type type, @NotNull JsonDeserializationContext context) {
if (json instanceof JsonPrimitive primitive && primitive.isString()) {
return requireRequestId(primitive.getAsString());
}
throw new JsonParseException("Invalid database authentication requestId");
}
}

@NotNull
private static UUID requireRequestId(@NotNull String text) {
UUID id = parseRequestId(text);
if (id == null) {
throw new JsonParseException("Invalid database authentication requestId");
}
return id;
}

@Nullable
public static UUID requestId(@Nullable DDMcpFunctionResult result) {
if (result == null || !result.error()) {
return null;
}
if (result.value() instanceof Challenge challenge) {
return challenge.databaseAuthenticationRequestId();
}
if (result.value() instanceof Map<?, ?> fields) {
Object value = fields.get("databaseAuthenticationRequestId");
if (value instanceof UUID id) {
return id;
}
return value instanceof String id ? parseRequestId(id) : null;
}
if (result.value() instanceof JsonObject fields
&& fields.get("databaseAuthenticationRequestId") instanceof JsonPrimitive id && id.isString()) {
return parseRequestId(id.getAsString());
}
return null;
}

@Nullable
private static UUID parseRequestId(@NotNull String id) {
try {
UUID parsed = UUID.fromString(id);
return parsed.toString().equalsIgnoreCase(id) ? parsed : null;
} catch (IllegalArgumentException ignored) {
return null;
}
}

public enum Status {
PENDING,
CONNECTED,
FAILED,
EXPIRED
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2026 DBeaver Corp
*
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of DBeaver Corp and its suppliers, if any.
* The intellectual and technical concepts contained
* herein are proprietary to DBeaver Corp and its suppliers
* and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from DBeaver Corp.
*/
package com.dbeaver.datadam.mcp.api.service.desktop;

import com.dbeaver.datadam.api.base.DDRemoteService;
import com.dbeaver.datadam.mcp.api.exception.DDMcpFunctionException;
import com.dbeaver.datadam.mcp.api.model.DDMcpDatabaseAuthentication;
import org.jkiss.code.NotNull;

/**
* Credential submission for an MCP database authentication challenge.
* Relays must authorize the originating user, tool and live connection before forwarding this RPC.
*/
public interface DDMcpDatabaseAuthenticationService extends DDRemoteService {
String SERVICE_ID = "dbeaver.mcp.database-authentication";

@NotNull
DDMcpDatabaseAuthentication.Response authenticateDatabase(
@NotNull DDMcpDatabaseAuthentication.Request request
) throws DDMcpFunctionException;
}
Loading
Loading