Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Add SAML request spec

Oliver Güntherf26e27efc24b
diff --git a/modules/auth_saml/spec/fixtures/saml_response.xml b/modules/auth_saml/spec/fixtures/saml_response.xml
new file mode 100644
index 00000000000..b995b7f130f
--- /dev/null
+++ b/modules/auth_saml/spec/fixtures/saml_response.xml
@@ -0,0 +1 @@
+<samlp:Response xmlns:samlp="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" Version="2.0" ID="_e8f268dc07dad48281a3318e83ad962ccb8e3dc75b" IssueInstant="2023-04-19T09:36:03.256Z" Destination="http://localhost:3000/auth/saml/callback"><saml:Issuer>https://foobar.org</saml:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/><ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/><ds:Reference URI="#_e8f268dc07dad48281a3318e83ad962ccb8e3dc75b"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>/B4p7Ab3QK9Y3XQ5LoUMcJ3hxWkBoFTLMVKmBNS01e0=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>f4WLA8kcPXYTIn/8Ra1PjByizB8fqM22H+AJXGfPoO2ZqXEkQzWNcS66FluYns/3XOSP/8yTk5fK7AhOAssXCif6O94XCdk7+roj/Xl+AG9BrgHDQU9ytblNmTU0Q0EFEarlgAuPocCimBqjLcvRFLzyr/nia6XGoREx77bRjSw=</ds:SignatureValue><ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIICpzCCAhACCQDuFX0Db5iljDANBgkqhkiG9w0BAQsFADCBlzELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExEjAQBgNVBAcMCVBhbG8gQWx0bzEQMA4GA1UECgwHU2FtbGluZzEPMA0GA1UECwwGU2FsaW5nMRQwEgYDVQQDDAtjYXByaXphLmNvbTEmMCQGCSqGSIb3DQEJARYXZW5naW5lZXJpbmdAY2Fwcml6YS5jb20wHhcNMTgwNTE1MTgxMTEwWhcNMjgwNTEyMTgxMTEwWjCBlzELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExEjAQBgNVBAcMCVBhbG8gQWx0bzEQMA4GA1UECgwHU2FtbGluZzEPMA0GA1UECwwGU2FsaW5nMRQwEgYDVQQDDAtjYXByaXphLmNvbTEmMCQGCSqGSIb3DQEJARYXZW5naW5lZXJpbmdAY2Fwcml6YS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAJEBNDJKH5nXr0hZKcSNIY1l4HeYLPBEKJLXyAnoFTdgGrvi40YyIx9lHh0LbDVWCgxJp21BmKll0CkgmeKidvGlr3FUwtETro44L+SgmjiJNbftvFxhNkgA26O2GDQuBoQwgSiagVadWXwJKkodH8tx4ojBPYK1pBO8fHf3wOnxAgMBAAEwDQYJKoZIhvcNAQELBQADgYEACIylhvh6T758hcZjAQJiV7rMRg+Omb68iJI4L9f0cyBcJENR+1LQNgUGyFDMm9Wm9o81CuIKBnfpEE2Jfcs76YVWRJy5xJ11GFKJJ5T0NEB7txbUQPoJOeNoE736lF5vYw6YKp8fJqPW0L2PLWe9qTn8hxpdnjo3k6r5gXyl8tk=</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature><samlp:Status><samlp:StatusCode Value="urn:oasis:names:tc:SAML:2.0:status:Success"/></samlp:Status><saml:Assertion xmlns:saml="urn:oasis:names:tc:SAML:2.0:assertion" Version="2.0" ID="_f96ef8785bff51f9c198f768e2b7401af51556c0ca" IssueInstant="2023-04-19T09:36:03.193Z"><saml:Issuer>https://foobar.org</saml:Issuer><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/><ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/><ds:Reference URI="#_f96ef8785bff51f9c198f768e2b7401af51556c0ca"><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/><ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/><ds:DigestValue>Ztp3jfFiai++82S52hLRZZMUEAm0gYpkSonPC0aVV7I=</ds:DigestValue></ds:Reference></ds:SignedInfo><ds:SignatureValue>bQuXxqLvkyFZuEpD8jab4xBGOz0Xg2i1DhZheCR2CN12VM8PcLhvWjZF7APvNsI6D7HC1SmBQIg2dAQUB1TGO5+ZD5TDDQd90qiKvesW1uYWWh5QP7rvphKvJl/cBXH8w3hbMnC/noC9DMQVL1ugjpa5y7Gzsj6JwNYhjWDPjHc=</ds:SignatureValue><ds:KeyInfo><ds:X509Data><ds:X509Certificate>MIICpzCCAhACCQDuFX0Db5iljDANBgkqhkiG9w0BAQsFADCBlzELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExEjAQBgNVBAcMCVBhbG8gQWx0bzEQMA4GA1UECgwHU2FtbGluZzEPMA0GA1UECwwGU2FsaW5nMRQwEgYDVQQDDAtjYXByaXphLmNvbTEmMCQGCSqGSIb3DQEJARYXZW5naW5lZXJpbmdAY2Fwcml6YS5jb20wHhcNMTgwNTE1MTgxMTEwWhcNMjgwNTEyMTgxMTEwWjCBlzELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExEjAQBgNVBAcMCVBhbG8gQWx0bzEQMA4GA1UECgwHU2FtbGluZzEPMA0GA1UECwwGU2FsaW5nMRQwEgYDVQQDDAtjYXByaXphLmNvbTEmMCQGCSqGSIb3DQEJARYXZW5naW5lZXJpbmdAY2Fwcml6YS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAJEBNDJKH5nXr0hZKcSNIY1l4HeYLPBEKJLXyAnoFTdgGrvi40YyIx9lHh0LbDVWCgxJp21BmKll0CkgmeKidvGlr3FUwtETro44L+SgmjiJNbftvFxhNkgA26O2GDQuBoQwgSiagVadWXwJKkodH8tx4ojBPYK1pBO8fHf3wOnxAgMBAAEwDQYJKoZIhvcNAQELBQADgYEACIylhvh6T758hcZjAQJiV7rMRg+Omb68iJI4L9f0cyBcJENR+1LQNgUGyFDMm9Wm9o81CuIKBnfpEE2Jfcs76YVWRJy5xJ11GFKJJ5T0NEB7txbUQPoJOeNoE736lF5vYw6YKp8fJqPW0L2PLWe9qTn8hxpdnjo3k6r5gXyl8tk=</ds:X509Certificate></ds:X509Data></ds:KeyInfo></ds:Signature><saml:Subject><saml:NameID Format="urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress">testuser</saml:NameID><saml:SubjectConfirmation Method="urn:oasis:names:tc:SAML:2.0:cm:bearer"><saml:SubjectConfirmationData NotOnOrAfter="2023-04-19T09:46:03.193Z" Recipient="http://localhost:3000/auth/saml/callback"/></saml:SubjectConfirmation></saml:Subject><saml:Conditions NotBefore="2023-04-19T09:36:03.193Z" NotOnOrAfter="2023-04-19T09:46:03.193Z"/><saml:AuthnStatement AuthnInstant="2023-04-19T09:36:03.193Z" SessionNotOnOrAfter="2023-04-19T09:46:03.188Z" SessionIndex="_samling_2425237_5964826476"><saml:AuthnContext><saml:AuthnContextClassRef>urn:oasis:names:tc:SAML:2.0:ac:classes:unspecified</saml:AuthnContextClassRef></saml:AuthnContext></saml:AuthnStatement><saml:AttributeStatement xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><saml:Attribute Name="email"><saml:AttributeValue xsi:type="xs:anyType">[email protected]</saml:AttributeValue></saml:Attribute><saml:Attribute Name="givenName"><saml:AttributeValue xsi:type="xs:anyType">test</saml:AttributeValue></saml:Attribute><saml:Attribute Name="sn"><saml:AttributeValue xsi:type="xs:anyType">user</saml:AttributeValue></saml:Attribute></saml:AttributeStatement></saml:Assertion></samlp:Response>
diff --git a/modules/auth_saml/spec/requests/saml_provider_callback_spec.rb b/modules/auth_saml/spec/requests/saml_provider_callback_spec.rb
new file mode 100644
index 00000000000..463810540d4
--- /dev/null
+++ b/modules/auth_saml/spec/requests/saml_provider_callback_spec.rb
@@ -0,0 +1,110 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2023 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See COPYRIGHT and LICENSE files for more details.
+#++
+
+require 'spec_helper'
+require 'rack/test'
+
+describe 'SAML provider callback' do
+ include Rack::Test::Methods
+ include API::V3::Utilities::PathHelper
+
+ let(:saml_response) do
+ xml = File.read("#{File.dirname(__FILE__)}/../fixtures/saml_response.xml")
+ Base64.encode64(xml)
+ end
+
+ let(:body) do
+ { SAMLResponse: saml_response }
+ end
+
+ let(:issuer) { 'https://foobar.org' }
+ let(:fingerprint) { 'b711a422a0579da630063cbfac448f90be5ae23f' }
+
+ let(:config) do
+ {
+ 'name' => 'saml',
+ 'display_name' => 'SAML',
+ 'assertion_consumer_service_url' => 'http://localhost:3000/auth/saml/callback',
+ 'issuer' => issuer,
+ 'idp_cert_fingerprint' => fingerprint,
+ 'idp_sso_target_url' => 'https://foobar.org/login',
+ 'idp_slo_target_url' => 'https://foobar.org/logout',
+ 'security' => {
+ 'digest_method' => 'http://www.w3.org/2001/04/xmlenc#sha256',
+ 'check_idp_cert_expiration' => false
+ },
+ 'attribute_statements' => {
+ 'email' => ['mail', 'urn:oid:0.9.2342.19200300.100.1.3'],
+ 'login' => ['uid', 'login', 'urn:oid:0.9.2342.19200300.100.1.3'],
+ 'first_name' => ['givenName', 'urn:oid:2.5.4.42'],
+ 'last_name' => ['sn', 'urn:oid:2.5.4.4']
+ }
+ }
+ end
+
+ let(:request) { post '/auth/saml/callback', body }
+
+ subject do
+ Timecop.freeze("2023-04-19T09:37:00Z".to_datetime) { request }
+ end
+
+ before do
+ with_enterprise_token :openid_providers
+ Setting.plugin_openproject_auth_saml = {
+ "providers" => { "saml" => config }
+ }
+ end
+
+ shared_examples 'request fails' do
+ it 'redirects to the failure page' do
+ expect(subject.status).to eq(302)
+ expect(subject.headers['Location']).to eq("/auth/failure?message=invalid_ticket&strategy=saml")
+ end
+ end
+
+ it 'redirects user when no errors occured' do
+ expect(subject.status).to eq(302)
+ expect(subject.headers['Location']).to eq("http://example.org/two_factor_authentication/request")
+ end
+
+ context 'with an invalid timestamp' do
+ subject do
+ Timecop.freeze("2023-04-15T09:37:00Z".to_datetime) do
+ request
+ end
+ end
+
+ it_behaves_like 'request fails'
+ end
+
+ context 'with an invalid fingerprint' do
+ let(:fingerprint) { 'invalid' }
+
+ it_behaves_like 'request fails'
+ end
+end