1

Unable to get https://www.npmjs.com/package/keycloak-connect to work with proxy. I am able to access the Keycloak server with the same proxy from the browser and from curl, though.

The only promising thing that I have tried was this:

app.set('trust proxy', 'loopback');

... and it didn't work.

I am able to connect through keycloak-connect if I am using it on my own WiFi (no proxy). The error that I get when using the proxy is:

Could not obtain grant code: Error: connect ETIMEDOUT 108.128.***.168:443

108.128.***.168 was the IP of the Keycloak server.

I can't verify whether there is a configuration that would allow me to use keycloak-connect with proxy.

Attaching few lines of the code that relate to keycloak:

const Keycloak = require('keycloak-connect');
const memoryStore = new session.MemoryStore();

const keycloakConfig = {
  clientId: process.env.CLIENT_ID,
  'auth-server-url': process.env.SERVER_URL,
  'confidential-port': 0,
  'policy-enforcer': {},
  'ssl-required': 'external',
  'verify-token-audience': true,
  realm: process.env.REALM,
  credentials: {
    'secret': process.env.CLIENT_SECRET
  },
  realmPublicKey: process.env.REALM_PUBLIC_KEY
};

const keycloak = new Keycloak({
  store: memoryStore
}, keycloakConfig);

const MongoStore = mongoSessionStore(session);

const sess = {
  name: 'ssg-next.sid',
  secret: sessionSecret,
  store: new MongoStore({
    mongooseConnection: mongoose.connection,
    ttl: 14 * 24 * 60 * 60
  }),
  resave: false,
  saveUninitialized: false,
  cookie: {
    httpOnly: true,
    maxAge: 14 * 24 * 60 * 60 * 1000
  }
};

if (!dev) {
  server.set('trust proxy', 'loopback'); // trust first proxy
  sess.cookie.secure = true; // serve secure cookies
}

server.use(keycloak.middleware({
  logout: '/logout'
}));
Ajay Raghav
  • 906
  • 9
  • 16

1 Answers1

0

You don't need to app.set("trust proxy") but app.enable("trust proxy").

Stumbled into this myself and saw a SO question about it. I currently sail smooth without anything else behind "trust proxy" as in the example - running my apps behind an Ingress Controller but this essentially resolved my protocol problems with Keycloak.

damnedOperator
  • 208
  • 2
  • 13