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

[#62107] Update SCIM server API.

Pavel Balashoubfe8f32e1623
diff --git a/app/controllers/scim_v2/base_controller_actions.rb b/app/controllers/scim_v2/base_controller_actions.rb
index de58897c464..9aca5278ab5 100644
--- a/app/controllers/scim_v2/base_controller_actions.rb
+++ b/app/controllers/scim_v2/base_controller_actions.rb
@@ -55,15 +55,27 @@ module ScimV2
.limit(pagination_info.limit)
.to_a
+ excluded_attributes = params.fetch(:excludedAttributes, "").split(",")
+ attributes = storage_class.scim_attributes_map.keys + storage_class.scim_attributes_map.values.find_all { |i| i.is_a? Hash }.flat_map(&:keys)
+ attributes = attributes.map(&:to_s)
super(pagination_info, page_of_results) do |record|
- record.to_scim(location: url_for(action: :show, id: record.id))
+ record.to_scim(
+ location: url_for(action: :show, id: record.id),
+ include_attributes: attributes - excluded_attributes
+ )
end
end
def show
super do |record_id|
record = storage_scope.find(record_id)
- record.to_scim(location: url_for(action: :show, id: record_id))
+ excluded_attributes = params.fetch(:excludedAttributes, "").split(",")
+ attributes = storage_class.scim_attributes_map.keys + storage_class.scim_attributes_map.values.find_all { |i| i.is_a? Hash }.flat_map(&:keys)
+ attributes = attributes.map(&:to_s)
+ record.to_scim(
+ location: url_for(action: :show, id: record_id),
+ include_attributes: attributes - excluded_attributes
+ )
end
end
end
diff --git a/app/controllers/scim_v2/groups_controller.rb b/app/controllers/scim_v2/groups_controller.rb
index 95ec8584601..b0e708a81ed 100644
--- a/app/controllers/scim_v2/groups_controller.rb
+++ b/app/controllers/scim_v2/groups_controller.rb
@@ -40,13 +40,27 @@ module ScimV2
call = Groups::CreateService
.new(user: User.system, model: group)
.call(group.attributes)
- .on_failure { |result| raise result.message }
+ .on_failure do |result|
+ uniqueness_error = result.errors.find { |e| e.type == :taken }
+ if uniqueness_error.present?
+ raise Scimitar::ErrorResponse.new(
+ status: 409,
+ scimType: "uniqueness",
+ detail: "Operation failed due to a uniqueness constraint: #{result.message}"
+ )
+ else
+ raise result.message
+ end
+ end
group = call.result
- Groups::AddUsersService
- .new(group, current_user: User.system)
- .call(ids: scim_resource.members.map(&:value), send_notifications: false)
- .on_failure { |call| raise call.message }
+ members = scim_resource.members
+ if members.present?
+ Groups::AddUsersService
+ .new(group, current_user: User.system)
+ .call(ids: members.map(&:value), send_notifications: false)
+ .on_failure { |call| raise call.message }
+ end
group.to_scim(location: url_for(action: :show, id: group.id))
end
@@ -73,9 +87,10 @@ module ScimV2
storage_class.transaction do
group = storage_scope.find(group_id)
group.from_scim_patch!(patch_hash: patch_hash)
+ user_ids = group.scim_members.map(&:id)
Groups::UpdateService
.new(user: User.system, model: group)
- .call
+ .call(user_ids:)
.on_failure { |call| raise call.message }
group.reload
group.to_scim(location: url_for(action: :show, id: group.id))
diff --git a/app/models/group.rb b/app/models/group.rb
index 083b6c6b483..5aad3ccb7cd 100644
--- a/app/models/group.rb
+++ b/app/models/group.rb
@@ -84,14 +84,29 @@ class Group < Principal
external_id
end
- def scim_users_and_groups
- users
+ def scim_members
+ @scim_members ||= users
end
- def scim_users_and_groups=(_array)
- # Nothing is happening here, because users should be added/removed to the group
- # through Groups::UpdateSerivce. So, it is done on a higher(controller) level.
- users
+ def scim_members=(array)
+ # Here we just assign array of found users to an instance variable.
+ # So it is done on a higher(controller) level to pass users_ids list
+ # to Groups::UpdateService
+ @scim_members = array
+ end
+
+ def scim_active=(is_active)
+ if is_active
+ activate
+ true
+ else
+ lock if active?
+ false
+ end
+ end
+
+ def scim_active
+ active?
end
def self.scim_resource_type
@@ -104,7 +119,7 @@ class Group < Principal
externalId: :scim_external_id,
displayName: :name,
members: [
- list: :scim_users_and_groups,
+ list: :scim_members,
using: {
value: :id
},
diff --git a/app/models/user.rb b/app/models/user.rb
index 0d4eb649c83..06da8a79aff 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -31,6 +31,8 @@
require "digest/sha1"
class User < Principal
+ ScimEmail = Struct.new("ScimEmail", :value, :primary, :type)
+
VALID_NAME_REGEX = /\A[\d\p{Alpha}\p{Mark}\p{Space}\p{Emoji}'’´\-_.,@()+&*–]+\z/
CURRENT_USER_LOGIN_ALIAS = "me"
USER_FORMATS_STRUCTURE = {
@@ -585,13 +587,16 @@ class User < Principal
active?
end
- Email = Struct.new("Email", :value, :primary, :type)
def scim_emails
- [Email.new(mail, true, "work")]
+ [ScimEmail.new(mail, true, "work")]
end
def scim_emails=(emails)
- self.mail = emails.first.value
+ email = (emails.find { |email| email.primary == true }) ||
+ (emails.find { |email| email.type == "work" }) ||
+ emails.sort.first
+
+ self.mail = email&.value
end
def self.scim_resource_type
@@ -617,7 +622,7 @@ class User < Principal
type: :type
},
find_with: Proc.new do |qwe|
- Email.new(qwe["value"], !!qwe["primary"], qwe["type"])
+ ScimEmail.new(qwe["value"], qwe["primary"] == true, qwe["type"])
end
}
],
diff --git a/app/services/groups/delete_service.rb b/app/services/groups/delete_service.rb
index 99001014fd9..8b877cca58a 100644
--- a/app/services/groups/delete_service.rb
+++ b/app/services/groups/delete_service.rb
@@ -30,6 +30,7 @@ class Groups::DeleteService < BaseServices::Delete
protected
def destroy(group)
+ group.update_column(:status, Group.statuses[:locked])
::Principals::DeleteJob.perform_later(group)
true
end
diff --git a/spec/requests/scim_v2/groups_spec.rb b/spec/requests/scim_v2/groups_spec.rb
index effa2973a09..5c4d47ba8bd 100644
--- a/spec/requests/scim_v2/groups_spec.rb
+++ b/spec/requests/scim_v2/groups_spec.rb
@@ -36,6 +36,8 @@ RSpec.describe "SCIM API Groups" do
let(:admin) { create(:admin) }
let(:oidc_provider) { create(:oidc_provider, slug: "keycloak", creator: admin) }
let(:user) { create(:user, identity_url: "#{oidc_provider.slug}:#{external_user_id}") }
+ let(:user1) { user }
+ let(:user2) { create(:user, identity_url: "#{oidc_provider.slug}:#{external_user_id}_user2") }
let(:group) { create(:group, identity_url: "#{oidc_provider.slug}:#{external_group_id}", members: [user]) }
let(:headers) { { "CONTENT_TYPE" => "application/scim+json", "HTTP_AUTHORIZATION" => "Bearer access_token" } }
@@ -123,6 +125,21 @@ RSpec.describe "SCIM API Groups" do
"resourceType" => "Group" },
"schemas" => ["urn:ietf:params:scim:schemas:core:2.0:Group"] })
end
+
+ it "excludes specified attributes" do
+ get "/scim_v2/Groups/#{group.id}?excludedAttributes=members", {}, headers
+
+ response_body = JSON.parse(last_response.body)
+ expect(response_body).to eq({ "displayName" => group.name,
+ "externalId" => external_group_id,
+ "id" => group.id.to_s,
+ "meta" => { "location" => "http://test.host/scim_v2/Groups/#{group.id}",
+ "created" => group.created_at.iso8601,
+ "lastModified" => group.updated_at.iso8601,
+ "resourceType" => "Group" },
+ "schemas" => ["urn:ietf:params:scim:schemas:core:2.0:Group"] })
+ expect(response_body["members"]).to be_nil
+ end
end
context "with the feature flag disabled", with_flag: { scim_api: false } do
@@ -160,6 +177,29 @@ RSpec.describe "SCIM API Groups" do
"lastModified" => group.updated_at.iso8601,
"resourceType" => "Group" },
"schemas" => ["urn:ietf:params:scim:schemas:core:2.0:Group"] })
+
+ end
+
+ it "memberless request" do
+ user
+ request_body = { "displayName" => "Group 123",
+ "externalId" => external_group_id,
+ "schemas" => ["urn:ietf:params:scim:schemas:core:2.0:Group"] }
+ post "/scim_v2/Groups/", request_body.to_json, headers
+
+ response_body = JSON.parse(last_response.body)
+ group = Group.last
+ expect(Group.count).to eq(1)
+ expect(response_body).to eq({ "displayName" => group.name,
+ "externalId" => external_group_id,
+ "id" => group.id.to_s,
+ "members" => [],
+ "meta" => { "location" => "http://test.host/scim_v2/Groups/#{group.id}",
+ "created" => group.created_at.iso8601,
+ "lastModified" => group.updated_at.iso8601,
+ "resourceType" => "Group" },
+ "schemas" => ["urn:ietf:params:scim:schemas:core:2.0:Group"] })
+
end
end
@@ -302,6 +342,92 @@ RSpec.describe "SCIM API Groups" do
"resourceType" => "Group" },
"schemas" => ["urn:ietf:params:scim:schemas:core:2.0:Group"] })
end
+
+ it "replaces members" do
+ group
+ user2
+
+ request_body = {
+ "schemas" =>
+ ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
+ "Operations" => [{
+ "op"=> "replace",
+ "path"=> "members",
+ "value"=> [{
+ "value"=> user2.id.to_s
+ }]
+ }]
+ }
+ patch "/scim_v2/Groups/#{group.id}", request_body.to_json, headers
+
+ response_body = JSON.parse(last_response.body)
+ group.reload
+ expect(response_body).to eq({ "displayName" => group.name,
+ "externalId" => external_group_id,
+ "id" => group.id.to_s,
+ "members" => [{ "value" => user2.id.to_s }],
+ "meta" => { "location" => "http://test.host/scim_v2/Groups/#{group.id}",
+ "created" => group.created_at.iso8601,
+ "lastModified" => group.updated_at.iso8601,
+ "resourceType" => "Group" },
+ "schemas" => ["urn:ietf:params:scim:schemas:core:2.0:Group"] })
+ end
+
+ it "adds a member" do
+ group
+ user2
+
+ request_body = {
+ "schemas" =>
+ ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
+ "Operations" => [{
+ "op"=> "add",
+ "path"=> "members",
+ "value"=> [{"value"=> user2.id.to_s},]
+ }]
+ }
+ patch "/scim_v2/Groups/#{group.id}", request_body.to_json, headers
+
+ response_body = JSON.parse(last_response.body)
+ group.reload
+ expect(response_body).to eq({ "displayName" => group.name,
+ "externalId" => external_group_id,
+ "id" => group.id.to_s,
+ "members" => [{ "value" => user1.id.to_s },
+ { "value" => user2.id.to_s }],
+ "meta" => { "location" => "http://test.host/scim_v2/Groups/#{group.id}",
+ "created" => group.created_at.iso8601,
+ "lastModified" => group.updated_at.iso8601,
+ "resourceType" => "Group" },
+ "schemas" => ["urn:ietf:params:scim:schemas:core:2.0:Group"] })
+ end
+
+ it "removes a member" do
+ group
+
+ request_body = {
+ "schemas" =>
+ ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
+ "Operations" => [{
+ "op"=> "remove",
+ "path"=> "members",
+ "value"=> [{"value"=> user1.id.to_s},]
+ }]
+ }
+ patch "/scim_v2/Groups/#{group.id}", request_body.to_json, headers
+
+ response_body = JSON.parse(last_response.body)
+ group.reload
+ expect(response_body).to eq({ "displayName" => group.name,
+ "externalId" => external_group_id,
+ "id" => group.id.to_s,
+ "members" => [],
+ "meta" => { "location" => "http://test.host/scim_v2/Groups/#{group.id}",
+ "created" => group.created_at.iso8601,
+ "lastModified" => group.updated_at.iso8601,
+ "resourceType" => "Group" },
+ "schemas" => ["urn:ietf:params:scim:schemas:core:2.0:Group"] })
+ end
end
context "with the feature flag disabled", with_flag: { scim_api: false } do
diff --git a/spec/requests/scim_v2/schemas_spec.rb b/spec/requests/scim_v2/schemas_spec.rb
new file mode 100644
index 00000000000..f54371c3f59
--- /dev/null
+++ b/spec/requests/scim_v2/schemas_spec.rb
@@ -0,0 +1,70 @@
+# frozen_string_literal: true
+
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 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"
+
+RSpec.describe "SCIM API ServiceProviderConfig" do
+ let(:external_user_id) { "idp_user_id_123asdqwe12345" }
+ let(:external_group_id) { "idp_group_id_123asdqwe12345" }
+ let(:admin) { create(:admin) }
+ let(:oidc_provider) { create(:oidc_provider, slug: "keycloak", creator: admin) }
+ let(:user) { create(:user, identity_url: "#{oidc_provider.slug}:#{external_user_id}") }
+ let(:group) { create(:group, identity_url: "#{oidc_provider.slug}:#{external_group_id}", members: [user]) }
+ let(:headers) { { "CONTENT_TYPE" => "application/scim+json", "HTTP_AUTHORIZATION" => "Bearer access_token" } }
+
+ describe "GET /scim_v2/ServiceProviderConfig" do
+ context "with the feature flag enabled", with_flag: { scim_api: true } do
+ before { group }
+
+ it do
+ get "/scim_v2/Schemas", {}, headers
+
+ response_body = JSON.parse(last_response.body)
+ expect(response_body["totalResults"]).to eq(2)
+ expect(response_body["schemas"]).to eq(["urn:ietf:params:scim:api:messages:2.0:ListResponse"])
+ expect(response_body["schemas"]).to eq(["urn:ietf:params:scim:api:messages:2.0:ListResponse"])
+ group_schema = response_body["Resources"].find { |r| r["name"] == "Group" }
+ user_schema = response_body["Resources"].find { |r| r["name"] == "User" }
+ end
+ end
+
+ context "with the feature flag disabled", with_flag: { scim_api: false } do
+ it do
+ get "/scim_v2/Schemas", {}, headers
+
+ response_body = JSON.parse(last_response.body)
+ expect(response_body).to eq(
+ { "detail" => "Requires authentication", "schemas" => ["urn:ietf:params:scim:api:messages:2.0:Error"],
+ "status" => "401" }
+ )
+ end
+ end
+ end
+end
diff --git a/spec/requests/scim_v2/users_spec.rb b/spec/requests/scim_v2/users_spec.rb
index c94def7034c..9ac7a4d7d28 100644
--- a/spec/requests/scim_v2/users_spec.rb
+++ b/spec/requests/scim_v2/users_spec.rb
@@ -52,7 +52,6 @@ RSpec.describe "SCIM API Users" do
"emails" => [{ "primary" => true,
"type" => "work",
"value" => admin.mail }],
- "externalId" => nil,
"groups" => [],
"id" => admin.id.to_s,
"meta" => { "created" => admin.created_at.iso8601,