scimitar 1.10.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. checksums.yaml +4 -4
  2. data/app/controllers/scimitar/active_record_backed_resources_controller.rb +23 -98
  3. data/app/controllers/scimitar/application_controller.rb +13 -41
  4. data/app/controllers/scimitar/resource_types_controller.rb +2 -0
  5. data/app/controllers/scimitar/resources_controller.rb +2 -0
  6. data/app/controllers/scimitar/schemas_controller.rb +3 -366
  7. data/app/controllers/scimitar/service_provider_configurations_controller.rb +1 -0
  8. data/app/models/scimitar/complex_types/address.rb +6 -0
  9. data/app/models/scimitar/engine_configuration.rb +5 -15
  10. data/app/models/scimitar/error_response.rb +0 -12
  11. data/app/models/scimitar/lists/query_parser.rb +13 -113
  12. data/app/models/scimitar/resource_invalid_error.rb +1 -1
  13. data/app/models/scimitar/resources/base.rb +9 -53
  14. data/app/models/scimitar/resources/mixin.rb +59 -646
  15. data/app/models/scimitar/schema/address.rb +0 -1
  16. data/app/models/scimitar/schema/attribute.rb +5 -14
  17. data/app/models/scimitar/schema/base.rb +1 -1
  18. data/app/models/scimitar/schema/name.rb +2 -2
  19. data/app/models/scimitar/schema/user.rb +10 -10
  20. data/app/models/scimitar/schema/vdtp.rb +1 -1
  21. data/app/models/scimitar/service_provider_configuration.rb +3 -14
  22. data/config/initializers/scimitar.rb +3 -69
  23. data/lib/scimitar/engine.rb +12 -57
  24. data/lib/scimitar/support/hash_with_indifferent_case_insensitive_access.rb +10 -140
  25. data/lib/scimitar/version.rb +2 -2
  26. data/lib/scimitar.rb +2 -7
  27. data/spec/apps/dummy/app/controllers/mock_groups_controller.rb +1 -1
  28. data/spec/apps/dummy/app/models/mock_group.rb +1 -1
  29. data/spec/apps/dummy/app/models/mock_user.rb +9 -52
  30. data/spec/apps/dummy/config/application.rb +1 -0
  31. data/spec/apps/dummy/config/environments/test.rb +28 -5
  32. data/spec/apps/dummy/config/initializers/scimitar.rb +10 -90
  33. data/spec/apps/dummy/config/routes.rb +7 -28
  34. data/spec/apps/dummy/db/migrate/20210304014602_create_mock_users.rb +1 -11
  35. data/spec/apps/dummy/db/migrate/20210308044214_create_join_table_mock_groups_mock_users.rb +3 -8
  36. data/spec/apps/dummy/db/schema.rb +4 -12
  37. data/spec/controllers/scimitar/application_controller_spec.rb +3 -126
  38. data/spec/controllers/scimitar/resource_types_controller_spec.rb +2 -2
  39. data/spec/controllers/scimitar/schemas_controller_spec.rb +48 -344
  40. data/spec/models/scimitar/complex_types/address_spec.rb +4 -3
  41. data/spec/models/scimitar/complex_types/email_spec.rb +2 -0
  42. data/spec/models/scimitar/lists/query_parser_spec.rb +9 -146
  43. data/spec/models/scimitar/resources/base_spec.rb +71 -217
  44. data/spec/models/scimitar/resources/base_validation_spec.rb +5 -43
  45. data/spec/models/scimitar/resources/mixin_spec.rb +129 -1508
  46. data/spec/models/scimitar/schema/attribute_spec.rb +3 -22
  47. data/spec/models/scimitar/schema/base_spec.rb +1 -1
  48. data/spec/models/scimitar/schema/user_spec.rb +2 -12
  49. data/spec/requests/active_record_backed_resources_controller_spec.rb +66 -1016
  50. data/spec/requests/application_controller_spec.rb +3 -16
  51. data/spec/requests/engine_spec.rb +0 -75
  52. data/spec/spec_helper.rb +1 -9
  53. data/spec/support/hash_with_indifferent_case_insensitive_access_spec.rb +0 -108
  54. metadata +26 -37
  55. data/LICENSE.txt +0 -21
  56. data/README.md +0 -717
  57. data/lib/scimitar/support/utilities.rb +0 -111
  58. data/spec/apps/dummy/app/controllers/custom_create_mock_users_controller.rb +0 -25
  59. data/spec/apps/dummy/app/controllers/custom_replace_mock_users_controller.rb +0 -25
  60. data/spec/apps/dummy/app/controllers/custom_save_mock_users_controller.rb +0 -24
  61. data/spec/apps/dummy/app/controllers/custom_update_mock_users_controller.rb +0 -25
data/README.md DELETED
@@ -1,717 +0,0 @@
1
- # Scimitar
2
-
3
- [![Gem Version](https://badge.fury.io/rb/scimitar.svg)](https://badge.fury.io/rb/scimitar)
4
- [![Build Status](https://app.travis-ci.com/RIPAGlobal/scimitar.svg?branch=main)](https://app.travis-ci.com/RIPAGlobal/scimitar)
5
- [![License](https://img.shields.io/badge/license-mit-blue.svg)](https://opensource.org/licenses/MIT)
6
-
7
- A SCIM v2 API endpoint implementation for Ruby On Rails.
8
-
9
- For a list of changes and information on major version upgrades, please see `CHANGELOG.md`.
10
-
11
-
12
-
13
- ## Overview
14
-
15
- System for Cross-domain Identity Management (SCIM) is a protocol that helps systems synchronise user data between different business systems. A _service provider_ hosts a SCIM API endpoint implementation and the Scimitar gem is used to help quickly build this implementation. One or more _enterprise subscribers_ use these APIs to let that service know about changes in the enterprise's user (employee) list.
16
-
17
- In the context of the names used by the SCIM standard, the service that is provided is some kind of software-as-a-service solution that the enterprise subscriber uses to assist with their day to day business. The enterprise maintains its user (employee) list via whatever means it wants, but includes SCIM support so that any third party services it uses can be kept up to date with adds, removals or changes to employee data.
18
-
19
- * [Overview](https://en.wikipedia.org/wiki/System_for_Cross-domain_Identity_Management) at Wikipedia
20
- * [More detailed introduction](http://www.simplecloud.info) at SimpleCloud
21
- * SCIM v2 RFC [7642](https://tools.ietf.org/html/rfc7642): Concepts
22
- * SCIM v2 RFC [7643](https://tools.ietf.org/html/rfc7643): Core schema
23
- * SCIM v2 RFC [7644](https://tools.ietf.org/html/rfc7644): Protocol
24
-
25
-
26
-
27
- ## Installation
28
-
29
- Install using:
30
-
31
- ```shell
32
- gem install scimitar
33
- ```
34
-
35
- In your Gemfile:
36
-
37
- ```ruby
38
- gem 'scimitar', '~> 1.0'
39
- ```
40
-
41
- Scimitar uses [semantic versioning](https://semver.org) so you can be confident that patch and minor version updates for features, bug fixes and/or security patches will not break your application.
42
-
43
-
44
-
45
- ## Heritage
46
-
47
- Scimitar borrows heavily - to the point of cut-and-paste - from:
48
-
49
- * [ScimEngine](https://github.com/Cisco-AMP/scim_engine) for the Rails controllers and resource-agnostic subclassing approach that makes supporting User and/or Group, along with custom resource types if you need them, quite easy.
50
- * [ScimRails](https://github.com/lessonly/scim_rails) for the bearer token support, 'index' actions and filter support.
51
- * [SCIM Query Filter Parser](https://github.com/ingydotnet/scim-query-filter-parser-rb) for advanced filter handling.
52
-
53
- All three are provided under the MIT license. Scimitar is too.
54
-
55
-
56
-
57
- ## Usage
58
-
59
- Scimitar is best used with Rails and ActiveRecord, but it can be used with other persistence back-ends too - you just have to do more of the work in controllers using Scimitar's lower level controller subclasses, rather than relying on Scimitar's higher level ActiveRecord abstractions.
60
-
61
- ### Authentication
62
-
63
- Noting the _Security_ section later - to set up an authentication method, create a `config/initializers/scimitar.rb` in your Rails application and define a token-based authenticator and/or a username-password authenticator in the [engine configuration section documented in the sample file](https://github.com/RIPAGlobal/scimitar/blob/main/config/initializers/scimitar.rb). For example:
64
-
65
- ```ruby
66
- Scimitar.engine_configuration = Scimitar::EngineConfiguration.new({
67
- token_authenticator: Proc.new do | token, options |
68
-
69
- # This is where you'd write the code to validate `token` - the means by
70
- # which your application issues tokens to SCIM clients, or validates them,
71
- # is outside the scope of the gem; the required mechanisms vary by client.
72
- # More on this can be found in the 'Security' section later.
73
- #
74
- SomeLibraryModule.validate_access_token(token)
75
-
76
- end
77
- })
78
- ```
79
-
80
- When it comes to token access, Scimitar neither enforces nor presumes any kind of encoding for bearer tokens. You can use anything you like, including encoding/encrypting JWTs if you so wish - https://rubygems.org/gems/jwt may be useful. The way in which a client might integrate with your SCIM service varies by client and you will have to check documentation to see how a token gets conveyed to that client in the first place (e.g. a full OAuth flow with your application, or just a static token generated in some UI which an administrator copies and pastes into their client's SCIM configuration UI).
81
-
82
- **Strongly recommended:** You should wrap any Scimitar configuration with `Rails.application.config.to_prepare do...` so that any changes you make to configuration during local development are reflected via auto-reload, rather than requiring a server restart.
83
-
84
- ```ruby
85
- Rails.application.config.to_prepare do
86
- Scimitar.engine_configuration = Scimitar::EngineConfiguration.new({
87
- # ...
88
- end
89
- end
90
- ```
91
-
92
- In general, Scimitar's own development and tests assume this approach. If you choose to put the configuration directly into an initializer file without the `to_prepare` wrapper, you will be at a _slightly_ higher risk of tripping over unrecognised Scimitar bugs; please make sure that your own application test coverage is reasonably comprehensive.
93
-
94
- ### Routes
95
-
96
- For each resource you support, add these lines to your `routes.rb`:
97
-
98
- ```ruby
99
- namespace :scim_v2 do
100
- mount Scimitar::Engine, at: '/'
101
-
102
- get 'Users', to: 'users#index'
103
- get 'Users/:id', to: 'users#show'
104
- post 'Users', to: 'users#create'
105
- put 'Users/:id', to: 'users#replace'
106
- patch 'Users/:id', to: 'users#update'
107
- delete 'Users/:id', to: 'users#destroy'
108
- end
109
- ```
110
-
111
- All routes then will be available at `https://.../scim_v2/...` via controllers you write in `app/controllers/scim_v2/...`, e.g. `app/controllers/scim_v2/users_controller.rb`. More on controllers later.
112
-
113
- #### URL helpers
114
-
115
- Internally Scimitar always invokes URL helpers in the controller layer. I.e. any variable path parameters will be resolved by Rails automatically. If you need more control over the way URLs are generated you can override any URL helper by redefining it in the application controller mixin. See the [`application_controller_mixin` engine configuration option](https://github.com/RIPAGlobal/scimitar/blob/main/config/initializers/scimitar.rb).
116
-
117
- ### Data models
118
-
119
- Scimitar assumes that each SCIM resource maps to a single corresponding class in your system. This might be an abstraction over more complex underpinings, but either way, a 1:1 relationship is expected. For example, a SCIM User might map to a User ActiveRecord model in your Rails application, while a SCIM Group might map to some custom class called Team which operates on a more complex set of data "under the hood".
120
-
121
- Before writing any controllers, it's a good idea to examine the SCIM specification and figure out how you intend to map SCIM attributes in any resources of interest, to your local data. A [mixin is provided](https://github.com/RIPAGlobal/scimitar/blob/main/app/models/scimitar/resources/mixin.rb) which you can include in any plain old Ruby class (including, but not limited to ActiveRecord model classes) - a more readable form of the comments in this file [is in the RDoc output](https://www.rubydoc.info/gems/scimitar/Scimitar/Resources/Mixin).
122
-
123
- The functionality exposed by the mixin is relatively complicated because the range of operations that the SCIM API supports is quite extensive. Rather than duplicate all the information here, please see the extensive comments in the mixin linked above for more information. There are examples in the [test suite's Rails models](https://github.com/RIPAGlobal/scimitar/tree/main/spec/apps/dummy/app/models), or for another example:
124
-
125
- ```ruby
126
- class User < ActiveRecord::Base
127
-
128
- # The attributes in the SCIM section below include a reference to this
129
- # hypothesised 'groups' HABTM relationship. All of the other "leaf node"
130
- # Symbols - e.g. ":first_name", ":last_name" - are expected to be defined as
131
- # accessors e.g. via ActiveRecord and your related database table columns,
132
- # "attr_accessor" declarations, or bespoke "def foo"/"def foo=(value)". If a
133
- # write accessor is not present, the attribute will not be writable via SCIM.
134
- #
135
- has_and_belongs_to_many :groups
136
-
137
- # ===========================================================================
138
- # SCIM MIXIN AND REQUIRED METHODS
139
- # ===========================================================================
140
- #
141
- # All class methods shown below are mandatory unless otherwise commented.
142
-
143
- def self.scim_resource_type
144
- return Scimitar::Resources::User
145
- end
146
-
147
- def self.scim_attributes_map
148
- return {
149
- id: :id,
150
- externalId: :scim_uid,
151
- userName: :username,
152
- name: {
153
- givenName: :first_name,
154
- familyName: :last_name
155
- },
156
- emails: [
157
- {
158
- match: 'type',
159
- with: 'work',
160
- using: {
161
- value: :work_email_address,
162
- primary: true
163
- }
164
- },
165
- {
166
- match: 'type',
167
- with: 'home',
168
- using: {
169
- value: :home_email_address,
170
- primary: false
171
- }
172
- },
173
- ],
174
- phoneNumbers: [
175
- {
176
- match: 'type',
177
- with: 'work',
178
- using: {
179
- value: :work_phone_number,
180
- primary: false
181
- }
182
- },
183
- ],
184
-
185
- # NB The 'groups' collection in a SCIM User resource is read-only, so
186
- # we provide no ":find_with" key for looking up records for writing
187
- # updates to the associated collection.
188
- #
189
- groups: [
190
- {
191
- list: :groups,
192
- using: {
193
- value: :id,
194
- display: :display_name
195
- }
196
- }
197
- ],
198
- active: :is_active
199
- }
200
- end
201
-
202
- def self.scim_mutable_attributes
203
- return nil
204
- end
205
-
206
- # The attributes in this example include a reference to the same hypothesised
207
- # 'Group' model as in the HABTM relationship above. In this case, in order to
208
- # filter by "groups" or "groups.value", the 'column' entry must reference the
209
- # Group model's ID column as an AREL attribute as shown below, and the SCIM
210
- # controller's #storage_scope implementation must also introduce a #join with
211
- # ':groups' - see the "Queries & Optimisations" section below.
212
- #
213
- def self.scim_queryable_attributes
214
- return {
215
- givenName: { column: :first_name },
216
- familyName: { column: :last_name },
217
- emails: { column: :work_email_address },
218
- groups: { column: Group.arel_table[:id] },
219
- "groups.value" => { column: Group.arel_table[:id] },
220
- }
221
- end
222
-
223
- # Optional but recommended.
224
- #
225
- def self.scim_timestamps_map
226
- {
227
- created: :created_at,
228
- lastModified: :updated_at
229
- }
230
- end
231
-
232
- # If you omit any mandatory declarations, you'll get an exception raised by
233
- # this inclusion which tells you which method(s) need(s) to be added.
234
- #
235
- include Scimitar::Resources::Mixin
236
- end
237
- ```
238
-
239
- ### Controllers
240
-
241
- #### ActiveRecord
242
-
243
- If you use ActiveRecord, your controllers can potentially be extremely simple by subclassing [`Scimitar::ActiveRecordBackedResourcesController`](https://www.rubydoc.info/gems/scimitar/Scimitar/ActiveRecordBackedResourcesController) - at a minimum:
244
-
245
- ```ruby
246
- module Scim
247
- class UsersController < Scimitar::ActiveRecordBackedResourcesController
248
-
249
- protected
250
-
251
- def storage_class
252
- User
253
- end
254
-
255
- def storage_scope
256
- User.all # Or e.g. "User.where(is_deleted: false)" - whatever base scope you require
257
- end
258
-
259
- end
260
- end
261
- ```
262
-
263
- All data-layer actions are taken via `#find` or `#save!`, with exceptions such as `ActiveRecord::RecordNotFound`, `ActiveRecord::RecordInvalid` or generalised SCIM exceptions handled by various superclasses. For a real Rails example of this, see the [test suite's controllers](https://github.com/RIPAGlobal/scimitar/tree/main/spec/apps/dummy/app/controllers) which are invoked via its [routing declarations](https://github.com/RIPAGlobal/scimitar/blob/main/spec/apps/dummy/config/routes.rb).
264
-
265
- ##### Overriding controller methods
266
-
267
- You can overwrite write-based controller methods `#create`, `#update`, `#replace` and `#destroy` in your controller subclass, should you wish, wherein a call to `super` is passed a block. The block is invoked with the instance of a new unsaved record for `#create`, the updated record that needs to have its changes saved for `#update` and `#replace` and the record that should be destroyed for `#destroy`. This allows you to do things like applying business logic, default values, extra request-derived data and so-forth before then calling `record.save!`, or using some different method other than `record.destroy!` to discard a record (e.g. you might be using soft-delete, or want to skip all callbacks for some reason via `record.delete`).
268
-
269
- * The `#destroy` method just calls `record.destroy!` unless a block is given, with nothing much else to say about it.
270
-
271
- * The other methods all establish a database transaction and call through to the _controller's_ protected `#save!` method, passing it the record; it is _this_ method which then either calls `record.save!` or invokes a block. Using the exception-throwing versions of persistence methods is recommended, as there is exception handling within the controller's implementation which rescues things like `ActiveRecord::RecordInvalid` and builds an appropriate SCIM error response when they occur. You can change the list of exceptions handled in this way by overriding protected method `#scimitar_rescuable_exceptions'.
272
-
273
- * If you want to override saving behaviour for both new and modified records, overriding `#save!` in your controller subclass, rather than overriding all of `#create`, `#update` and `#replace`, is likely to be the better choice.
274
-
275
- * For more information, see the [RDoc output for `Scimitar::ActiveRecordBackedResourcesController`](https://www.rubydoc.info/github/RIPAGlobal/scimitar/main/Scimitar/ActiveRecordBackedResourcesController).
276
-
277
- Example:
278
-
279
- ```ruby
280
- module Scim
281
- class UsersController < Scimitar::ActiveRecordBackedResourcesController
282
-
283
- # Create all new records with some special internal field set to a value
284
- # determined by a bespoke-to-your-application mechanism.
285
- #
286
- def create
287
- super do | user |
288
- user.some_special_on_creation_field = method_that_calculates_value()
289
- user.save!
290
- end
291
- end
292
-
293
- # Use #discard! rather than #destroy! as an example of soft-delete via the
294
- # 'discard' gem - https://rubygems.org/gems/discard.
295
- #
296
- def destroy
297
- super do | user |
298
- user.discard!
299
- end
300
- end
301
- end
302
- ```
303
-
304
- #### Queries & Optimisations
305
-
306
- The scope can be optimised to eager load the data exposed by the SCIM interface, i.e.:
307
-
308
- ```ruby
309
- def storage_scope
310
- User.eager_load(:groups)
311
- end
312
- ```
313
-
314
- In cases where you have references to related columns in your `scim_queryable_attributes`, your `storage_scope` must join the relation:
315
-
316
- ```ruby
317
- def storage_scope
318
- User.left_join(:groups)
319
- end
320
- ```
321
-
322
- #### Other source types
323
-
324
- If you do _not_ use ActiveRecord to store data, or if you have very esoteric read-write requirements, you can subclass [`Scimigar::ResourcesController`](https://www.rubydoc.info/gems/scimitar/Scimitar/ResourcesController) in a manner similar to this:
325
-
326
- ```ruby
327
- class UsersController < Scimitar::ResourcesController
328
-
329
- # SCIM clients don't use Rails CSRF tokens.
330
- #
331
- skip_before_action :verify_authenticity_token
332
-
333
- # If you have any filters you need to run BEFORE authentication done in
334
- # the superclass (typically set up in config/initializers/scimitar.rb),
335
- # then use "prepend_before_filter to declare these - else Scimitar's
336
- # own authorisation before-action filter would always run first.
337
-
338
- def index
339
- # There's a degree of heavy lifting for arbitrary storage engines.
340
- query = if params[:filter].present?
341
- attribute_map = User.new.scim_queryable_attributes() # Note use of *instance* method
342
- parser = Scimitar::Lists::QueryParser.new(attribute_map)
343
-
344
- parser.parse(params[:filter])
345
- # Then use 'parser' to read e.g. #tree or #rpn and turn this into a
346
- # query object for your storage engine. With ActiveRecord, you could
347
- # just do: parser.to_activerecord_query(base_scope)
348
- else
349
- # Return a query object for 'all results' (e.g. User.all).
350
- end
351
-
352
- # Assuming the 'query' object above had ActiveRecord-like semantics,
353
- # you'd create a Scimitar::Lists::Count object with total count filled in
354
- # via #scim_pagination_info and obtain a page of results with something
355
- # like the code shown below.
356
- pagination_info = scim_pagination_info(query.count())
357
- page_of_results = query.offset(pagination_info.offset).limit(pagination_info.limit).to_a
358
-
359
- super(pagination_info, page_of_results) do | record |
360
- # Return each instance as a SCIM object, e.g. via Scimitar::Resources::Mixin#to_scim
361
- record.to_scim(location: url_for(action: :show, id: record.id))
362
- end
363
- end
364
-
365
- def show
366
- super do |user_id|
367
- user = find_user(user_id)
368
- # Evaluate to the record as a SCIM object, e.g. via Scimitar::Resources::Mixin#to_scim
369
- user.to_scim(location: url_for(action: :show, id: user_id))
370
- end
371
- end
372
-
373
- def create
374
- super do |scim_resource|
375
- # Create an instance based on the Scimitar::Resources::User in
376
- # "scim_resource" (or whatever your ::storage_class() defines via its
377
- # ::scim_resource_type class method).
378
- record = self.storage_class().new
379
- record.from_scim!(scim_hash: scim_resource.as_json())
380
- self.save!(record)
381
- # Evaluate to the record as a SCIM object (or do that via "self.save!")
382
- user.to_scim(location: url_for(action: :show, id: record.id))
383
- end
384
- end
385
-
386
- def replace
387
- super do |record_id, scim_resource|
388
- # Fully update an instance based on the Scimitar::Resources::User in
389
- # "scim_resource" (or whatever your ::storage_class() defines via its
390
- # ::scim_resource_type class method). For example:
391
- record = self.find_record(record_id)
392
- record.from_scim!(scim_hash: scim_resource.as_json())
393
- self.save!(record)
394
- # Evaluate to the record as a SCIM object (or do that via "self.save!")
395
- user.to_scim(location: url_for(action: :show, id: record_id))
396
- end
397
- end
398
-
399
- def update
400
- super do |record_id, patch_hash|
401
- # Partially update an instance based on the PATCH payload *Hash* given
402
- # in "patch_hash" (note that unlike the "scim_resource" parameter given
403
- # to blocks in #create or #replace, this is *not* a high-level object).
404
- record = self.find_record(record_id)
405
- record.from_scim_patch!(patch_hash: patch_hash)
406
- self.save!(record)
407
- # Evaluate to the record as a SCIM object (or do that via "self.save!")
408
- user.to_scim(location: url_for(action: :show, id: record_id))
409
- end
410
- end
411
-
412
- def destroy
413
- super do |user_id|
414
- user = find_user(user_id)
415
- user.delete
416
- end
417
- end
418
-
419
- protected
420
-
421
- # The class including Scimitar::Resources::Mixin which declares mappings
422
- # to the entity you return in #resource_type.
423
- #
424
- def storage_class
425
- User
426
- end
427
-
428
- # Find your user. The +id+ parameter is one of YOUR identifiers, which
429
- # are returned in "id" fields in JSON responses via SCIM schema. If the
430
- # remote caller (client) doesn't want to remember your IDs and hold a
431
- # mapping to their IDs, then they do an index with filter on their own
432
- # "externalId" value and retrieve your "id" from that response.
433
- #
434
- def find_user(id)
435
- # Find records by your ID here.
436
- end
437
-
438
- # Persist 'user' - for example, if we *were* using ActiveRecord...
439
- #
440
- def save!(user)
441
- user.save!
442
- rescue ActiveRecord::RecordInvalid => exception
443
- raise Scimitar::ResourceInvalidError.new(record.errors.full_messages.join('; '))
444
- end
445
-
446
- end
447
-
448
- ```
449
-
450
- Note that the [`Scimitar::ApplicationController` parent class](https://www.rubydoc.info/gems/scimitar/Scimitar/ApplicationController) of `Scimitar::ResourcesController` has a few methods to help with handling exceptions and rendering them as SCIM responses; for example, if a resource were not found by ID, you might wish to use [`Scimitar::ApplicationController#handle_resource_not_found`](https://github.com/RIPAGlobal/scimitar/blob/v1.0.3/app/controllers/scimitar/application_controller.rb#L22).
451
-
452
- ### Extension schema
453
-
454
- You can extend schema with custom data by defining an extension class and calling `::extend_schema` on the SCIM resource class to which the extension applies. These extension classes:
455
-
456
- * Must subclass `Scimitar::Schema::Base`
457
- * Must call `super` in `def initialize`, providing data as shown in the example below
458
- * Must define class methods for `::id` and `::scim_attributes`
459
-
460
- The `::id` class method defines a unique schema ID that is used to namespace payloads or paths in JSON responses describing extended resources, JSON payloads creating them or PATCH paths modifying them. The RFCs require this to be a URN ([see RFC 2141](https://tools.ietf.org/html/rfc2141)). Your extension's ID URN must be globally unique. Depending on your expected use case, you should review the [IANA registration considerations that RFC 7643 describes](https://tools.ietf.org//html/rfc7643#section-10) and definitely review the [syntactic structure declaration therein](https://tools.ietf.org/html/rfc7643#section-10.2.1) (`urn:ietf:params:scim:{type}:{name}{:other}`).
461
-
462
- For example, we might choose to use the [RFC-defined User extension schema](https://tools.ietf.org/html/rfc7643#section-4.3) to define a couple of extra fields our User model happens to support:
463
-
464
- ```ruby
465
- class UserEnterpriseExtension < Scimitar::Schema::Base
466
- def initialize(options = {})
467
- super(
468
- name: 'ExtendedUser',
469
- description: 'Enterprise extension for a User',
470
- id: self.class.id,
471
- scim_attributes: self.class.scim_attributes
472
- )
473
- end
474
-
475
- def self.id
476
- 'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'
477
- end
478
-
479
- def self.scim_attributes
480
- [
481
- Scimitar::Schema::Attribute.new(name: 'organization', type: 'string'),
482
- Scimitar::Schema::Attribute.new(name: 'department', type: 'string')
483
- ]
484
- end
485
- end
486
- ```
487
-
488
- ...with the `super` call providing your choice of `name` and `description`, but also always providing `id` and `scim_attributes` as shown above. The class name chosen here is just an example and the class can be put inside any level of wrapping namespaces you choose - it's *your* class that can be named however you like. The extension class is then applied to the SCIM User resource _globally in your application_ by calling:
489
-
490
- ```ruby
491
- Scimitar::Resources::User.extend_schema(UserEnterpriseExtension)
492
- ```
493
-
494
- This is often done in `config/initializers/scimitar.rb` to help make it very clear that extensions are globally available and remove the risk of SCIM resources somehow being referenced before schema extensions have been applied.
495
-
496
- In `def self.scim_attributes_map` in the underlying data model, add any new fields - `organization` and `department` in this example - to map them to whatever the equivalent data model attributes are, just as you would do with any other resource fields. These are declared without any special nesting - for example:
497
-
498
- ```ruby
499
- def self.scim_attributes_map
500
- return {
501
- id: :id,
502
- externalId: :scim_uid,
503
- userName: :username,
504
- # ...etc...
505
- organization: :company,
506
- department: :team
507
- }
508
- end
509
- ```
510
-
511
- Whatever you provide in the `::id` method in your extension class will be used as a namespace in JSON data. This means that, for example, a SCIM representation of the above resource would look something like this:
512
-
513
- ```json
514
- {
515
- "schemas": [
516
- "urn:ietf:params:scim:schemas:core:2.0:User",
517
- "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
518
- ],
519
- "id": "2819c223-7f76-453a-413861904646",
520
- "externalId": "701984",
521
- "userName": "bjensen@example.com",
522
- // ...
523
-
524
- "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User": {
525
- "organization": "Corporation Incorporated",
526
- "department": "Marketing",
527
- },
528
- // ...
529
- }
530
- ```
531
-
532
- ...and likewise, creation via `POST` would require the same nesting if a caller wanted to create a resource instance with those extended properties set (and RFC-compliant consumers of your SCIM API should already be doing this). For `PATCH` operations, [the `path` uses a _colon_ to separate the ID/URN part from the path](https://tools.ietf.org/html/rfc7644#section-3.10) rather than just using a dot as you might expect from the JSON nesting above:
533
-
534
- ```json
535
- {
536
- "schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
537
- "Operations": [
538
- {
539
- "op": "replace",
540
- "path": "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:organization",
541
- "value": "Sales"
542
- }
543
- ]
544
- }
545
- ```
546
-
547
- **IMPORTANT: Attribute names must be unique** across your entire combined schema, regardless of URNs used. This is because of a limitation in Scimitar's implementation. [This GitHub issue](https://github.com/RIPAGlobal/scimitar/issues/130) explains more. If this is a problem for you, please comment on the GitHub issue to help the maintainers understand the level of demand for remediation.
548
-
549
- Resource extensions can provide any fields you choose, under any ID/URN you choose, to either RFC-described resources or entirely custom SCIM resources. There are no hard-coded assumptions or other "magic" that might require you to only extend RFC-described resources with RFC-described extensions. Of course, if you use custom resources or custom extensions that are not described by the SCIM RFCs, then the SCIM API you provide may only work with custom-written API callers that are aware of your bespoke resources and/or extensions.
550
-
551
- Extensions can also contain complex attributes such as groups. For instance, if you want the ability to write to groups from the User resource perspective (since 'groups' collection in a SCIM User resource is read-only), you can add one attribute to your extension like this:
552
-
553
- ```ruby
554
- Scimitar::Schema::Attribute.new(name: "userGroups", multiValued: true, complexType: Scimitar::ComplexTypes::ReferenceGroup, mutability: "writeOnly"),
555
- ```
556
-
557
- Then map it in your `scim_attributes_map`:
558
-
559
- ```ruby
560
- userGroups: [
561
- {
562
- list: :groups,
563
- find_with: ->(value) { Group.find(value["value"]) },
564
- using: {
565
- value: :id,
566
- display: :name
567
- }
568
- }
569
- ]
570
- ```
571
-
572
- And write to it like this:
573
-
574
- ```json
575
- {
576
- "schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
577
- "Operations": [
578
- {
579
- "op": "replace",
580
- "path": "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:userGroups",
581
- "value": [{ "value": "1" }]
582
- }
583
- ]
584
- }
585
- ```
586
-
587
- ### Helping with auto-discovery
588
-
589
- If you have an API consumer entity querying your Scimitar-based SCIM API provider endpoint and want to enable a degree of auto-discovery for that entity, then depending on your implementation, there may be customisations you wish to make.
590
-
591
- #### Default resources
592
-
593
- By default, Scimitar advertises (via things like [the `/Schemas` endpoint](https://tools.ietf.org/html/rfc7644#section-4)) support for both a `User` and `Group` resource, but if you (say) only support a `User` concept, you override the default using code such as this in your `config/initializers/scimitar.rb` file:
594
-
595
- ```ruby
596
- Rails.application.config.to_prepare do
597
- Scimitar::Engine::set_default_resources([Scimitar::Resources::User])
598
- # ...other Scimitar configuration / initialisation code...
599
- end
600
- ```
601
-
602
-
603
-
604
- ## Security
605
-
606
- One vital feature of SCIM is its authorisation and security model. The best resource I've found to describe this in any detail is [section 2 of the protocol RFC, 7644](https://tools.ietf.org/html/rfc7644#section-2).
607
-
608
- Often, you'll find that bearer tokens are in use by SCIM API consumers, but the way in which this is used by that consumer in practice can vary a great deal. For example, suppose a corporation uses Microsoft Azure Active Directory to maintain a master database of employee details. Azure lets administrators [connect to SCIM endpoints](https://docs.microsoft.com/en-us/azure/active-directory/app-provisioning/how-provisioning-works) for services that this corporation might use. In all cases, bearer tokens are used.
609
-
610
- * When the third party integration builds an app that it gets hosted in the Azure Marketplace, the token is obtained via full OAuth flow of some kind - the enterprise corporation would sign into your app by some OAuth UI mechanism you provide, which leads to a Bearer token being issued. Thereafter, the Azure system would quote this back to you in API calls via the `Authorization` HTTP header.
611
-
612
- * If you are providing SCIM services as part of some wider service offering it might not make sense to go to the trouble of adding all the extra features and requirements for Marketplace inclusion. Fortunately, Microsoft support [addition of 'user-defined' enterprise "app" integrations](https://docs.microsoft.com/en-us/azure/active-directory/app-provisioning/use-scim-to-provision-users-and-groups#integrate-your-scim-endpoint-with-the-aad-scim-client) in Azure, so the administrator can set up and 'provision' your SCIM API endpoint. In _this_ case, the bearer token is just some string that you generate which they paste into the Azure AD UI. Clearly, then, this amounts to little more than a glorified password, but you can take steps to make sure that it's long, unguessable and potentially be some encrypted/encoded structure that allows you to make additional security checks on "your side" when you unpack the token as part of API request handling.
613
-
614
- * HTTPS is obviously a given here and localhost integration during development is difficult; perhaps search around for things like POSTman collections to assist with development testing. Scimitar has a reasonably comprehensive internal test suite but it's only as good as the accuracy and reliability of the subclass code you write to "bridge the gap" between SCIM schema and actions, and your User/Group equivalent records and the operations you perform upon them. Microsoft provide [additional information](https://techcommunity.microsoft.com/t5/identity-standards-blog/provisioning-with-scim-design-build-and-test-your-scim-endpoint/ba-p/1204883) to help guide service provider implementors with best practice.
615
-
616
-
617
-
618
- ## Limitations
619
-
620
- ### Specification versus implementation
621
-
622
- * Several complex types for User contain the same set of `value`, `display`, `type` and `primary` fields, all used in synonymous ways.
623
-
624
- - The `value` field - which is e.g. an e-mail address or phone number - is described as optional by [the RFC 7643 core schema](https://tools.ietf.org/html/rfc7643#section-8.7.1), also using "SHOULD" rather than "MUST" in field descriptions elsewhere. Scimitar marks this as required by default, since there's not much point being sent (say) an e-mail section which has entries that don't provide the e-mail address. Some services might send `null` values here regardless so, if you need to be able to accept such data, you can set [engine configuration option `optional_value_fields_required`](https://github.com/RIPAGlobal/scimitar/blob/main/config/initializers/scimitar.rb) to `false`.
625
-
626
- - The schema _descriptions_ for `display` declare that the field is something optionally sent by the service provider and state clearly that it is read-only - yet the formal schema declares it `readWrite`. Scimitar marks it as read-only.
627
-
628
- * The `displayName` of a Group is described in [RFC 7643 section 4.2](https://tools.ietf.org/html/rfc7643#section-4.2) and in the free-text schema `description` field as required, but the schema nonetheless states `"required" : false` in the formal definition. We consider this to be an error and mark the property as `"required" : true`.
629
-
630
- * In the `members` section of a [`Group` in the RFC 7643 core schema](https://tools.ietf.org/html/rfc7643#page-69), any member's `value` is noted as _not_ required but [the RFC also says](https://tools.ietf.org/html/rfc7643#section-4.2) "Service providers MAY require clients to provide a non-empty value by setting the "required" attribute characteristic of a sub-attribute of the "members" attribute in the "Group" resource schema". Scimitar does this. The `value` field would contain the `id` of a SCIM resource, which is the primary key on "our side" as a service provider. Just as we must store `externalId` values to maintain a mapping on "our side", we in turn _do_ require clients to provide our ID in group member lists via the `value` field.
631
-
632
- * While the gem attempts to support difficult/complex filter strings via incorporating code and ideas in [SCIM Query Filter Parser](https://github.com/ingydotnet/scim-query-filter-parser-rb), it is possible that ActiveRecord / Rails precedence on some query operations in complex cases might not exactly match the SCIM specification. Please do submit a bug report if you encounter this. You may also wish to view [`query_parser_spec.rb`](https://github.com/RIPAGlobal/scimitar/blob/main/spec/models/scimitar/lists/query_parser_spec.rb) to get an idea of the tested examples - more interesting test cases are in the "`context 'with complex cases' do`" section.
633
-
634
- * Group resource examples show the `members` array including field `display`, but this is not in the [formal schema](https://tools.ietf.org/html/rfc7643#page-69); Scimitar includes it in the Group definition.
635
-
636
- * `POST` actions with only a subset of attributes specified treat missing attributes "to be cleared" for anything that's mapped for the target model. If you have defaults established at instantiation rather than (say) before-validation, you'll need to override `Scimitar::ActiveRecordBackedResourcesController#create` (if using that controller as a base class) as normally the controller just instantiates a model, applies _all_ attributes (with any mapped attribute values without an inbound value set to `nil`), then saves the record. This might cause default values to be overwritten. For consistency, `PUT` operations apply the same behaviour. The decision on this optional specification aspect is in part constrained by the difficulties of implementing `PATCH`.
637
-
638
- * [RFC 7644 indicates](https://tools.ietf.org/html/rfc7644#page-35) that a resource might only return its core schema in the `schemas` attribute if it was created without any extension fields used. Only if e.g. a subsequent `PATCH` operation added data provided by extension schema, would that extension also appear in `schemas`. This behaviour is extremely difficult to implement and Scimitar does not try - it will always return a resource's core schema and any/all defined extension schemas in the `schemas` array at all times.
639
-
640
- * As noted earlier, extension schema attribute names must be unique across your entire combined schema, regardless of schema IDs (URNs) used.
641
-
642
- If you believe choices made in this section may be incorrect, please [create a GitHub issue](https://github.com/RIPAGlobal/scimitar/issues/new) describing the problem.
643
-
644
- ### Omissions
645
-
646
- * Bulk operations are not supported.
647
-
648
- * List ("index") endpoint [filters in SCIM](https://tools.ietf.org/html/rfc7644#section-3.4.2.2) are _extremely_ complicated. There is a syntax for specifying equals, not-equals, precedence through parentheses and things like "and"/"or"/"not" along the lines of "attribute operator value", which Scimitar supports to a reasonably comprehensive degree but with some limitations discussed shortly. That aside, it isn't at all clear what some of the [examples in the RFC](https://tools.ietf.org/html/rfc7644#page-23) are even meant to mean. Consider:
649
-
650
- - `filter=userType eq "Employee" and (emails co "example.com" or emails.value co "example.org")`
651
-
652
- It's very strange just specifying `emails co...`, since this is an Array which contains complex types. Is the filter there meant to try and match every attribute of the nested types in all array entries? I.e. if `type` happened to contain `example.com`, is that meant to match? It's strongly implied, because the next part of the filter specifically says `emails.value`. Again, we have to reach a little and assume that `emails.value` means "in _any_ of the objects in the `emails` Array, match all things where `value` contains `example.org`. It seems likely that this is a specification error and both of the specifiers should be `emails.value`.
653
-
654
- * Currently filtering for lists is always matched case-insensitive regardless of schema declarations that might indicate otherwise, for `eq`, `ne`, `co`, `sw` and `ew` operators; for greater/less-thank style filters, case is maintained with simple `>`, `<` etc. database operations in use. The standard Group and User schema have `caseExact` set to `false` for just about anything readily queryable, so this hopefully would only ever potentially be an issue for custom schema.
655
-
656
- * As an exception to the above, attributes `id`, `externalId` and `meta.*` are matched case-sensitive. Filters that use `eq` on such attributes will end up a comparison using `=` rather than e.g. `ILIKE` (arising from https://github.com/RIPAGlobal/scimitar/issues/36).
657
-
658
- * The `PATCH` mechanism is supported, but where filters are included, only a single "attribute eq value" is permitted - no other operators or combinations. For example, a work e-mail address's value could be replaced by a PATCH patch of `emails[type eq "work"].value`. For in-path filters such as this, other operators such as `ne` are not supported; combinations with "and"/"or" are not supported; negation with "not" is not supported.
659
-
660
- If you would like to see something listed in the session implemented, please [create a GitHub issue](https://github.com/RIPAGlobal/scimitar/issues/new) asking for it to be implemented, or if possible, implement the feature and send a Pull Request.
661
-
662
-
663
-
664
- ## Development
665
-
666
- Install Ruby dependencies first:
667
-
668
- ```
669
- bundle install
670
- ```
671
-
672
- ### Tests
673
-
674
- For testing, two main options are available:
675
-
676
- * The first option is running the project locally. This is also the recommended way, as running the tests on a variety of setups and platforms increases he chance of finding platform-specific or setup-specific bugs.
677
- * The second option is utilising the existing Docker Compose setup provided in the project. You can use this if getting the project to work locally is hard or not feasible.
678
-
679
- #### Testing on your machine
680
-
681
- You will need to have PostgreSQL running. This database is chosen for tests to prove case-insensitive behaviour via detection of ILIKE in generated queries. Using SQLite would have resulted in a more conceptually self-contained test suite, but SQLite is case-insensitive by default and uses "LIKE" either way, making it hard to "see" if the query system is doing the right thing.
682
-
683
- After `bundle install` and with PostgreSQL up, set up the test database with:
684
-
685
- ```shell
686
- pushd spec/apps/dummy
687
- RAILS_ENV=test bundle exec bin/rails db:drop db:create db:migrate
688
- popd
689
- ```
690
-
691
- ...and thereafter, run tests with:
692
-
693
- ```
694
- bundle exec rspec
695
- ```
696
-
697
- You can get an idea of arising test coverage by opening `coverage/index.html` in your preferred web browser.
698
-
699
- #### Testing with Docker (Compose)
700
-
701
- In order to be able to utilise the Docker Compose setup, you will need to have Docker installed with the Compose plugin. For an easy installation of Docker (with a GUI and the Compose plugin preinstalled) please see [Docker Desktop](https://www.docker.com/products/docker-desktop/).
702
-
703
- In order to configure the Docker image, run `docker compose build` in a terminal of your choice, in the root of this project. This will download the required image and install the required libraries. After this is complete, running the tests is as easy as running the command `docker compose up test`.
704
-
705
- As mentioned in the previous section, test coverage may be analysed using `coverage/index.html` after running the project.
706
-
707
- You can also open a raw terminal in this test container by running `docker run --rm test sh`. For more Compose commands, please refer to [the Docker Compose reference manual](https://docs.docker.com/compose/reference/).
708
-
709
- ### Internal documentation
710
-
711
- Locally generated RDoc HTML seems to contain a more comprehensive and inter-linked set of pages than those available from `rubydoc.info`. You can (re)generate the internal [`rdoc` documentation](https://ruby-doc.org/stdlib-2.4.1/libdoc/rdoc/rdoc/RDoc/Markup.html#label-Supported+Formats) with:
712
-
713
- ```shell
714
- bundle exec rake rerdoc
715
- ```
716
-
717
- ...yes, that's `rerdoc` - Re-R-Doc - then open `docs/rdoc/index.html`.