Skip to content

Commit 2ba5c0f

Browse files
author
Andrew Burns
committed
use parameter_service
1 parent ab149ec commit 2ba5c0f

File tree

3 files changed

+58
-7
lines changed

3 files changed

+58
-7
lines changed

app/controllers/scim_rails/scim_users_controller.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module ScimRails
22
class ScimUsersController < ScimRails::ApplicationController
3+
before_action :check_allowed_parameters!, only: %i[create patch_update put_update]
4+
35
def index
46
ScimRails.config.before_scim_response.call(request.params) unless ScimRails.config.before_scim_response.nil?
57

@@ -97,7 +99,7 @@ def patch_update
9799

98100
active_param = extract_active_param(operation, path_params)
99101
status = patch_status(active_param)
100-
102+
101103
next if status.nil?
102104

103105
provision_method = status ? ScimRails.config.user_reprovision_method : ScimRails.config.user_deprovision_method
@@ -116,7 +118,7 @@ def delete
116118

117119
user = @company.public_send(ScimRails.config.scim_users_scope).find(params[:id])
118120
user.update!(ScimRails.config.custom_user_attributes)
119-
121+
120122
user.destroy
121123

122124
ScimRails.config.after_scim_response.call(user, "DELETED") unless ScimRails.config.after_scim_response.nil?
@@ -129,5 +131,25 @@ def delete
129131
def get_multi_value_attrs(operation)
130132
schema_hash = contains_square_brackets?(operation["path"]) ? multi_attr_type_to_value(process_filter_path(operation)) : {}
131133
end
134+
135+
def check_allowed_parameters!
136+
schema = ScimRails.config.user_schema.dup
137+
if schema.fetch(:schemas, []).include?("urn:ietf:params:scim:schemas:core:2.0:User")
138+
schema.delete(:schemas)
139+
schema.merge(ParameterService::SCIM_CORE_USER_SCHEMA)
140+
end
141+
142+
bad_fields = ParameterService.invalid_parameters(schema, params)
143+
return if bad_fields.empty?
144+
145+
json_response(
146+
{
147+
schemas: ["urn:ietf:params:scim:api:messages:2.0:Error"],
148+
detail: "Unknown fields: #{bad_fields.join(", ")}",
149+
status: "422"
150+
},
151+
:unprocessable_entity,
152+
)
153+
end
132154
end
133155
end

app/services/parameter_service.rb

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,40 @@ module ParameterService
66
# https://datatracker.ietf.org/doc/html/rfc7643#section-4
77
SCIM_CORE_USER_SCHEMA = {
88
"userName" => String,
9+
"displayName" => String,
10+
"nickName" => String,
911
"name" => {
1012
"formatted" => String,
1113
"familyName" => String,
1214
"givenName" => String,
1315
"middleName" => String,
16+
"honorificPrefix" => String,
17+
"honorificSuffix" => String,
1418
},
19+
"profileUrl" => String,
20+
"title" => String,
21+
"userType" => String,
22+
"preferredLanguage" => String,
23+
"locale" => String,
24+
"timezone" => String,
25+
"active" => String,
26+
"password" => String,
1527
"emails" => [],
28+
"phoneNumbers" => [],
29+
"ims" => String,
30+
"photos" => String,
31+
"addresses" => {
32+
"formatted" => String,
33+
"streetAddress" => String,
34+
"locality" => String,
35+
"region" => String,
36+
"postalCode" => String,
37+
"country" => String,
38+
},
39+
"entitlements" => [],
40+
"roles" => [],
41+
"x509Certificates" => [],
1642
}
17-
1843
# def invalid_params(params, object_type)
1944
# raise "Not supported" unless object_type == "User"
2045
# schema = ScimRails.config.user_schema
@@ -37,9 +62,13 @@ def invalid_parameters(schema, parameters, parent_path: nil)
3762

3863
# Okay... both keys exist.. Did either side specify a subtype?
3964
sub_schema = schema[param_key.to_s]
40-
next unless sub_schema.is_a?(Hash) || param_value.is_a?(Hash)
65+
param_has_subtype = sub_schema.is_a?(Hash) || param_value.is_a?(Hash)
66+
param_has_subtype ||= sub_schema.is_a?(Array) || param_value.is_a?(Array)
67+
next unless param_has_subtype
4168

42-
if param_value.is_a?(Hash) && sub_schema.is_a?(Hash)
69+
if param_value.is_a?(Array) && sub_schema.is_a?(Array)
70+
next
71+
elsif param_value.is_a?(Hash) && sub_schema.is_a?(Hash)
4372
invalid += invalid_parameters(sub_schema, param_value, parent_path: param_path)
4473
else
4574
# One, and only one, of them is a subtype so it is invalid

spec/services/parameter_service_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373

7474
context "nested" do
7575
context "array" do
76-
it "with valid top-level" do
76+
it "top-level" do
7777
params = good_params.merge(
7878
"name" => %w[one two]
7979
)
@@ -82,7 +82,7 @@
8282
expect(result).to eq ["name"]
8383
end
8484

85-
it ">>>" do
85+
it "nested hash" do
8686
params = good_params.merge(
8787
"name" => {
8888
"givenName" => %w[one two]

0 commit comments

Comments
 (0)