I have been struggling in the last two days to figure out what is not working well with my nock
setup.
Description:
- I work with
node
environment andexpress
as the application server. - The authentication flow is openid-connect using
openid-client
npm package andpassport
strategy. - I am working with Keycloak as the Identity Provider- so to access an app, you first
need to authenticate in Keyclaok.In development mode, I want to mock all the outgoing
Keyclaok requests:https://keycloak-domain.com/realms/Demo-Realm/.well-known/openid-configuration
https://keycloak-domain.com/realms/Demo-Realm/protocol/openid-connect/auth
However, as hard as I try to mock the requests to Keyclaok, the requests always reach the Keycloak server domain and I did not get the mock response set in nock
.
When bootstrapping the application, once I set the express
server created I run the nock
setup (it runs before initializing the openid-client
and ant passport
strategies).
Below is my nock
setup:
export const initializeMiddleware = async () => {
const keycloakBaseUrl = "https://keycloak-domain.com/realms/Demo-Realm";
const wellKnownEndpoint = "/.well-known/openid-configuration";
const tokenEndpoint = "/protocol/openid-connect/token";
const scope: Scope = nock(keycloakBaseUrl);
scope
.persist()
.get(wellKnownEndpoint)
.query(true)
.reply(StatusCodes.OK, {
issuer: keycloakBaseUrl,
authorization_endpoint: `${keycloakBaseUrl}${authEndpoint}`,
token_endpoint: `${keycloakBaseUrl}${tokenEndpoint}`,
userinfo_endpoint: `${keycloakBaseUrl}${userInfoEndpoint}`,
jwks_uri: `${keycloakBaseUrl}${jwksUri}`,
});
scope
.persist()
.get(tokenEndpoint)
.query(true)
.reply(StatusCodes.OK, { user: { email: '[email protected]' } });
}
Thanks in advance to anyone who can assist me with this one.