def on_delete(self, req: Request, resp: Response, scim_id: str): self.context.logger.info(f'Deleting invite {scim_id}') db_invite = ctx_invitedb(req).get_invite_by_scim_id(scim_id=scim_id) self.context.logger.debug(f'Found invite: {db_invite}') if not db_invite: raise NotFound(detail="Invite not found") # Check version if not self._check_version(req, db_invite): raise BadRequest(detail="Version mismatch") # Remove signup invite ref = self._create_signup_ref(req, db_invite) signup_invite = self.context.signup_invitedb.get_invite_by_reference( ref) self.context.signup_invitedb.remove_document(signup_invite.invite_id) # Remove scim invite res = ctx_invitedb(req).remove(db_invite) add_api_event( context=self.context, data_owner=req.context['data_owner'], db_obj=db_invite, resource_type=SCIMResourceType.INVITE, level=EventLevel.INFO, status=EventStatus.DELETED, message='Group was deleted', ) self.context.logger.debug(f'Remove invite result: {res}') resp.status = HTTP_204
def on_delete(self, req: Request, resp: Response, scim_id: str): self.context.logger.info(f'Deleting group {scim_id}') db_group = ctx_groupdb(req).get_group_by_scim_id(scim_id=scim_id) self.context.logger.debug(f'Found group: {db_group}') if not db_group: raise NotFound(detail="Group not found") # Check version if not self._check_version(req, db_group): raise BadRequest(detail="Version mismatch") res = ctx_groupdb(req).remove_group(db_group) add_api_event( context=self.context, data_owner=req.context['data_owner'], db_obj=db_group, resource_type=SCIMResourceType.GROUP, level=EventLevel.INFO, status=EventStatus.DELETED, message='Group was deleted', ) self.context.logger.debug(f'Remove group result: {res}') resp.status = HTTP_204
def on_get(self, req: Request, resp: Response, scim_id: Optional[str] = None): """ GET /Groups/c3819cbe-c893-4070-824c-fe3d0db8f955 HTTP/1.1 Host: example.com Accept: application/scim+json Content-Type: application/scim+json Authorization: Bearer h480djs93hd8 Content-Length: ... HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 etag: W/"5e79df24f77769b475177bc7" location: http://scimapi.eduid.docker/scim/test/Groups/c3819cbe-c893-4070-824c-fe3d0db8f955 { "displayName": "test group", "id": "c3819cbe-c893-4070-824c-fe3d0db8f955", "members": [], "meta": { "created": "2020-03-24T10:21:24.686000", "lastModified": null, "location": "http://scimapi.eduid.docker/scim/test/Groups/c3819cbe-c893-4070-824c-fe3d0db8f955", "resourceType": "Group", "version": "5e79df24f77769b475177bc7" }, "schemas": [ "urn:ietf:params:scim:schemas:core:2.0:Group" ] } """ if scim_id: self.context.logger.info(f"Fetching group {scim_id}") db_group = ctx_groupdb(req).get_group_by_scim_id(scim_id) self.context.logger.debug(f'Found group: {db_group}') if not db_group: raise NotFound(detail="Group not found") self._db_group_to_response(resp, db_group) return # Return all Groups for scope db_groups = ctx_groupdb(req).get_groups() resources = [] for db_group in db_groups: resources.append({ 'id': str(db_group.scim_id), 'displayName': db_group.graph.display_name }) list_response = ListResponse(total_results=len(db_groups), resources=resources) resp.media = ListResponseSchema().dump(list_response)
def on_get(self, req: Request, resp: Response, scim_id: Optional[str] = None): if scim_id is None: raise BadRequest(detail='Not implemented') self.context.logger.info(f'Fetching event {scim_id}') db_event = ctx_eventdb(req).get_event_by_scim_id(scim_id) if not db_event: raise NotFound(detail='Event not found') self._db_event_to_response(req, resp, db_event)
def on_get(self, req: Request, resp: Response, scim_id: Optional[str] = None): if scim_id is None: raise BadRequest(detail='Not implemented') self.context.logger.info(f'Fetching user {scim_id}') db_user = ctx_userdb(req).get_user_by_scim_id(scim_id) if not db_user: raise NotFound(detail='User not found') self._db_user_to_response(req=req, resp=resp, db_user=db_user)
def on_get(self, req: Request, resp: Response, scim_id: Optional[str] = None): if scim_id is None: raise BadRequest(detail='Not implemented') self.context.logger.info(f'Fetching invite {scim_id}') db_invite = ctx_invitedb(req).get_invite_by_scim_id(scim_id) if not db_invite: raise NotFound(detail='Invite not found') ref = self._create_signup_ref(req, db_invite) signup_invite = self.context.signup_invitedb.get_invite_by_reference( ref) self._db_invite_to_response(req, resp, db_invite, signup_invite)
def on_put(self, req: Request, resp: Response, scim_id: str): """ PUT /Groups/c3819cbe-c893-4070-824c-fe3d0db8f955 HTTP/1.1 Host: example.com Accept: application/scim+json Content-Type: application/scim+json Authorization: Bearer h480djs93hd8 If-Match: W/"5e79df24f77769b475177bc7" Content-Length: ... { "schemas": ["urn:ietf:params:scim:schemas:core:2.0:Group"], "displayName": "Test SCIMv2", "members": [ { "value": "2819c223-7f76-453a-919d-413861904646", "$ref": "https://example.com/v2/Users/2819c223-7f76-453a-919d-413861904646", "display": "Babs Jensen" }, ] } HTTP/1.1 200 OK content-type: application/json; charset=UTF-8 etag: W/"5e79df24f77769b475177bc7" location: http://scimapi.eduid.docker/scim/test/Groups/c3819cbe-c893-4070-824c-fe3d0db8f955 { "displayName": "test group", "id": "c3819cbe-c893-4070-824c-fe3d0db8f955", "members": [ { "value": "2819c223-7f76-453a-919d-413861904646", "$ref": "https://example.com/v2/Users/2819c223-7f76-453a-919d-413861904646", "display": "Babs Jensen" }, ], "meta": { "created": "2020-03-24T10:21:24.686000", "lastModified": 2020-03-25T14:42:24.686000, "location": "http://scimapi.eduid.docker/scim/test/Groups/c3819cbe-c893-4070-824c-fe3d0db8f955", "resourceType": "Group", "version": "3e79d424f77269f475177bc5" }, "schemas": [ "urn:ietf:params:scim:schemas:core:2.0:Group" ] } """ self.context.logger.info('Updating group') try: update_request = GroupUpdateRequestSchema().load(req.media) except ValidationError as e: raise BadRequest(detail=f"{e}") self.context.logger.debug(update_request) if scim_id != str(update_request.id): self.context.logger.error(f'Id mismatch') self.context.logger.debug(f'{scim_id} != {update_request.id}') raise BadRequest(detail='Id mismatch') self.context.logger.info(f"Fetching group {scim_id}") db_group = ctx_groupdb(req).get_group_by_scim_id(str( update_request.id)) self.context.logger.debug(f'Found group: {db_group}') if not db_group: raise NotFound(detail="Group not found") # Check version if not self._check_version(req, db_group): raise BadRequest(detail="Version mismatch") # Check that members exists in their respective db self.context.logger.info(f'Checking if group and user members exists') for member in update_request.members: if member.is_group: if not ctx_groupdb(req).group_exists(str(member.value)): self.context.logger.error( f'Group {member.value} not found') raise BadRequest(detail=f'Group {member.value} not found') if member.is_user: if not ctx_userdb(req).user_exists(scim_id=str(member.value)): self.context.logger.error(f'User {member.value} not found') raise BadRequest(detail=f'User {member.value} not found') updated_group, changed = ctx_groupdb(req).update_group( update_request=update_request, db_group=db_group) # Load the group from the database to ensure results are consistent with subsequent GETs. # For example, timestamps have higher resolution in updated_group than after a load. db_group = ctx_groupdb(req).get_group_by_scim_id( str(updated_group.scim_id)) assert db_group # please mypy if changed: add_api_event( context=self.context, data_owner=req.context['data_owner'], db_obj=db_group, resource_type=SCIMResourceType.GROUP, level=EventLevel.INFO, status=EventStatus.UPDATED, message='Group was updated', ) self._db_group_to_response(resp, db_group)
def on_put(self, req: Request, resp: Response, scim_id): try: self.context.logger.info(f'Updating user {scim_id}') update_request: UserUpdateRequest = UserUpdateRequestSchema().load( req.media) self.context.logger.debug(update_request) if scim_id != str(update_request.id): self.context.logger.error(f'Id mismatch') self.context.logger.debug(f'{scim_id} != {update_request.id}') raise BadRequest(detail='Id mismatch') db_user = ctx_userdb(req).get_user_by_scim_id(scim_id) if not db_user: raise NotFound(detail="User not found") # Check version if not self._check_version(req, db_user): raise BadRequest(detail="Version mismatch") self.context.logger.debug( f'Extra debug: user {scim_id} as dict:\n{db_user.to_dict()}') core_changed = False if SCIMSchema.CORE_20_USER in update_request.schemas: name_in = ScimApiName(**asdict(update_request.name)) emails_in = set( ScimApiEmail(**asdict(email)) for email in update_request.emails) phone_numbers_in = set( ScimApiPhoneNumber(**asdict(number)) for number in update_request.phone_numbers) # external_id if update_request.external_id != db_user.external_id: db_user = replace(db_user, external_id=update_request.external_id) core_changed = True # preferred_language if update_request.preferred_language != db_user.preferred_language: db_user = replace( db_user, preferred_language=update_request.preferred_language) core_changed = True # name if name_in != db_user.name: db_user = replace(db_user, name=name_in) core_changed = True # emails if emails_in != set(db_user.emails): db_user = replace(db_user, emails=list(emails_in)) core_changed = True # phone_numbers if phone_numbers_in != set(db_user.phone_numbers): db_user = replace(db_user, phone_numbers=list(phone_numbers_in)) core_changed = True nutid_changed = False if SCIMSchema.NUTID_USER_V1 in update_request.schemas: # Look for changes in profiles for this in update_request.nutid_user_v1.profiles.keys(): if this not in db_user.profiles: self.context.logger.info( f'Adding profile {this}/{update_request.nutid_user_v1.profiles[this]} to user' ) nutid_changed = True elif update_request.nutid_user_v1.profiles[this].to_dict( ) != db_user.profiles[this].to_dict(): self.context.logger.info( f'Profile {this}/{update_request.nutid_user_v1.profiles[this]} updated' ) nutid_changed = True else: self.context.logger.info( f'Profile {this}/{update_request.nutid_user_v1.profiles[this]} not changed' ) for this in db_user.profiles.keys(): if this not in update_request.nutid_user_v1.profiles: self.context.logger.info( f'Profile {this}/{db_user.profiles[this]} removed') nutid_changed = True if nutid_changed: for profile_name, profile in update_request.nutid_user_v1.profiles.items( ): db_profile = ScimApiProfile( attributes=profile.attributes, data=profile.data) db_user.profiles[profile_name] = db_profile self.context.logger.debug( f'Core changed: {core_changed}, nutid_changed: {nutid_changed}' ) if core_changed or nutid_changed: self._save_user(req, db_user) add_api_event( context=self.context, data_owner=req.context['data_owner'], db_obj=db_user, resource_type=SCIMResourceType.USER, level=EventLevel.INFO, status=EventStatus.UPDATED, message='User was updated', ) else: self.context.logger.info(f'No changes detected') self._db_user_to_response(req=req, resp=resp, db_user=db_user) except ValidationError as e: raise BadRequest(detail=f"{e}")