swelham / ueberauth_microsoft

Microsoft Strategy for Überauth
MIT License
37 stars 35 forks source link

Allow dynamic configs #55

Closed ryanzidago closed 2 years ago

ryanzidago commented 2 years ago

Hey @swelham,

I propose the following PR to allow UeberauthMicrosoft to be used with multi-tenant applications (where a single Microsoft config won't be sufficient; i.e. applications that let users to login via their own Microsoft accounts).

It somehow relates to this comment here and this one here.

Basically what I'm doing is passing a bunch of options from the conn all the way down to the Oauth.client/1 function. This way, I can do the following in my own app controller:

  def request(conn, %{"provider" => "microsoft", "key" => key}) do
    config = get_config_for_key(key)
    config = Application.fetch_env!(:ueberauth, Ueberauth.Strategy.Microsoft.OAuth)[key]

    {
      Ueberauth.Strategy.Microsoft,
      [
        tenant_id: config[:tenant_id],
        client_id: config[:client_id],
        client_secret: config[:client_secret],
        callback_path: "/auth/microsoft/callback/#{key}",
        site: "https://graph.microsoft.com",
        authorize_url:
          "https://login.microsoftonline.com/#{config[:tenant_id]}/oauth2/v2.0/authorize",
        token_url: "https://login.microsoftonline.com/#{config[:tenant_id]}/oauth2/v2.0/token",
        request_opts: [ssl_options: [versions: [:"tlsv1.2"]]]
      ]
    }
  end

You can see here that I select the proper Microsoft credentials based on some path params. Let me know what you think. Happy to discuss a different approach.

swelham commented 2 years ago

Hey @ryanzidago, apologies for the slow response.

From a quick look, this is looking great. I will take a proper look later today and get back to you.

swelham commented 2 years ago

This looks good to me 👍

Thanks for the PR.