1

I have been trying to include Keycloak authentication with my NestJS app and this is driving me crazy. I keep getting an error

"WARN [Keycloak] Cannot validate access token: Error: Grant validation failed. Reason: failed to load public key to verify token. Reason: connect ECONNREFUSED ::1:8080"

My Keycloak.json file is:

{
  "realm": "my-realm",
  "auth-server-url": "http://localhost:8080/",
  "ssl-required": "external",
  "resource": "test",
  "verify-token-audience": false,
  "credentials": {
    "secret": "my-secret"
  },
  "policy-enforcer": {}
}

This is being imported in Apps.module.ts as:

KeycloakConnectModule.register('./dist/keycloak.json', {
  policyEnforcement: PolicyEnforcementMode.PERMISSIVE,
  tokenValidation: TokenValidation.ONLINE,
}),

I am using Keycloak version 19.0.1 and nest-key cloak-connect v 1.9.0. When I tried debugging. Grant-manager.js's public key is undefined. I checked with the well-known config and jwks-uri was defined as:

http://localhost:8080/realms/my-realm/protocol/openid-connect/certs

Any ideas on what might be wrong?

Knerdist
  • 53
  • 1
  • 4

2 Answers2

0

change "auth-server-url": "http://localhost:8080/", to "auth-server-url": "http://127.0.0.1:8080/",

Ariman
  • 141
  • 1
  • 5
0

If both services are running within Docker containers, change "auth-server-url" to "http://{KEYCLOAK_CONTAINER_NAME}:8080/"