panva / openid-client

OAuth 2 / OpenID Connect Client API for JavaScript Runtimes
MIT License
1.83k stars 392 forks source link

discovery unexpected HTTP response status code #707

Closed NilsBaumgartner1994 closed 1 week ago

NilsBaumgartner1994 commented 1 week ago

What happened?

When configuring an open id provider: https://XXXXXXX/.well-known/openid-configuration they redirect and therefore i get an error.

This is due to a problem openid-client?

[16:31:04.966] ERROR: [OpenID] Failed to fetch provider config
err: {
  "type": "OPError",
  "message": "expected 200 OK, got: 303 See Other",
  "stack":
      OPError: expected 200 OK, got: 303 See Other
          at processResponse (/directus/node_modules/.pnpm/openid-client@5.6.5/node_modules/openid-client/lib/helpers/process_response.js:41:11)
          at Issuer.discover (/directus/node_modules/.pnpm/openid-client@5.6.5/node_modules/openid-client/lib/issuer.js:151:18)
          at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  "error": "expected 200 OK, got: 303 See Other",
  "name": "OPError"
}

This happens in ^5.0.0 and ^6.0.0

Version

v6.0.0

Runtime

Node.js

Runtime Details

v18.19.1

Code to reproduce

import { Issuer } from 'openid-client'; // ^5.0.0
//import * as client from 'openid-client'; // ^6.0.0

async function testProviderConfig() {
  try {

    // Replace this with the URL you're trying to test
    const issuerUrl = "https://XXXXXXX/.well-known/openid-configuration"

    // Discover the OpenID configuration

    // ^5.0.0
    const issuer = await Issuer.discover(issuerUrl);

    // ^6.0.0
    //const issuer = await client.discovery(new URL(issuerUrl));

    console.log('Successfully fetched provider config:');
    console.log(issuer);
  } catch (error) {
    console.error('Error fetching provider config:', error.message);
  }
}

testProviderConfig();

Required

panva commented 1 week ago

There is no definition that would explicitly allow redirects, the specs say the response is to be a 200, that's what the client expects. That's just to explain the behaviour.

That being said, you can opt-in to redirects for particular URLs.

import * as client from 'openid-client'

const issuer = new URL('https://oidc-proxy.aai.dfn.de')
await client.discovery(issuer, '<clientId>', undefined, undefined, {
  [client.customFetch]: (url, options) => {
    if (url === `${issuer.href}.well-known/openid-configuration`) {
      options.redirect = 'follow'
    }

    return globalThis.fetch(url, options)
  }
})