scimaenaga 1.0.10 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: da20787f2c3bad068c87e5fd5d4a67a718942703180058aa337c1ece0f3a3f43
4
- data.tar.gz: a42bd3a94f55f36d8460e21e9c23cbafd7cee0b33a89e61777892f63dcac659e
3
+ metadata.gz: b4df3cb922181e1d650ba6eae08634028e7baa685ae74caae3631eecd3ed167a
4
+ data.tar.gz: 6c39da3cb789142f0b36c8515302d04590784590f7327d4e763a5d4b812c7d19
5
5
  SHA512:
6
- metadata.gz: 8489b2338e9ffa589fa7f371cd73f07adbadce7f5f8cfa1026353a3362b6feaed290674e69c536166c84291a0abf7ac8857008f616fac3894ac213fb604478fe
7
- data.tar.gz: d7d631b2daae9d9e33e8242268688c2c601fe881ab4cca406be0eec4cf4f08ca1a2f794482084b4dbb16d7de322902385c2a83aa736a8e70414282cfc4e13a59
6
+ metadata.gz: 0ccdbed86016a6e4c0429eb02be5ca2b1a9610a82a04b6812be783feea307675728d1afd25f04c97f2adbb460a18eb3a70e0a700118f130fd0996cb8847c5707
7
+ data.tar.gz: 9a5d2b8dea62235f54ad7679995cc2c81590ac5c055019eefae402202df2cb4b4e623e3eefec11d4cfb828b88dd8940ee2ce258161b907cf60ed102f49e29e5b
data/README.md CHANGED
@@ -114,6 +114,10 @@ $ curl -X GET 'http://username:password@localhost:3000/scim/v2/Users'
114
114
  ###### Signing Algorithm
115
115
  In the config settings, ensure you set `signing_algorithm` to a valid JWT signing algorithm, e.g "HS256". Defaults to `"none"` when not set.
116
116
 
117
+ Do not leave it `"none"` in production, even if you only use basic authentication. An unsigned token contains nothing but the searchable attribute and the issue time (`iat`), so it can be guessed from the issue time alone. This follows [RFC 8725 Section 3.2](https://www.rfc-editor.org/rfc/rfc8725#section-3.2), which allows `"none"` only when the token is protected by other means.
118
+
119
+ In the production environment, `Scimaenaga::Encoder.encode` raises `Scimaenaga::ExceptionHandler::InvalidConfiguration` when `signing_algorithm` is `"none"`. Tokens already issued with `"none"` are still accepted, so re-issue them after you change the algorithm.
120
+
117
121
  ###### Signing Secret
118
122
  In the config settings, ensure you set `signing_secret` to a secret key that will be used to encode and decode tokens. Defaults to `nil` when not set.
119
123
 
@@ -61,6 +61,7 @@ module Scimaenaga
61
61
  .public_send(Scimaenaga.config.scim_groups_scope)
62
62
  .find(params[:id])
63
63
  patch = ScimPatch.new(params, :group)
64
+ verify_member_ids!(patch.operations.flat_map(&:member_ids_to_assign))
64
65
  patch.save(group)
65
66
 
66
67
  json_scim_response(object: group)
@@ -101,12 +102,24 @@ module Scimaenaga
101
102
  end
102
103
 
103
104
  def member_params
104
- {
105
- Scimaenaga.config.group_member_relation_attribute =>
106
- params[:members].map do |member|
107
- member[Scimaenaga.config.group_member_relation_schema.keys.first]
108
- end,
109
- }
105
+ member_ids = params[:members].map do |member|
106
+ member[Scimaenaga.config.group_member_relation_schema.keys.first]
107
+ end
108
+ verify_member_ids!(member_ids)
109
+ { Scimaenaga.config.group_member_relation_attribute => member_ids }
110
+ end
111
+
112
+ # Without this a client could attach users of another company to its own group.
113
+ # Ids are normalized the same way as the relation writer (e.g. user_ids=) does,
114
+ # so ids it accepts ("01", blank) are not rejected here.
115
+ def verify_member_ids!(member_ids)
116
+ users = @company.public_send(Scimaenaga.config.scim_users_scope)
117
+ pk_type = users.klass.type_for_attribute(users.primary_key)
118
+ ids = member_ids.compact_blank.map { |id| pk_type.cast(id) }.uniq
119
+ missing_ids = ids - users.where(users.primary_key => ids).ids
120
+ return if missing_ids.empty?
121
+
122
+ raise ExceptionHandler::ResourceNotFound, missing_ids.join(', ')
110
123
  end
111
124
 
112
125
  def mutable_attributes
@@ -21,7 +21,10 @@ class ScimPatchOperation
21
21
  @path_scim = parse_path_scim(path)
22
22
  @path_sp = path_scim_to_path_sp(@path_scim)
23
23
 
24
- # define parse method in the inherited class
24
+ # define mutable? method in the inherited class
25
+ return if mutable?(@path_scim, @path_sp)
26
+
27
+ raise Scimaenaga::ExceptionHandler::UnsupportedPatchRequest
25
28
  end
26
29
 
27
30
  private
@@ -16,8 +16,27 @@ class ScimPatchOperationGroup < ScimPatchOperation
16
16
  end
17
17
  end
18
18
 
19
+ # Member ids this operation will attach to the group ('add' / 'replace').
20
+ # 'remove' never attaches anything, so it returns [].
21
+ # Ids must be read the same way #save reads them, or the check can be bypassed.
22
+ # String elements are read too, because #save attaches 'value'['value'] ('value').
23
+ # A value that #save cannot read (not an Array, or elements such as Integer or nil)
24
+ # is skipped on purpose: this method is called before #save, and raising here
25
+ # would turn the 422 that #save raises into a 500.
26
+ def member_ids_to_assign
27
+ return [] unless @path_scim[:attribute] == 'members'
28
+ return [] if @op == 'remove'
29
+ return [] unless @value.is_a?(Array)
30
+
31
+ @value.select { |v| readable_member?(v) }.map { |v| v['value'].to_s }
32
+ end
33
+
19
34
  private
20
35
 
36
+ def readable_member?(value)
37
+ [Hash, ActionController::Parameters, String].any? { |klass| value.is_a?(klass) }
38
+ end
39
+
21
40
  def save_members(model)
22
41
  current_member_ids = model.public_send(member_relation_attribute).map(&:to_s)
23
42
 
@@ -68,6 +87,12 @@ class ScimPatchOperationGroup < ScimPatchOperation
68
87
  return
69
88
  end
70
89
 
90
+ def mutable?(path_scim, path_sp)
91
+ return true if path_scim[:attribute] == 'members'
92
+
93
+ Array(Scimaenaga.config.mutable_group_attributes).include?(path_sp)
94
+ end
95
+
71
96
  def path_scim_to_path_sp(path_scim)
72
97
  # path_scim example1:
73
98
  # {
@@ -21,6 +21,10 @@ class ScimPatchOperationUser < ScimPatchOperation
21
21
  return
22
22
  end
23
23
 
24
+ def mutable?(_path_scim, path_sp)
25
+ Array(Scimaenaga.config.mutable_user_attributes).include?(path_sp)
26
+ end
27
+
24
28
  def path_scim_to_path_sp(path_scim)
25
29
  # path_scim example1:
26
30
  # {
@@ -1,5 +1,5 @@
1
1
  Mime::Type.register "application/scim+json", :scimjson
2
2
 
3
- ActionDispatch::Request.parameter_parsers[:scimjson] = lambda do |body|
4
- ActiveSupport::JSON.decode(body)
5
- end
3
+ original_parsers = ActionDispatch::Request.parameter_parsers
4
+ parsers = original_parsers.merge(scimjson: ->(body) { ActiveSupport::JSON.decode(body) })
5
+ ActionDispatch::Request.parameter_parsers = parsers
@@ -33,13 +33,14 @@ Scimaenaga.configure do |config|
33
33
  # Cryptographic algorithm used for signing the auth tokens.
34
34
  # It supports all algorithms supported by the jwt gem.
35
35
  # See https://github.com/jwt/ruby-jwt#algorithms-and-usage for supported algorithms
36
- # It is "none" by default, hence generated tokens are unsigned
37
- # The tokens do not need to be signed if you only need basic authentication.
36
+ # It is "none" by default, hence generated tokens are unsigned.
37
+ # Do not leave it "none" in production: an unsigned token contains only
38
+ # the searchable attribute and the issue time, so it can be guessed.
39
+ # Scimaenaga::Encoder.encode raises in production when it is "none".
38
40
  # config.signing_algorithm = "HS256"
39
41
 
40
42
  # Secret token used to sign authorization tokens
41
- # It is `nil` by default, hence generated tokens are unsigned
42
- # The tokens do not need to be signed if you only need basic authentication.
43
+ # It is `nil` by default, hence generated tokens are unsigned.
43
44
  # config.signing_secret = SECRET_TOKEN
44
45
 
45
46
  # Default sort order for pagination is by id. If you
@@ -5,6 +5,8 @@ module Scimaenaga
5
5
  extend self
6
6
 
7
7
  def encode(company)
8
+ refuse_unsigned_in_production
9
+
8
10
  payload = {
9
11
  iat: Time.current.to_i,
10
12
  Scimaenaga.config.basic_auth_model_searchable_attribute =>
@@ -23,5 +25,18 @@ module Scimaenaga
23
25
  rescue JWT::VerificationError, JWT::DecodeError
24
26
  raise Scimaenaga::ExceptionHandler::InvalidCredentials
25
27
  end
28
+
29
+ private
30
+
31
+ def refuse_unsigned_in_production
32
+ return unless Rails.env.production?
33
+
34
+ # The jwt gem also treats "NONE" and :none as "none".
35
+ algorithm = Scimaenaga.config.signing_algorithm.to_s
36
+ return unless algorithm.casecmp?(Scimaenaga::Config::ALGO_NONE)
37
+
38
+ raise Scimaenaga::ExceptionHandler::InvalidConfiguration,
39
+ 'signing_algorithm must not be "none" in production'
40
+ end
26
41
  end
27
42
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Scimaenaga
4
- VERSION = '1.0.10'
4
+ VERSION = '2.0.0'
5
5
  end
@@ -289,6 +289,30 @@ RSpec.describe Scimaenaga::ScimGroupsController, type: :controller do
289
289
  expect(company.groups.count).to eq 1
290
290
  end
291
291
 
292
+ it 'returns :not_found when a member belongs to another company' do
293
+ other_user = create(:user, company: create(:company, subdomain: 'other'))
294
+
295
+ post :create, params: {
296
+ displayName: 'Test Group',
297
+ members: [{ value: other_user.id.to_s }],
298
+ }, as: :json
299
+
300
+ expect(response.status).to eq 404
301
+ expect(company.groups.count).to eq 0
302
+ end
303
+
304
+ it 'accepts member ids in a form the relation writer accepts' do
305
+ user = create(:user, company: company)
306
+
307
+ post :create, params: {
308
+ displayName: 'Test Group',
309
+ members: [{ value: "0#{user.id}" }, { value: nil }],
310
+ }, as: :json
311
+
312
+ expect(response.status).to eq 201
313
+ expect(company.groups.first.users).to eq [user]
314
+ end
315
+
292
316
  it 'creates group' do
293
317
  users = create_list(:user, 3, company: company)
294
318
 
@@ -388,6 +412,16 @@ RSpec.describe Scimaenaga::ScimGroupsController, type: :controller do
388
412
 
389
413
  expect(response.status).to eq 422
390
414
  end
415
+
416
+ it 'returns :not_found and does not add a User of another company' do
417
+ other_user = create(:user, company: create(:company, subdomain: 'other'))
418
+
419
+ expect do
420
+ put :put_update, params: put_params(users: [other_user]), as: :json
421
+ end.not_to(change { group.reload.users.to_a })
422
+
423
+ expect(response.status).to eq 404
424
+ end
391
425
  end
392
426
  end
393
427
 
@@ -451,6 +485,120 @@ RSpec.describe Scimaenaga::ScimGroupsController, type: :controller do
451
485
  expect(response.status).to eq 404
452
486
  end
453
487
 
488
+ it 'returns :not_found and does not add a User of another company' do
489
+ other_user = create(:user, company: create(:company, subdomain: 'other'))
490
+
491
+ expect do
492
+ patch :patch_update, params: patch_params(user_id: other_user.id), as: :json
493
+ end.not_to(change { group.reload.users.to_a })
494
+
495
+ expect(response.status).to eq 404
496
+ response_body = JSON.parse(response.body)
497
+ expect(response_body['detail']).to eq "Resource #{other_user.id} not found."
498
+ end
499
+
500
+ it 'returns :not_found for another company User mixed with a non-Hash' do
501
+ other_user = create(:user, company: create(:company, subdomain: 'other'))
502
+
503
+ expect do
504
+ patch :patch_update, params: {
505
+ id: group.id,
506
+ schemas: ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],
507
+ Operations: [{
508
+ op: 'Add',
509
+ path: 'members',
510
+ value: ['abc', { value: other_user.id }],
511
+ }],
512
+ }, as: :json
513
+ end.not_to(change { group.reload.users.to_a })
514
+
515
+ expect(response.status).to eq 404
516
+ end
517
+
518
+ it 'can remove a User even if the id is not in the company' do
519
+ expect do
520
+ patch :patch_update, params: patch_params(user_id: 0, op: 'Remove'), as: :json
521
+ end.not_to(change { group.reload.users.to_a })
522
+
523
+ expect(response.status).to eq 200
524
+ end
525
+
526
+ it 'returns :not_found when replacing with a User of another company' do
527
+ other_user = create(:user, company: create(:company, subdomain: 'other'))
528
+
529
+ expect do
530
+ patch :patch_update,
531
+ params: patch_params(user_id: other_user.id, op: 'Replace'), as: :json
532
+ end.not_to(change { group.reload.users.to_a })
533
+
534
+ expect(response.status).to eq 404
535
+ end
536
+
537
+ it 'returns :not_found when members mix own and other company Users' do
538
+ other_user = create(:user, company: create(:company, subdomain: 'other'))
539
+
540
+ expect do
541
+ patch :patch_update, params: {
542
+ id: group.id,
543
+ schemas: ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],
544
+ Operations: [{
545
+ op: 'Add',
546
+ path: 'members',
547
+ value: [
548
+ { value: user2.id },
549
+ { value: other_user.id }
550
+ ],
551
+ }],
552
+ }, as: :json
553
+ end.not_to(change { group.reload.users.to_a })
554
+
555
+ expect(response.status).to eq 404
556
+ end
557
+
558
+ it 'returns 422 when members value is not an array' do
559
+ patch :patch_update, params: {
560
+ id: group.id,
561
+ schemas: ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],
562
+ Operations: [{
563
+ op: 'Add',
564
+ path: 'members',
565
+ value: user2.id.to_s,
566
+ }],
567
+ }, as: :json
568
+
569
+ expect(response.status).to eq 422
570
+ end
571
+
572
+ it 'returns 422 when a members element is an Integer' do
573
+ patch :patch_update, params: {
574
+ id: group.id,
575
+ schemas: ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],
576
+ Operations: [{
577
+ op: 'Add',
578
+ path: 'members',
579
+ value: [user2.id],
580
+ }],
581
+ }, as: :json
582
+
583
+ expect(response.status).to eq 422
584
+ end
585
+
586
+ it 'accepts member ids in a form the relation writer accepts' do
587
+ expect do
588
+ patch :patch_update, params: {
589
+ id: group.id,
590
+ schemas: ['urn:ietf:params:scim:api:messages:2.0:PatchOp'],
591
+ Operations: [{
592
+ op: 'Add',
593
+ path: 'members',
594
+ value: [{ value: "0#{user2.id}" }, { value: nil }],
595
+ }],
596
+ }, as: :json
597
+ end.to change { group.reload.users }.from([user1]).to([user1, user2])
598
+
599
+ expect(response.status).to eq 200
600
+ end
601
+
454
602
  it 'rollback if even one cannot be saved' do
455
603
  expect do
456
604
  patch :patch_update, params: {
Binary file