Class: CF::UAA::Scim
Overview
This class is for apps that need to manage User Accounts, Groups, or OAuth Client Registrations. It provides access to the SCIM endpoints on the UAA. For more information about SCIM – the IETF’s System for Cross-domain Identity Management (formerly known as Simple Cloud Identity Management) – see http://www.simplecloud.info.
The types of objects and links to their schema are as follows:
-
:user– http://www.simplecloud.info/specs/draft-scim-core-schema-01.html#user-resource or http://www.simplecloud.info/specs/draft-scim-core-schema-01.html#anchor8 -
:group– http://www.simplecloud.info/specs/draft-scim-core-schema-01.html#group-resource or http://www.simplecloud.info/specs/draft-scim-core-schema-01.html#anchor10 -
:client -
:user_id– https://github.com/cloudfoundry/uaa/blob/master/docs/UAA-APIs.rst#converting-userids-to-names
Naming attributes by type of object:
-
:useris “username” -
:groupis “displayname” -
:clientis “client_id”
Constant Summary
Constants included from Http
Http::FORM_UTF8, Http::JSON_UTF8
Instance Method Summary collapse
-
#add(type, info) ⇒ Hash
Creates a SCIM resource.
-
#all_pages(type, query = {}) ⇒ Array
Collects all pages of entries from a query.
-
#change_password(user_id, new_password, old_password = nil) ⇒ Hash
Change password.
-
#change_secret(client_id, new_secret, old_secret = nil) ⇒ Hash
Change client secret.
-
#delete(type, id) ⇒ nil
Deletes a SCIM resource.
-
#get(type, id) ⇒ Hash
Get information about a specific object.
-
#id(type, name) ⇒ String
Convenience method to query for single object by name.
-
#ids(type, *names) ⇒ Array
Gets id/name pairs for given names.
-
#initialize(target, auth_header, options = {}) ⇒ Scim
constructor
A new instance of Scim.
- #list_group_mappings(start = nil, count = nil) ⇒ Object
- #map_group(group, is_id, external_group) ⇒ Object
-
#name_attr(type) ⇒ String
Convenience method to get the naming attribute, e.g.
-
#patch(type, info) ⇒ Hash
Modifies the contents of a SCIM object.
-
#put(type, info) ⇒ Hash
Replaces the contents of a SCIM object.
-
#query(type, query = {}) ⇒ Hash
Gets a set of attributes for each object that matches a given filter.
- #unmap_group(group_id, external_group) ⇒ Object
Methods included from Http
basic_auth, #logger, #logger=, #set_request_handler, #trace?
Methods included from ProxyOptions
Constructor Details
#initialize(target, auth_header, options = {}) ⇒ Scim
Returns a new instance of Scim.
99 100 101 102 103 104 105 |
# File 'lib/uaa/scim.rb', line 99 def initialize(target, auth_header, = {}) @target, @auth_header = target, auth_header @key_style = [:symbolize_keys] ? :downsym : :down self.skip_ssl_validation = [:skip_ssl_validation] self.http_proxy = [:http_proxy] self.https_proxy = [:https_proxy] end |
Instance Method Details
#add(type, info) ⇒ Hash
Creates a SCIM resource.
118 119 120 121 122 123 124 |
# File 'lib/uaa/scim.rb', line 118 def add(type, info) path, info = type_info(type, :path), force_case(info) reply = json_parse_reply(@key_style, *json_post(@target, path, info, "authorization" => @auth_header)) fake_client_id(reply) if type == :client # hide client reply, not quite scim reply end |
#all_pages(type, query = {}) ⇒ Array
Collects all pages of entries from a query
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/uaa/scim.rb', line 228 def all_pages(type, query = {}) query = force_case(query).reject {|k, v| v.nil? } query["startindex"], info, rk = 1, [], jkey(:resources) while true qinfo = query(type, query) raise BadResponse unless qinfo[rk] return info if qinfo[rk].empty? info.concat(qinfo[rk]) total = qinfo[jkey :totalresults] return info unless total && total > info.length unless qinfo[jkey :startindex] && qinfo[jkey :itemsperpage] raise BadResponse, "incomplete #{type} pagination data from #{@target}" end query["startindex"] = info.length + 1 end end |
#change_password(user_id, new_password, old_password = nil) ⇒ Hash
Change password.
-
For a user to change their own password, the token in @auth_header must contain “password.write” scope and the correct
old_passwordmust be given. -
For an admin to set a user’s password, the token in @auth_header must contain “uaa.admin” scope.
285 286 287 288 289 290 291 |
# File 'lib/uaa/scim.rb', line 285 def change_password(user_id, new_password, old_password = nil) req = {"password" => new_password} req["oldPassword"] = old_password if old_password json_parse_reply(@key_style, *json_put(@target, "#{type_info(:user, :path)}/#{URI.encode(user_id)}/password", req, 'authorization' => @auth_header)) end |
#change_secret(client_id, new_secret, old_secret = nil) ⇒ Hash
Change client secret.
-
For a client to change its own secret, the token in @auth_header must contain “client.secret” scope and the correct
old_secretmust be given. -
For an admin to set a client secret, the token in @auth_header must contain “uaa.admin” scope.
302 303 304 305 306 307 308 |
# File 'lib/uaa/scim.rb', line 302 def change_secret(client_id, new_secret, old_secret = nil) req = {"secret" => new_secret } req["oldSecret"] = old_secret if old_secret json_parse_reply(@key_style, *json_put(@target, "#{type_info(:client, :path)}/#{URI.encode(client_id)}/secret", req, 'authorization' => @auth_header)) end |
#delete(type, id) ⇒ nil
Deletes a SCIM resource
130 131 132 |
# File 'lib/uaa/scim.rb', line 130 def delete(type, id) http_delete @target, "#{type_info(type, :path)}/#{URI.encode(id)}", @auth_header end |
#get(type, id) ⇒ Hash
Get information about a specific object.
211 212 213 214 215 216 217 |
# File 'lib/uaa/scim.rb', line 211 def get(type, id) info = json_get(@target, "#{type_info(type, :path)}/#{URI.encode(id)}", @key_style, 'authorization' => @auth_header) fake_client_id(info) if type == :client # hide client reply, not quite scim info end |
#id(type, name) ⇒ String
Convenience method to query for single object by name.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/uaa/scim.rb', line 259 def id(type, name) res = ids(type, name) # hide client endpoints that are not scim compatible ik, ck = jkey(:id), jkey(:client_id) if type == :client && res && res.length > 0 && (res.length > 1 || res[0][ik].nil?) cr = res.find { |o| o[ck] && name.casecmp(o[ck]) == 0 } return cr[ik] || cr[ck] if cr end unless res && res.is_a?(Array) && res.length == 1 && res[0].is_a?(Hash) && (id = res[0][jkey :id]) raise NotFound, "#{name} not found in #{@target}#{type_info(type, :path)}" end id end |
#ids(type, *names) ⇒ Array
Gets id/name pairs for given names. For naming attribute of each object type see CF::UAA::Scim
248 249 250 251 252 |
# File 'lib/uaa/scim.rb', line 248 def ids(type, *names) na = type_info(type, :name_attr) filter = names.map { |n| "#{na} eq \"#{n}\""} all_pages(type, :attributes => "id,#{na}", :filter => filter.join(" or ")) end |
#list_group_mappings(start = nil, count = nil) ⇒ Object
324 325 326 |
# File 'lib/uaa/scim.rb', line 324 def list_group_mappings(start = nil, count = nil) json_get(@target, "#{type_info(:group_mapping, :path)}/list?startIndex=#{start}&count=#{count}", @key_style, 'authorization' => @auth_header) end |
#map_group(group, is_id, external_group) ⇒ Object
310 311 312 313 314 315 316 317 |
# File 'lib/uaa/scim.rb', line 310 def map_group(group, is_id, external_group) key_name = is_id ? :groupId : :displayName request = {key_name => group, :externalGroup => external_group, :schemas => ["urn:scim:schemas:core:1.0"] } result = json_parse_reply(@key_style, *json_post(@target, "#{type_info(:group_mapping, :path)}", request, 'authorization' => @auth_header)) result end |
#name_attr(type) ⇒ String
Convenience method to get the naming attribute, e.g. userName for user, displayName for group, client_id for client.
111 |
# File 'lib/uaa/scim.rb', line 111 def name_attr(type) type_info(type, :name_attr) end |
#patch(type, info) ⇒ Hash
Modifies the contents of a SCIM object.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/uaa/scim.rb', line 155 def patch(type, info) path, info = type_info(type, :path), force_case(info) ida = type == :client ? 'client_id' : 'id' raise ArgumentError, "info must include #{ida}" unless id = info[ida] hdrs = {'authorization' => @auth_header} if info && info['meta'] && (etag = info['meta']['version']) hdrs.merge!('if-match' => etag) end reply = json_parse_reply(@key_style, *json_patch(@target, "#{path}/#{URI.encode(id)}", info, hdrs)) # hide client endpoints that are not quite scim compatible type == :client && !reply ? get(type, info['client_id']): reply end |
#put(type, info) ⇒ Hash
Replaces the contents of a SCIM object.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/uaa/scim.rb', line 137 def put(type, info) path, info = type_info(type, :path), force_case(info) ida = type == :client ? 'client_id' : 'id' raise ArgumentError, "info must include #{ida}" unless id = info[ida] hdrs = {'authorization' => @auth_header} if info && info['meta'] && (etag = info['meta']['version']) hdrs.merge!('if-match' => etag) end reply = json_parse_reply(@key_style, *json_put(@target, "#{path}/#{URI.encode(id)}", info, hdrs)) # hide client endpoints that are not quite scim compatible type == :client && !reply ? get(type, info['client_id']): reply end |
#query(type, query = {}) ⇒ Hash
Gets a set of attributes for each object that matches a given filter.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/uaa/scim.rb', line 182 def query(type, query = {}) query = force_case(query).reject {|k, v| v.nil? } if attrs = query['attributes'] attrs = Util.arglist(attrs).map {|a| force_attr(a)} query['attributes'] = Util.strlist(attrs, ",") end qstr = query.empty?? '': "?#{Util.encode_form(query)}" info = json_get(@target, "#{type_info(type, :path)}#{qstr}", @key_style, 'authorization' => @auth_header) unless info.is_a?(Hash) && info[rk = jkey(:resources)].is_a?(Array) # hide client endpoints that are not yet scim compatible if type == :client && info.is_a?(Hash) info = info.each{ |k, v| fake_client_id(v) }.values if m = /^client_id\s+eq\s+"([^"]+)"$/i.match(query['filter']) idk = jkey(:client_id) info = info.select { |c| c[idk].casecmp(m[1]) == 0 } end return {rk => info} end raise BadResponse, "invalid reply to #{type} query of #{@target}" end info end |
#unmap_group(group_id, external_group) ⇒ Object
319 320 321 322 |
# File 'lib/uaa/scim.rb', line 319 def unmap_group(group_id, external_group) http_delete(@target, "#{type_info(:group_mapping, :path)}/id/#{group_id}/#{external_group}", @auth_header) end |