Skip to content

A platform for community discussion. Free, open, simple.

DEV: Support mTLS for OIDC auth (#41524)

Chris Alberti30d8364f0abb
diff --git a/plugins/discourse-openid-connect/config/locales/server.en.yml b/plugins/discourse-openid-connect/config/locales/server.en.yml
index 05682521c71..fcb7609cd23 100644
--- a/plugins/discourse-openid-connect/config/locales/server.en.yml
+++ b/plugins/discourse-openid-connect/config/locales/server.en.yml
@@ -19,6 +19,10 @@ en:
openid_connect_user_field_mappings: "Map OIDC claims onto Discourse user fields. Each entry pairs an OIDC claim name with the numeric ID of a Discourse user field."
openid_connect_match_by_email: "Use email address to match OpenID Connect authentications to existing Discourse user accounts."
openid_connect_use_pkce: "Enable Proof Key for Code Exchange (PKCE) for OpenID Connect authentication."
+ openid_connect_mtls_client_cert: "X.509 client certificate for mTLS client authentication with the OIDC provider. To enable mTLS, {{setting:openid_connect_mtls_client_key}} must also be set."
+ openid_connect_mtls_client_key: "X.509 private key for the mTLS client certificate. To enable mTLS, {{setting:openid_connect_mtls_client_cert}} must also be set."
+ openid_connect_mtls_client_key_passcode: "Passphrase for the mTLS private key (optional)"
login:
omniauth_error:
openid_connect_discovery_error: Unable to fetch configuration from identity provider. Please try again.
+ openid_connect_mtls_error: There is a problem with the mTLS client certificate configuration. Please contact an administrator.
diff --git a/plugins/discourse-openid-connect/config/settings.yml b/plugins/discourse-openid-connect/config/settings.yml
index c7f2f311405..95eef22db28 100644
--- a/plugins/discourse-openid-connect/config/settings.yml
+++ b/plugins/discourse-openid-connect/config/settings.yml
@@ -72,3 +72,16 @@ discourse_openid_connect:
openid_connect_use_pkce:
default: false
area: "oidc"
+ openid_connect_mtls_client_cert:
+ default: ""
+ textarea: true
+ area: "oidc"
+ openid_connect_mtls_client_key:
+ default: ""
+ textarea: true
+ secret: true
+ area: "oidc"
+ openid_connect_mtls_client_key_passcode:
+ default: ""
+ secret: true
+ area: "oidc"
diff --git a/plugins/discourse-openid-connect/lib/openid_connect_authenticator.rb b/plugins/discourse-openid-connect/lib/openid_connect_authenticator.rb
index aa7e45f9719..0208f99bf22 100644
--- a/plugins/discourse-openid-connect/lib/openid_connect_authenticator.rb
+++ b/plugins/discourse-openid-connect/lib/openid_connect_authenticator.rb
@@ -179,6 +179,12 @@ class OpenIDConnectAuthenticator < Auth::ManagedAuthenticator
},
}
+ ssl_opts = mtls_ssl_options
+ if ssl_opts.present?
+ opts[:client_options][:auth_scheme] = :tls_client_auth
+ opts[:client_options][:connection_opts][:ssl] = ssl_opts
+ end
+
opts[:client_options][:connection_build] = lambda do |builder|
if SiteSetting.openid_connect_verbose_logging
builder.response :logger,
@@ -200,6 +206,22 @@ class OpenIDConnectAuthenticator < Auth::ManagedAuthenticator
Base64.urlsafe_encode64(Digest::SHA256.digest(code_verifier)).tr("+/", "-_").tr("=", "")
end
+ def mtls_ssl_options
+ cert_pem = SiteSetting.openid_connect_mtls_client_cert
+ key_pem = SiteSetting.openid_connect_mtls_client_key
+ return {} if cert_pem.blank? || key_pem.blank?
+
+ key_passcode = SiteSetting.openid_connect_mtls_client_key_passcode.presence
+
+ {
+ client_cert: OpenSSL::X509::Certificate.new(cert_pem),
+ client_key: OpenSSL::PKey.read(key_pem, key_passcode),
+ }
+ rescue OpenSSL::OpenSSLError => e
+ oidc_log("Failed to parse mTLS certificate or key: #{e.message}", error: true)
+ raise
+ end
+
def request_timeout_seconds
GlobalSetting.openid_connect_request_timeout_seconds
end
diff --git a/plugins/discourse-openid-connect/spec/lib/omniauth_open_id_connect_spec.rb b/plugins/discourse-openid-connect/spec/lib/omniauth_open_id_connect_spec.rb
index 78a87def970..48e17f1d846 100644
--- a/plugins/discourse-openid-connect/spec/lib/omniauth_open_id_connect_spec.rb
+++ b/plugins/discourse-openid-connect/spec/lib/omniauth_open_id_connect_spec.rb
@@ -259,4 +259,88 @@ describe OmniAuth::Strategies::OpenIDConnect do
end
end
end
+
+ context "with mTLS client certificate configured" do
+ before do
+ key = OpenSSL::PKey::RSA.new(2048)
+ cert = OpenSSL::X509::Certificate.new
+ cert.subject = OpenSSL::X509::Name.parse("/CN=test")
+ cert.issuer = cert.subject
+ cert.not_before = Time.now
+ cert.not_after = Time.now + 365 * 86_400
+ cert.public_key = key.public_key
+ cert.sign(key, OpenSSL::Digest.new("SHA256"))
+
+ strategy.options.client_options[:auth_scheme] = :tls_client_auth
+ strategy.options.client_options[:connection_opts] ||= {}
+ strategy.options.client_options[:connection_opts][:ssl] = {
+ client_cert: cert,
+ client_key: key,
+ }
+ end
+
+ before do
+ strategy.stubs(:full_host).returns("https://example.com")
+ strategy.stubs(:request).returns(mock)
+ strategy.request.stubs(:params).returns({})
+
+ auth_params = strategy.authorize_params
+ strategy
+ .request
+ .stubs(:params)
+ .returns("state" => auth_params[:state], "code" => "supersecretcode")
+
+ payload = {
+ iss: "https://id.example.com/",
+ sub: "someuserid",
+ aud: "appid",
+ iat: Time.now.to_i - 30,
+ exp: Time.now.to_i + 120,
+ nonce: auth_params[:nonce],
+ name: "My Auth Token Name",
+ email: "[email protected]",
+ }
+ @token = ::JWT.encode payload, nil, "none"
+
+ stub_request(:post, "https://id.example.com/token")
+ .with { |request| @request_body = request.body }
+ .to_return(
+ status: 200,
+ body: { access_token: "MTLSAccessToken", expires_in: 3600, id_token: @token }.to_json,
+ headers: {
+ "Content-Type" => "application/json",
+ },
+ )
+
+ stub_request(:get, "https://id.example.com/userinfo").with(
+ headers: {
+ "Authorization" => "Bearer MTLSAccessToken",
+ },
+ ).to_return(
+ status: 200,
+ body: {
+ sub: "someuserid",
+ name: "My Userinfo Name",
+ email: "[email protected]",
+ }.to_json,
+ headers: {
+ "Content-Type" => "application/json",
+ },
+ )
+ end
+
+ it "uses mTLS authentication instead of client_secret" do
+ expect(strategy.options.client_options.auth_scheme).to eq(:tls_client_auth)
+
+ ssl = strategy.options.client_options.connection_opts[:ssl]
+ expect(ssl[:client_cert]).to be_a(OpenSSL::X509::Certificate)
+ expect(ssl[:client_key]).to be_a(OpenSSL::PKey::RSA)
+
+ callback_response = strategy.callback_phase
+ expect(callback_response[0]).to eq(200)
+
+ expect(@request_body).to include("client_id")
+ expect(@request_body).not_to include("client_secret")
+ end
+ end
end
diff --git a/plugins/discourse-openid-connect/spec/lib/openid_connect_authenticator_spec.rb b/plugins/discourse-openid-connect/spec/lib/openid_connect_authenticator_spec.rb
index 425c7d76996..2bb74f95d1d 100644
--- a/plugins/discourse-openid-connect/spec/lib/openid_connect_authenticator_spec.rb
+++ b/plugins/discourse-openid-connect/spec/lib/openid_connect_authenticator_spec.rb
@@ -203,6 +203,70 @@ describe OpenIDConnectAuthenticator do
end
end
+ describe "mTLS support" do
+ let!(:mtls_key) { OpenSSL::PKey::RSA.new(2048) }
+ let!(:mtls_cert) do
+ cert = OpenSSL::X509::Certificate.new
+ cert.subject = OpenSSL::X509::Name.parse("/CN=test")
+ cert.issuer = cert.subject
+ cert.not_before = Time.now
+ cert.not_after = Time.now + 365 * 86_400
+ cert.public_key = mtls_key.public_key
+ cert.sign(mtls_key, OpenSSL::Digest.new("SHA256"))
+ cert
+ end
+
+ it "returns empty hash when no mTLS settings are configured" do
+ SiteSetting.openid_connect_mtls_client_cert = ""
+ SiteSetting.openid_connect_mtls_client_key = ""
+ expect(authenticator.mtls_ssl_options).to eq({})
+ end
+
+ it "parses valid PEM certificate and key" do
+ SiteSetting.openid_connect_mtls_client_cert = mtls_cert.to_pem
+ SiteSetting.openid_connect_mtls_client_key = mtls_key.to_pem
+
+ result = authenticator.mtls_ssl_options
+ expect(result[:client_cert]).to be_a(OpenSSL::X509::Certificate)
+ expect(result[:client_key]).to be_a(OpenSSL::PKey::RSA)
+ end
+
+ it "raises OpenSSL error for invalid cert PEM" do
+ SiteSetting.openid_connect_mtls_client_cert = "not-a-cert"
+ SiteSetting.openid_connect_mtls_client_key = mtls_key.to_pem
+ Rails.logger.expects(:error).with(includes("Failed to parse mTLS"))
+ expect { authenticator.mtls_ssl_options }.to raise_error(OpenSSL::OpenSSLError)
+ end
+
+ it "raises OpenSSL error for invalid key PEM" do
+ SiteSetting.openid_connect_mtls_client_cert = mtls_cert.to_pem
+ SiteSetting.openid_connect_mtls_client_key = "not-a-key"
+ Rails.logger.expects(:error).with(includes("Failed to parse mTLS"))
+ expect { authenticator.mtls_ssl_options }.to raise_error(OpenSSL::OpenSSLError)
+ end
+
+ it "decrypts a key with a passcode when the setting is provided" do
+ encrypted_key = mtls_key.export(OpenSSL::Cipher.new("aes-256-cbc"), "some_passphrase")
+
+ SiteSetting.openid_connect_mtls_client_cert = mtls_cert.to_pem
+ SiteSetting.openid_connect_mtls_client_key = encrypted_key
+ SiteSetting.openid_connect_mtls_client_key_passcode = "some_passphrase"
+
+ result = authenticator.mtls_ssl_options
+ expect(result[:client_key]).to be_a(OpenSSL::PKey::RSA)
+ end
+
+ it "raises OpenSSL error when the passphrase is wrong" do
+ encrypted_key = mtls_key.export(OpenSSL::Cipher.new("aes-256-cbc"), "some_passphrase")
+
+ SiteSetting.openid_connect_mtls_client_key = encrypted_key
+ SiteSetting.openid_connect_mtls_client_key_passcode = "wrong_passphrase"
+ SiteSetting.openid_connect_mtls_client_cert = mtls_cert.to_pem
+ Rails.logger.expects(:error).with(includes("Failed to parse mTLS"))
+ expect { authenticator.mtls_ssl_options }.to raise_error(OpenSSL::OpenSSLError)
+ end
+ end
+
describe "discovery document fetching" do
let(:document_url) do
SiteSetting.openid_connect_discovery_document =