Skip to content
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

feat: Tolerate trivial auth header when non-trivial api-key header is provided #675 #677

Merged
merged 1 commit into from
Feb 6, 2025
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
6 changes: 6 additions & 0 deletions server/src/main/java/com/epam/aidial/core/server/Proxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ private Future<AuthorizationResult> authorizeRequest(HttpServerRequest request)
});
}

// see https://github.com/epam/ai-dial-core/issues/675
if ("Bearer".equalsIgnoreCase(authorization.trim())) {
return apiKeyStore.getApiKeyData(apiKey)
.map(apiKeyData -> new AuthorizationResult(apiKeyData, null));
}

if (apiKey.equals(AccessTokenValidator.extractTokenFromHeader(authorization))) {
// we don't know exactly what kind of credentials a client provided to us.
// we try if it's access token the first and then API key
Expand Down
33 changes: 33 additions & 0 deletions server/src/test/java/com/epam/aidial/core/server/ProxyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,39 @@ public void testHandle_SuccessApiKey() {
verify(response).setStatusCode(OK.getCode());
}

@Test
public void testHandle_AzureOpenAiRequest() {
when(request.version()).thenReturn(HttpVersion.HTTP_1_1);
when(request.method()).thenReturn(HttpMethod.GET);
MultiMap headers = mock(MultiMap.class);
when(request.headers()).thenReturn(headers);
when(request.getHeader(eq(HttpHeaders.CONTENT_TYPE))).thenReturn(null);
when(request.getHeader(eq(HttpHeaders.AUTHORIZATION))).thenReturn("bearer");
when(headers.get(eq(HEADER_API_KEY))).thenReturn("key1");
when(headers.get(eq(HttpHeaders.CONTENT_LENGTH))).thenReturn(Integer.toString(512));
when(request.path()).thenReturn("/foo");
when(request.uri()).thenReturn("/foo");

Config config = new Config();
Route route = new Route();
route.setMethods(Set.of("GET"));
route.setName("route");
route.setPaths(List.of(Pattern.compile("/foo")));
route.setResponse(new Route.Response());
LinkedHashMap<String, Route> routes = new LinkedHashMap<>();
routes.put("route", route);
config.setRoutes(routes);
when(configStore.load()).thenReturn(config);
ApiKeyData apiKeyData = new ApiKeyData();
Key originalKey = new Key();
apiKeyData.setOriginalKey(originalKey);
when(apiKeyStore.getApiKeyData("key1")).thenReturn(Future.succeededFuture(apiKeyData));

proxy.handle(request);

verify(response).setStatusCode(OK.getCode());
}

@Test
public void testHandle_SuccessAccessToken() {
when(request.version()).thenReturn(HttpVersion.HTTP_1_1);
Expand Down
Loading