Search code examples
javascriptopenid-connectopenidopenid-client-js

invalid_request (Parameter redirect_uri does not match code) + openid-client


I am trying to get the authentication token while this openid-client library in JS but I am getting this error: invalid_request (Parameter redirect_uri does not match code)

My code is below:

const issuer = await Issuer.Issuer.discover('https://login.issuer.de/openidsandbox');

 code_verifier = Issuer.generators.codeVerifier();
 code_challenge = Issuer.generators.codeChallenge(code_verifier);
 client = new issuer.Client({
   client_id: 'someclientid',
   client_secret: 'someclientsecret,
   redirect_uri: `http://localhost:3000/authorize/`,
   response_types: ['code id_token'],
   id_token_signing_alg_values_supported: "RS256",
 });
 
 url = client.authorizationUrl({
   // scope: 'openid functiontest',
   scope: 'openid',
   response_mode: 'form_post',
   nonce,
   code_challenge,
   code_challenge_method: 'S256',
   state,
 });

It opens the authention page and after login on server we are succesfully redirected back to our end point. which is handled by end-point authorize

('/authorize/', async (request, reply) => {
const params = client.callbackParams(request);
const tokenSet = await client.callback('https://login.issuer.de/openidsandbox/authorize', params, { nonce, state });
// this line throws the error. I tried my own server's end-point but still same error.
});

Is there something I am doing wrong. Please suggest. 🙏


Solution

  • This was fixed by answer purposed by this link

    Basically in callback URL we need to pass our server's callback. Correct way to call this client.callBack method is:

    const tokenSet = await client.callback('http://localhost:3000/authorize/', params, { nonce, state, code_verifier });