You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Question: How to correctly configure tests for Keycloak authentication?
I'm having an issue running authenticated API endpoint tests with Keycloak. My configuration seems correct, but tests receive a 401 Unauthorized, while using curl or Scalar works fine for the same user credentials and endpoint.
Current configuration:
// Adding Keycloak test container and parametersvarkeycloak=builder.AddKeycloak("keycloak",8080).WithDataVolume().WithRealmImport("./realms");vargeminiApiKey=builder.AddParameter("GeminiApiKey",secret:true);varapi=builder.AddProject<Projects.Prickle_Api>("api").WithUrlForEndpoint("https", e =>{e.DisplayText="Scalar";e.Url+="/scalar";}).WithEnvironment("GEMINI_API_KEY",geminiApiKey).WithReference(keycloak).WaitFor(keycloak).WithReference(prickleDb).WaitFor(prickleDb);
// Test helpers for creating authed HttpClientsprotectedasyncTask<HttpClient>CreateHttpClientAsync(stringresourceName,string?endpointName=null,stringusername="bob",string?password=null){varclient=AppHostFactory.App.CreateHttpClient(resourceName,endpointName,useHttpClientFactory:false);vartoken=awaitGetTokenAsync(username,password??username);client.DefaultRequestHeaders.Authorization=newSystem.Net.Http.Headers.AuthenticationHeaderValue("Bearer",token);returnclient;}privateasyncTask<string>GetTokenAsync(stringusername,stringpassword){usingvarhttpclient=AppHostFactory.App.CreateHttpClient("keycloak","http");vartokenEndpoint=$"realms/{RealmName}/protocol/openid-connect/token";vartokenRequest=newFormUrlEncodedContent([newKeyValuePair<string,string>("grant_type","password"),newKeyValuePair<string,string>("client_id",PublicClientId),newKeyValuePair<string,string>("username",username),newKeyValuePair<string,string>("password",password),]);varauthorizationResponse=awaithttpclient.PostAsync(tokenEndpoint,tokenRequest);
.....
I use CreateHttpClientAsync in tests to call an endpoint as an authenticated user, but for every request I get HTTP 401. If I take the same user and call via curl or through Scalar UI, authentication works and the endpoint responds as expected.
Any help or best practices for configuring this scenario test would be appreciated.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Question: How to correctly configure tests for Keycloak authentication?
I'm having an issue running authenticated API endpoint tests with Keycloak. My configuration seems correct, but tests receive a 401 Unauthorized, while using curl or Scalar works fine for the same user credentials and endpoint.
Current configuration:
I use
CreateHttpClientAsyncin tests to call an endpoint as an authenticated user, but for every request I get HTTP 401. If I take the same user and call via curl or through Scalar UI, authentication works and the endpoint responds as expected.Any help or best practices for configuring this scenario test would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions