scimitar 1.7.0 → 1.8.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +21 -0
- data/README.md +710 -0
- data/app/controllers/scimitar/active_record_backed_resources_controller.rb +51 -16
- data/app/controllers/scimitar/application_controller.rb +13 -4
- data/app/models/scimitar/complex_types/address.rb +0 -6
- data/app/models/scimitar/resource_invalid_error.rb +1 -1
- data/app/models/scimitar/resources/mixin.rb +4 -1
- data/lib/scimitar/support/utilities.rb +51 -0
- data/lib/scimitar/version.rb +2 -2
- data/lib/scimitar.rb +1 -0
- data/spec/apps/dummy/app/controllers/custom_create_mock_users_controller.rb +25 -0
- data/spec/apps/dummy/app/controllers/custom_replace_mock_users_controller.rb +25 -0
- data/spec/apps/dummy/app/controllers/custom_update_mock_users_controller.rb +25 -0
- data/spec/apps/dummy/config/routes.rb +15 -3
- data/spec/apps/dummy/db/schema.rb +2 -2
- data/spec/controllers/scimitar/application_controller_spec.rb +56 -2
- data/spec/controllers/scimitar/schemas_controller_spec.rb +1 -1
- data/spec/models/scimitar/complex_types/address_spec.rb +3 -4
- data/spec/models/scimitar/resources/mixin_spec.rb +22 -0
- data/spec/requests/active_record_backed_resources_controller_spec.rb +410 -50
- data/spec/spec_helper.rb +8 -0
- metadata +34 -11
data/README.md
ADDED
@@ -0,0 +1,710 @@
|
|
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
|
+
**Important:** Under some more recent versions of Rails 6, you may need to wrap any Scimitar configuration with `Rails.application.config.to_prepare do...` to avoid `NameError: uninitialized constant...` exceptions arising due to autoloader problems:
|
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
|
+
### Routes
|
93
|
+
|
94
|
+
For each resource you support, add these lines to your `routes.rb`:
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
namespace :scim_v2 do
|
98
|
+
mount Scimitar::Engine, at: '/'
|
99
|
+
|
100
|
+
get 'Users', to: 'users#index'
|
101
|
+
get 'Users/:id', to: 'users#show'
|
102
|
+
post 'Users', to: 'users#create'
|
103
|
+
put 'Users/:id', to: 'users#replace'
|
104
|
+
patch 'Users/:id', to: 'users#update'
|
105
|
+
delete 'Users/:id', to: 'users#destroy'
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
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.
|
110
|
+
|
111
|
+
#### URL helpers
|
112
|
+
|
113
|
+
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).
|
114
|
+
|
115
|
+
### Data models
|
116
|
+
|
117
|
+
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".
|
118
|
+
|
119
|
+
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).
|
120
|
+
|
121
|
+
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:
|
122
|
+
|
123
|
+
```ruby
|
124
|
+
class User < ActiveRecord::Base
|
125
|
+
|
126
|
+
# The attributes in the SCIM section below include a reference to this
|
127
|
+
# hypothesised 'groups' HABTM relationship. All of the other "leaf node"
|
128
|
+
# Symbols - e.g. ":first_name", ":last_name" - are expected to be defined as
|
129
|
+
# accessors e.g. via ActiveRecord and your related database table columns,
|
130
|
+
# "attr_accessor" declarations, or bespoke "def foo"/"def foo=(value)". If a
|
131
|
+
# write accessor is not present, the attribute will not be writable via SCIM.
|
132
|
+
#
|
133
|
+
has_and_belongs_to_many :groups
|
134
|
+
|
135
|
+
# ===========================================================================
|
136
|
+
# SCIM MIXIN AND REQUIRED METHODS
|
137
|
+
# ===========================================================================
|
138
|
+
#
|
139
|
+
# All class methods shown below are mandatory unless otherwise commented.
|
140
|
+
|
141
|
+
def self.scim_resource_type
|
142
|
+
return Scimitar::Resources::User
|
143
|
+
end
|
144
|
+
|
145
|
+
def self.scim_attributes_map
|
146
|
+
return {
|
147
|
+
id: :id,
|
148
|
+
externalId: :scim_uid,
|
149
|
+
userName: :username,
|
150
|
+
name: {
|
151
|
+
givenName: :first_name,
|
152
|
+
familyName: :last_name
|
153
|
+
},
|
154
|
+
emails: [
|
155
|
+
{
|
156
|
+
match: 'type',
|
157
|
+
with: 'work',
|
158
|
+
using: {
|
159
|
+
value: :work_email_address,
|
160
|
+
primary: true
|
161
|
+
}
|
162
|
+
},
|
163
|
+
{
|
164
|
+
match: 'type',
|
165
|
+
with: 'home',
|
166
|
+
using: {
|
167
|
+
value: :home_email_address,
|
168
|
+
primary: false
|
169
|
+
}
|
170
|
+
},
|
171
|
+
],
|
172
|
+
phoneNumbers: [
|
173
|
+
{
|
174
|
+
match: 'type',
|
175
|
+
with: 'work',
|
176
|
+
using: {
|
177
|
+
value: :work_phone_number,
|
178
|
+
primary: false
|
179
|
+
}
|
180
|
+
},
|
181
|
+
],
|
182
|
+
|
183
|
+
# NB The 'groups' collection in a SCIM User resource is read-only, so
|
184
|
+
# we provide no ":find_with" key for looking up records for writing
|
185
|
+
# updates to the associated collection.
|
186
|
+
#
|
187
|
+
groups: [
|
188
|
+
{
|
189
|
+
list: :groups,
|
190
|
+
using: {
|
191
|
+
value: :id,
|
192
|
+
display: :display_name
|
193
|
+
}
|
194
|
+
}
|
195
|
+
],
|
196
|
+
active: :is_active
|
197
|
+
}
|
198
|
+
end
|
199
|
+
|
200
|
+
def self.scim_mutable_attributes
|
201
|
+
return nil
|
202
|
+
end
|
203
|
+
|
204
|
+
# The attributes in this example include a reference to the same hypothesised
|
205
|
+
# 'Group' model as in the HABTM relationship above. In this case, in order to
|
206
|
+
# filter by "groups" or "groups.value", the 'column' entry must reference the
|
207
|
+
# Group model's ID column as an AREL attribute as shown below, and the SCIM
|
208
|
+
# controller's #storage_scope implementation must also introduce a #join with
|
209
|
+
# ':groups' - see the "Queries & Optimisations" section below.
|
210
|
+
#
|
211
|
+
def self.scim_queryable_attributes
|
212
|
+
return {
|
213
|
+
givenName: { column: :first_name },
|
214
|
+
familyName: { column: :last_name },
|
215
|
+
emails: { column: :work_email_address },
|
216
|
+
groups: { column: Group.arel_table[:id] },
|
217
|
+
"groups.value" => { column: Group.arel_table[:id] },
|
218
|
+
}
|
219
|
+
end
|
220
|
+
|
221
|
+
# Optional but recommended.
|
222
|
+
#
|
223
|
+
def self.scim_timestamps_map
|
224
|
+
{
|
225
|
+
created: :created_at,
|
226
|
+
lastModified: :updated_at
|
227
|
+
}
|
228
|
+
end
|
229
|
+
|
230
|
+
# If you omit any mandatory declarations, you'll get an exception raised by
|
231
|
+
# this inclusion which tells you which method(s) need(s) to be added.
|
232
|
+
#
|
233
|
+
include Scimitar::Resources::Mixin
|
234
|
+
end
|
235
|
+
```
|
236
|
+
|
237
|
+
### Controllers
|
238
|
+
|
239
|
+
#### ActiveRecord
|
240
|
+
|
241
|
+
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:
|
242
|
+
|
243
|
+
```ruby
|
244
|
+
module Scim
|
245
|
+
class UsersController < Scimitar::ActiveRecordBackedResourcesController
|
246
|
+
|
247
|
+
skip_before_action :verify_authenticity_token
|
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 SCIM RFCs would refer to this as the URN. 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:
|
461
|
+
|
462
|
+
```ruby
|
463
|
+
class UserEnterpriseExtension < Scimitar::Schema::Base
|
464
|
+
def initialize(options = {})
|
465
|
+
super(
|
466
|
+
name: 'ExtendedUser',
|
467
|
+
description: 'Enterprise extension for a User',
|
468
|
+
id: self.class.id,
|
469
|
+
scim_attributes: self.class.scim_attributes
|
470
|
+
)
|
471
|
+
end
|
472
|
+
|
473
|
+
def self.id
|
474
|
+
'urn:ietf:params:scim:schemas:extension:enterprise:2.0:User'
|
475
|
+
end
|
476
|
+
|
477
|
+
def self.scim_attributes
|
478
|
+
[
|
479
|
+
Scimitar::Schema::Attribute.new(name: 'organization', type: 'string'),
|
480
|
+
Scimitar::Schema::Attribute.new(name: 'department', type: 'string')
|
481
|
+
]
|
482
|
+
end
|
483
|
+
end
|
484
|
+
```
|
485
|
+
|
486
|
+
...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:
|
487
|
+
|
488
|
+
```ruby
|
489
|
+
Scimitar::Resources::User.extend_schema(UserEnterpriseExtension)
|
490
|
+
```
|
491
|
+
|
492
|
+
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.
|
493
|
+
|
494
|
+
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:
|
495
|
+
|
496
|
+
```ruby
|
497
|
+
def self.scim_attributes_map
|
498
|
+
return {
|
499
|
+
id: :id,
|
500
|
+
externalId: :scim_uid,
|
501
|
+
userName: :username,
|
502
|
+
# ...etc...
|
503
|
+
organization: :company,
|
504
|
+
department: :team
|
505
|
+
}
|
506
|
+
end
|
507
|
+
```
|
508
|
+
|
509
|
+
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:
|
510
|
+
|
511
|
+
```json
|
512
|
+
{
|
513
|
+
"schemas": [
|
514
|
+
"urn:ietf:params:scim:schemas:core:2.0:User",
|
515
|
+
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User"
|
516
|
+
],
|
517
|
+
"id": "2819c223-7f76-453a-413861904646",
|
518
|
+
"externalId": "701984",
|
519
|
+
"userName": "bjensen@example.com",
|
520
|
+
// ...
|
521
|
+
|
522
|
+
"urn:ietf:params:scim:schemas:extension:enterprise:2.0:User": {
|
523
|
+
"organization": "Corporation Incorporated",
|
524
|
+
"department": "Marketing",
|
525
|
+
},
|
526
|
+
// ...
|
527
|
+
}
|
528
|
+
```
|
529
|
+
|
530
|
+
...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:
|
531
|
+
|
532
|
+
```json
|
533
|
+
{
|
534
|
+
"schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
|
535
|
+
"Operations": [
|
536
|
+
{
|
537
|
+
"op": "replace",
|
538
|
+
"path": "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:organization",
|
539
|
+
"value": "Sales"
|
540
|
+
}
|
541
|
+
]
|
542
|
+
}
|
543
|
+
```
|
544
|
+
|
545
|
+
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.
|
546
|
+
|
547
|
+
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:
|
548
|
+
|
549
|
+
```ruby
|
550
|
+
Scimitar::Schema::Attribute.new(name: "userGroups", multiValued: true, complexType: Scimitar::ComplexTypes::ReferenceGroup, mutability: "writeOnly"),
|
551
|
+
```
|
552
|
+
|
553
|
+
Then map it in your `scim_attributes_map`:
|
554
|
+
|
555
|
+
```ruby
|
556
|
+
userGroups: [
|
557
|
+
{
|
558
|
+
list: :groups,
|
559
|
+
find_with: ->(value) { Group.find(value["value"]) },
|
560
|
+
using: {
|
561
|
+
value: :id,
|
562
|
+
display: :name
|
563
|
+
}
|
564
|
+
}
|
565
|
+
]
|
566
|
+
```
|
567
|
+
|
568
|
+
And write to it like this:
|
569
|
+
|
570
|
+
```json
|
571
|
+
{
|
572
|
+
"schemas": ["urn:ietf:params:scim:api:messages:2.0:PatchOp"],
|
573
|
+
"Operations": [
|
574
|
+
{
|
575
|
+
"op": "replace",
|
576
|
+
"path": "urn:ietf:params:scim:schemas:extension:enterprise:2.0:User:userGroups",
|
577
|
+
"value": [{ "value": "1" }]
|
578
|
+
}
|
579
|
+
]
|
580
|
+
}
|
581
|
+
```
|
582
|
+
|
583
|
+
## Security
|
584
|
+
|
585
|
+
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).
|
586
|
+
|
587
|
+
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.
|
588
|
+
|
589
|
+
* 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.
|
590
|
+
|
591
|
+
* 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.
|
592
|
+
|
593
|
+
* 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.
|
594
|
+
|
595
|
+
|
596
|
+
|
597
|
+
## Limitations
|
598
|
+
|
599
|
+
### Specification versus implementation
|
600
|
+
|
601
|
+
* The `name` complex type of a User has `givenName` and `familyName` fields which [the RFC 7643 core schema](https://tools.ietf.org/html/rfc7643#section-8.7.1) describes as optional. Scimitar marks these as required, in the belief that most user synchronisation scenarios between clients and a Scimitar-based provider would require at least those names for basic user management on the provider side, in conjunction with the in-spec-required `userName` field. That's only if the whole `name` type is given at all - at the top level, this itself remains optional per spec, but if you're going to bother specifying names at all, Scimitar wants at least those two pieces of data.
|
602
|
+
|
603
|
+
* Several complex types for User contain the same set of `value`, `display`, `type` and `primary` fields, all used in synonymous ways.
|
604
|
+
|
605
|
+
- 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`.
|
606
|
+
|
607
|
+
- 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.
|
608
|
+
|
609
|
+
* 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`.
|
610
|
+
|
611
|
+
* 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.
|
612
|
+
|
613
|
+
* 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.
|
614
|
+
|
615
|
+
* 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.
|
616
|
+
|
617
|
+
* `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`.
|
618
|
+
|
619
|
+
* [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.
|
620
|
+
|
621
|
+
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.
|
622
|
+
|
623
|
+
### Omissions
|
624
|
+
|
625
|
+
* Bulk operations are not supported.
|
626
|
+
|
627
|
+
* 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:
|
628
|
+
|
629
|
+
- `filter=userType eq "Employee" and (emails co "example.com" or emails.value co "example.org")`
|
630
|
+
|
631
|
+
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`.
|
632
|
+
|
633
|
+
Adding even more complexity - the specification shows filters _which include filters within them_. In the same way that PATCH operations use paths to identify attributes not just by name, but by filter matches within collections - e.g. `emails[type eq "work"]`, for all e-mail objects inside the `emails` array with a `type` attribute that has a value of `work`) - so also can a filter _contain a filter_, which isn't supported. So, this [example from the RFC](https://tools.ietf.org/html/rfc7644#page-23) is not supported by Scimitar:
|
634
|
+
|
635
|
+
- `filter=userType eq "Employee" and emails[type eq "work" and value co "@example.com"]`
|
636
|
+
|
637
|
+
Another filter shows a potential workaround:
|
638
|
+
|
639
|
+
- `filter=userType eq "Employee" and (emails.type eq "work")`
|
640
|
+
|
641
|
+
...which is just a match on `emails.type`, so if you have a queryable attribute mapping defined for `emails.type`, that would become queryable. Likewise, you could rewrite the more complex prior example thus:
|
642
|
+
|
643
|
+
- `filter=userType eq "Employee" and emails.type eq "work" and emails.value co "@example.com"`
|
644
|
+
|
645
|
+
...so adding a mapping for `emails.value` would then allow a database query to be constructed.
|
646
|
+
|
647
|
+
* 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.
|
648
|
+
|
649
|
+
* 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).
|
650
|
+
|
651
|
+
* 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.
|
652
|
+
|
653
|
+
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.
|
654
|
+
|
655
|
+
|
656
|
+
|
657
|
+
## Development
|
658
|
+
|
659
|
+
Install Ruby dependencies first:
|
660
|
+
|
661
|
+
```
|
662
|
+
bundle install
|
663
|
+
```
|
664
|
+
|
665
|
+
### Tests
|
666
|
+
|
667
|
+
For testing, two main options are available:
|
668
|
+
|
669
|
+
* 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.
|
670
|
+
* 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.
|
671
|
+
|
672
|
+
#### Testing on your machine
|
673
|
+
|
674
|
+
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.
|
675
|
+
|
676
|
+
After `bundle install` and with PostgreSQL up, set up the test database with:
|
677
|
+
|
678
|
+
```shell
|
679
|
+
pushd spec/apps/dummy
|
680
|
+
RAILS_ENV=test bundle exec bin/rails db:drop db:create db:migrate
|
681
|
+
popd
|
682
|
+
```
|
683
|
+
|
684
|
+
...and thereafter, run tests with:
|
685
|
+
|
686
|
+
```
|
687
|
+
bundle exec rspec
|
688
|
+
```
|
689
|
+
|
690
|
+
You can get an idea of arising test coverage by opening `coverage/index.html` in your preferred web browser.
|
691
|
+
|
692
|
+
#### Testing with Docker (Compose)
|
693
|
+
|
694
|
+
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/).
|
695
|
+
|
696
|
+
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`.
|
697
|
+
|
698
|
+
As mentioned in the previous section, test coverage may be analysed using `coverage/index.html` after running the project.
|
699
|
+
|
700
|
+
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/).
|
701
|
+
|
702
|
+
### Internal documentation
|
703
|
+
|
704
|
+
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:
|
705
|
+
|
706
|
+
```shell
|
707
|
+
bundle exec rake rerdoc
|
708
|
+
```
|
709
|
+
|
710
|
+
...yes, that's `rerdoc` - Re-R-Doc - then open `docs/rdoc/index.html`.
|