-
Notifications
You must be signed in to change notification settings - Fork 1.7k
[OPIK-7940] [BE] [FE] feat: dynamic token auth (OAuth2 client credentials) for custom AI providers #7910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Open
[OPIK-7940] [BE] [FE] feat: dynamic token auth (OAuth2 client credentials) for custom AI providers #7910
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
2e64598
[OPIK-7940] [BE] feat: auth_config storage for dynamic token auth
miguelgrc 1117861
feat(llm-auth): token fetcher with Redis-backed cache
miguelgrc a6c0520
feat(llm-auth): wire dynamic bearer into the custom provider request …
miguelgrc 673c90b
feat(llm-auth): destination guard and test-connection endpoint
miguelgrc c3a5717
feat(fe): OAuth2 client credentials auth in the custom provider dialog
miguelgrc 3a2c2a0
fix(llm-auth): reject partial auth_config instead of clearing, plus r…
miguelgrc 8829c7f
fix(llm-auth): bound token lifetimes to one year, parameterize partia…
miguelgrc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
apps/opik-backend/src/main/java/com/comet/opik/api/ProviderAuthCheck.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.comet.opik.api; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.validation.Valid; | ||
| import lombok.Builder; | ||
|
|
||
| import java.util.UUID; | ||
|
|
||
| /** | ||
| * Test-connection request for a provider's dynamic token auth. Two modes, both executed by the | ||
| * backend: by {@code provider_id} alone the stored recipe is used (secrets never transit the | ||
| * browser); with an {@code auth_config} the submitted values are used, resolving | ||
| * {@code __SECRET__} sentinels against the stored recipe when {@code provider_id} is also given. | ||
| */ | ||
| @Builder(toBuilder = true) | ||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
| public record ProviderAuthCheck( | ||
| @Schema(description = "Test the stored auth config of this provider; also the sentinel-resolution target when auth_config is sent") UUID providerId, | ||
| @Valid @Schema(description = "Auth config to test as-submitted; omit to test the stored one") ProviderAuthConfig authConfig) { | ||
|
|
||
| @Builder(toBuilder = true) | ||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
| public record Result( | ||
| @Schema(description = "Lifetime of the fetched token in seconds; the token itself is never returned") long lifetimeSeconds) { | ||
| } | ||
| } |
142 changes: 142 additions & 0 deletions
142
apps/opik-backend/src/main/java/com/comet/opik/api/ProviderAuthConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package com.comet.opik.api; | ||
|
|
||
| import com.comet.opik.utils.ValidationUtils; | ||
| import com.fasterxml.jackson.annotation.JsonCreator; | ||
| import com.fasterxml.jackson.annotation.JsonIgnore; | ||
| import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
| import com.fasterxml.jackson.annotation.JsonValue; | ||
| import com.fasterxml.jackson.annotation.JsonView; | ||
| import com.fasterxml.jackson.databind.PropertyNamingStrategies; | ||
| import com.fasterxml.jackson.databind.annotation.JsonNaming; | ||
| import io.swagger.v3.oas.annotations.media.Schema; | ||
| import jakarta.validation.Valid; | ||
| import jakarta.validation.constraints.Max; | ||
| import jakarta.validation.constraints.Min; | ||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.Size; | ||
| import lombok.Builder; | ||
| import lombok.Getter; | ||
| import lombok.RequiredArgsConstructor; | ||
| import org.apache.commons.collections4.CollectionUtils; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Recipe describing how to fetch a short-lived bearer token before calling a custom LLM provider. | ||
| * Stored as a single AES-GCM-encrypted JSON document in {@code llm_provider_api_key.auth_config}; | ||
| * a row without one behaves exactly as before (static api_key auth). | ||
| */ | ||
| @Builder(toBuilder = true) | ||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
| public record ProviderAuthConfig( | ||
| @JsonView({ | ||
| ProviderApiKey.View.Public.class, | ||
| ProviderApiKey.View.Write.class}) @Schema(description = "Auth service URL the credentials are sent to", example = "https://developer.api.example.com/authentication/v1/token") String tokenUrl, | ||
| @JsonView({ProviderApiKey.View.Public.class, | ||
| ProviderApiKey.View.Write.class}) @Schema(description = "How credentials are sent: form body (default), JSON body, or basic auth (id/secret in an HTTP Basic header, remaining fields in the form body)") SendAs sendAs, | ||
| @JsonView({ProviderApiKey.View.Public.class, | ||
| ProviderApiKey.View.Write.class}) @Valid @Schema(description = "Fields sent to the token URL. Values flagged as secret are write-only: they read back as the '" | ||
| + ProviderAuthConfig.SECRET_SENTINEL + "' sentinel") List<Credential> credentials, | ||
| @JsonView({ProviderApiKey.View.Public.class, | ||
| ProviderApiKey.View.Write.class}) @Size(max = 250) @Schema(description = "Field holding the token in the reply; dot-path for nested replies", example = "access_token") String tokenField, | ||
| @JsonView({ProviderApiKey.View.Public.class, | ||
| ProviderApiKey.View.Write.class}) @Size(max = 250) @Schema(description = "Field holding the token lifetime in seconds in the reply; dot-path for nested replies", example = "expires_in") String expiresField, | ||
| @JsonView({ProviderApiKey.View.Public.class, | ||
| ProviderApiKey.View.Write.class}) @Min(0) @Max(31_536_000) @Schema(description = "Lifetime in seconds assumed when the reply doesn't state one, capped at one year; 0 means such tokens are not cached (fetched per call)") Long fallbackTtlSeconds) { | ||
|
|
||
| public static final String SECRET_SENTINEL = "__SECRET__"; | ||
| private static final ProviderAuthConfig EMPTY = ProviderAuthConfig.builder().build(); | ||
|
|
||
| @Builder(toBuilder = true) | ||
| @JsonIgnoreProperties(ignoreUnknown = true) | ||
| @JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) | ||
| public record Credential( | ||
| @JsonView({ | ||
| ProviderApiKey.View.Public.class, | ||
| ProviderApiKey.View.Write.class}) @NotBlank @Size(max = 250) String key, | ||
| @JsonView({ProviderApiKey.View.Public.class, ProviderApiKey.View.Write.class}) String value, | ||
| @JsonView({ProviderApiKey.View.Public.class, | ||
| ProviderApiKey.View.Write.class}) @Schema(description = "Secret values are encrypted at rest and never read back; once true it cannot be unset") boolean secret) { | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Credential{key='" + key + "', value='*******', secret=" + secret + '}'; | ||
| } | ||
| } | ||
|
|
||
| @Getter | ||
| @RequiredArgsConstructor | ||
| public enum SendAs { | ||
| FORM("form"), | ||
| JSON("json"), | ||
| BASIC("basic"), | ||
| ; | ||
|
|
||
| @JsonValue | ||
| private final String value; | ||
|
|
||
| @JsonCreator | ||
| public static SendAs fromString(String value) { | ||
| return Arrays.stream(values()) | ||
| .filter(sendAs -> sendAs.value.equals(value)) | ||
| .findFirst() | ||
| .orElseThrow(() -> new IllegalArgumentException("Unknown send_as '%s'".formatted(value))); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A literal empty object ({@code {}}) is the API convention for clearing the auth config on | ||
| * update, mirroring how an empty headers map clears headers. | ||
| */ | ||
| @JsonIgnore | ||
| public boolean isEmpty() { | ||
| return equals(EMPTY); | ||
| } | ||
|
|
||
| /** | ||
| * Requiredness rules shared by the create validator and the update path. Field-level Jakarta | ||
| * annotations can't carry these because the empty clear-object must pass bean validation. | ||
| */ | ||
| @JsonIgnore | ||
| public List<String> validationErrors() { | ||
| var errors = new ArrayList<String>(); | ||
| if (!ValidationUtils.isAbsoluteUri(tokenUrl)) { | ||
| errors.add("auth_config.token_url must be a valid absolute URI"); | ||
| } | ||
| if (CollectionUtils.isEmpty(credentials)) { | ||
| errors.add("auth_config.credentials must not be empty"); | ||
| } | ||
| return errors; | ||
| } | ||
|
|
||
| /** | ||
| * Copy safe to return from the API: secret values are replaced with {@link #SECRET_SENTINEL}. | ||
| */ | ||
| public ProviderAuthConfig mask() { | ||
| if (CollectionUtils.isEmpty(credentials)) { | ||
| return this; | ||
| } | ||
| return toBuilder() | ||
| .credentials(credentials.stream() | ||
| .map(credential -> credential.secret() | ||
| ? credential.toBuilder().value(SECRET_SENTINEL).build() | ||
| : credential) | ||
| .toList()) | ||
| .build(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "ProviderAuthConfig{" + | ||
| "tokenUrl='" + tokenUrl + '\'' + | ||
| ", sendAs=" + sendAs + | ||
| ", credentials=" + credentials + | ||
| ", tokenField='" + tokenField + '\'' + | ||
| ", expiresField='" + expiresField + '\'' + | ||
| ", fallbackTtlSeconds=" + fallbackTtlSeconds + | ||
| '}'; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.