module Concerns
  module OpenIdConnectSupport
    extend ActiveSupport::Concern

    protected

    def build_response(state, params, nonce)
      # The request doesn't contain any information to help us find the right application instance
      # so we have to use predefined URLs
      uri = URI.parse(current_application.oidc_url(params["iss"]))
      uri_params = Rack::Utils.parse_query(uri.query)
      auth_params = {
        response_type: "id_token",
        redirect_uri: params[:target_link_uri],
        response_mode: "form_post",
        client_id: current_application.client_id(params["iss"]),
        scope: "openid",
        state: state,
        login_hint: params[:login_hint],
        prompt: "none",
        lti_message_hint: params[:lti_message_hint],
        nonce: nonce,
      }.merge(uri_params)
      uri.fragment = uri.query = nil
      [uri.to_s, "?", auth_params.to_query].join
    end
  end
end

module Concerns
  module OpenIdConnectSupport
    extend ActiveSupport::Concern

    protected

    def build_response(state, params, nonce)
      # The request doesn't contain any information to help us find the right application instance
      # so we have to use predefined URLs
      uri = URI.parse(current_application.oidc_url(params["iss"]))
      uri_params = Rack::Utils.parse_query(uri.query)
      auth_params = {
        response_type: "id_token",
        redirect_uri: params[:target_link_uri],
        response_mode: "form_post",
        client_id: params[:client_id],
        scope: "openid",
        state: state,
        login_hint: params[:login_hint],
        prompt: "none",
        lti_message_hint: params[:lti_message_hint],
        nonce: nonce,
      }.merge(uri_params)
      uri.fragment = uri.query = nil
      [uri.to_s, "?", auth_params.to_query].join
    end
  end
end

Use client_id from params in OpenID initialization
fix: Use client_id from params in OpenID initialization

We were previously pulling the `client_id` from the first `LtiInstall` record
with a matching `iss`. This approach only works if there's only one
`LtiInstall` record with a matching `iss` though. In practice, we may very
well have multiple `LtiInstall` records with the same `iss`.
The request parameters include the `client_id` for the tool being launched.
We can use this `client_id` instead of pulling it from the `LtiInstall` record
and then we don't have to worry about `iss` collisions.
