Example #1
0
        private JObject CreateScimGroupJobject(ScimGroup scimGroup)
        {
            JArray membersJarray    = new JArray();
            var    scimGroupMembers = scimGroup.Members;

            foreach (var scimGroupMember in scimGroupMembers)
            {
                JObject jobt = new JObject()
                {
                    new JProperty("value", scimGroupMember.Value),
                    new JProperty("$ref", scimGroupMember.Reference),
                    new JProperty("display", scimGroupMember.Display)
                };
                membersJarray.Add(jobt);
            }
            JObject jObject = new JObject
            {
                new JProperty("schemas", new JArray(scimGroup.Schemas.FirstOrDefault())),
                new JProperty("id", scimGroup.ScimGroupId),
                new JProperty("externalId", scimGroup.ExternalId),
                new JProperty("displayName", scimGroup.DisplayName),
                new JProperty("members", membersJarray),
                new JProperty("meta", new JObject
                {
                    new JProperty("resourceType", scimGroup.Meta.ResourceType),
                    new JProperty("created", scimGroup.Meta.Created),
                    new JProperty("lastModified", scimGroup.Meta.LastModified),
                    new JProperty("location", scimGroup.Meta.Location),
                    new JProperty("version", scimGroup.Meta.Version)
                }),
            };

            return(jObject);
        }
Example #2
0
        public virtual async Task<IScimResponse<ScimGroup>> UpdateGroup(ScimGroup group)
        {
            return await (await RetrieveGroup(group.Id))
                .BindAsync<ScimGroup, ScimGroup>(async groupRecord =>
                {
                    group.Meta = new ResourceMetadata(ScimConstants.ResourceTypes.Group)
                    {
                        Created = groupRecord.Meta.Created,
                        LastModified = groupRecord.Meta.LastModified
                    };

                    _CanonicalizationService.Canonicalize(group, ServerConfiguration.GetScimTypeDefinition(group.GetType()));

                    var validator = await _ResourceValidatorFactory.CreateValidator(group);
                    var validationResult = (await validator.ValidateUpdateAsync(group, groupRecord)).ToScimValidationResult();

                    if (!validationResult)
                        return new ScimErrorResponse<ScimGroup>(validationResult.Errors.First());

                    SetResourceVersion(group);

                    // if both versions are equal, bypass persistence
                    if (string.Equals(group.Meta.Version, groupRecord.Meta.Version))
                        return new ScimDataResponse<ScimGroup>(groupRecord);

                    var updatedGroup = await _GroupRepository.UpdateGroup(group);

                    // set version of updated entity returned by repository
                    SetResourceVersion(updatedGroup);

                    return new ScimDataResponse<ScimGroup>(updatedGroup);
                });
        }
Example #3
0
        public async Task <IActionResult> Patch(string id)
        {
            var group = await _groupRepository.GetByIdAsync(new Guid(id));

            if (group == null || group.OrganizationId != _orgId)
            {
                throw new NotFoundException();
            }

            var memstream = new MemoryStream();

            Request.Body.CopyTo(memstream);
            memstream.Position = 0;
            using (var reader = new StreamReader(memstream))
            {
                var text = reader.ReadToEnd();
                Debug.WriteLine(text);
            }

            // TODO: Do patch

            var result = new ScimGroup(group);

            return(new OkObjectResult(result));
        }
Example #4
0
        public IActionResult CreateScimGroup([FromBody] JObject jObject)
        {
            try
            {
                var    scimGroupId = Guid.NewGuid().ToString();
                var    externalId  = (string)jObject["externalId"];
                var    displayName = (string)jObject["displayName"];
                JArray members     = (JArray)jObject["members"];

                var creationTime = DateTime.UtcNow;
                var varsion      = CommonFunctions.GetSHA256HashedString(creationTime.ToString());
                var etag         = "W/\"" + varsion + "\"";


                var scimGroupMeta = new ScimGroupMetaData
                {
                    ResourceType = "urn:ietf:params:scim:schemas:core:2.0:Group",
                    Created      = creationTime,
                    LastModified = creationTime,
                    Location     = new Uri(this.Url.Link("GetScimGroupById", new { id = scimGroupId })).ToString(),
                    Version      = etag
                };

                var scimGroup = new ScimGroup
                {
                    ScimGroupId = scimGroupId,
                    Schemas     = new string[] { "urn:ietf:params:scim:schemas:core:2.0:Group" },
                    ExternalId  = externalId,
                    DisplayName = displayName,
                    Members     = new ScimGroupMember[] { }
                };
                scimGroup.Meta          = scimGroupMeta;
                scimGroupMeta.ScimGroup = scimGroup;

                _applicationDbContext.scimGroupMetaDatas.Add(scimGroupMeta);
                _applicationDbContext.scimGroups.Add(scimGroup);
                _applicationDbContext.SaveChanges();

                var createdScimGroup     = _applicationDbContext.scimGroups.Where(sg => sg.ScimGroupId == scimGroupId).FirstOrDefault();
                var createdScimGroupMeta = _applicationDbContext.scimGroupMetaDatas.Where(sgm => sgm.ScimGroupId == scimGroupId).FirstOrDefault();
                createdScimGroup.Meta = createdScimGroupMeta;
                var returnedScimGroupJobject = CreateScimGroupJobject(createdScimGroup);
                var relativePath             = createdScimGroup.Meta.Location;

                Response.Headers.Add("Etag", etag);
                Response.Headers.Add("Content-Type", "application/scim+json");

                return(Created(relativePath, returnedScimGroupJobject));
            }
            catch (Exception exception)
            {
                var errorJobject = CommonFunctions.CreateErrorJobject(exception);
                Response.Headers.Add("Content-Type", "application/scim+json");

                return(StatusCode(StatusCodes.Status500InternalServerError, errorJobject));
            }
        }
        public async Task<ScimGroup> UpdateGroup(ScimGroup group)
        {
            if (!_Groups.ContainsKey(group.Id))
                return null;

            group.Meta.LastModified = DateTime.UtcNow;
            _Groups[group.Id] = group;

            return group;
        }
        protected static ScimGroup CreateGroup(ScimGroup group)
        {
            var response = Server
                .HttpClient
                .PostAsync("v2/groups", new ScimObjectContent<ScimGroup>(group)).Result;

            return response.StatusCode == HttpStatusCode.Created
                ? response.Content.ScimReadAsAsync<ScimGroup2>().Result
                : null;
        }
Example #7
0
        public async Task <IActionResult> Post([FromBody] ScimGroup model)
        {
            var group = model.ToGroup(_orgId);
            await _groupService.SaveAsync(group);

            var result = new ScimGroup(group);
            var getUrl = Url.Action("Get", "Groups", new { id = group.Id.ToString() }, Request.Protocol, Request.Host.Value);

            return(new CreatedResult(getUrl, result));
        }
Example #8
0
        protected static ScimGroup CreateGroup(ScimGroup group)
        {
            var response = Server
                           .HttpClient
                           .PostAsync("v2/groups", new ScimObjectContent <ScimGroup>(group)).Result;

            return(response.StatusCode == HttpStatusCode.Created
                ? response.Content.ScimReadAsAsync <ScimGroup2>().Result
                : null);
        }
Example #9
0
        private void PopulateMemberRef(ScimGroup group)
        {
            // needed to materialize ienumerable, otherwise it did not work
            var members = group.Members?.ToList();

            members?.ForEach(m =>
                             m.Ref = m.Type == ScimConstants.ResourceTypes.User
                    ? GetUserUri(UsersController.RetrieveUserRouteName, m.Value)
                    : GetGroupUri(RetrieveGroupRouteName, m.Value));
            group.Members = members;
        }
Example #10
0
        public Task <ScimGroup> UpdateGroup(ScimGroup group)
        {
            if (_Groups.ContainsKey(group.Id))
            {
                group.Meta.LastModified = DateTime.UtcNow;
                _Groups[group.Id]       = group;

                return(Task.FromResult(group));
            }

            return(Task.FromResult <ScimGroup>(null));
        }
Example #11
0
        public async Task <ScimGroup> UpdateGroup(ScimGroup group)
        {
            if (!_Groups.ContainsKey(group.Id))
            {
                return(null);
            }

            group.Meta.LastModified = DateTime.UtcNow;
            _Groups[group.Id]       = group;

            return(group);
        }
        public async Task<ScimGroup> CreateGroup(ScimGroup group)
        {
            group.Id = Guid.NewGuid().ToString("N");

            var createdDate = DateTime.UtcNow;
            group.Meta.Created = createdDate;
            group.Meta.LastModified = createdDate;

            _Groups.Add(group.Id, group);

            return group;
        }
Example #13
0
        public Task <ScimGroup> CreateGroup(ScimGroup group)
        {
            group.Id = Guid.NewGuid().ToString("N");

            var createdDate = DateTime.UtcNow;

            group.Meta.Created      = createdDate;
            group.Meta.LastModified = createdDate;

            _Groups.Add(group.Id, group);

            return(Task.FromResult(group));
        }
Example #14
0
        public async Task<HttpResponseMessage> Post(ScimGroup groupDto)
        {
            return (await _GroupService.CreateGroup(groupDto))
                .Let(group => SetMetaLocation(group, RetrieveGroupRouteName, new { groupId = group.Id }))
                .Let(PopulateMemberRef)
                .ToHttpResponseMessage(Request, (group, response) =>
                {
                    response.StatusCode = HttpStatusCode.Created;

                    SetContentLocationHeader(response, RetrieveGroupRouteName, new { groupId = group.Id });
                    SetETagHeader(response, group);
                });
        }
Example #15
0
        public async Task <IActionResult> Post([FromBody] ScimGroup groupDto)
        {
            try
            {
                var user = await groupService.CreateGroup(groupDto);

                return(Created(Request.Host.Value + "/api/groups/" + user.Id, user));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.InnerException.StackTrace));
            }
        }
Example #16
0
        public async Task <HttpResponseMessage> Put(string groupId, ScimGroup groupDto)
        {
            groupDto.Id = groupId;

            return((await _GroupService.UpdateGroup(groupDto))
                   .Let(group => SetMetaLocation(group, RetrieveGroupRouteName, new { groupId = group.Id }))
                   .Let(PopulateMemberRef)
                   .ToHttpResponseMessage(Request, (group, response) =>
            {
                SetContentLocationHeader(response, RetrieveGroupRouteName, new { groupId = group.Id });
                SetETagHeader(response, group);
            }));
        }
Example #17
0
        public async Task <IActionResult> Get(string id)
        {
            var group = await _groupRepository.GetByIdAsync(new Guid(id));

            if (group == null || group.OrganizationId != _orgId)
            {
                throw new NotFoundException();
            }

            var result = new ScimGroup(group);

            return(new OkObjectResult(result));
        }
Example #18
0
        public async Task <HttpResponseMessage> Post(ScimGroup groupDto)
        {
            return((await _GroupService.CreateGroup(groupDto))
                   .Let(group => SetMetaLocation(group, RetrieveGroupRouteName, new { groupId = group.Id }))
                   .Let(PopulateMemberRef)
                   .ToHttpResponseMessage(Request, (group, response) =>
            {
                response.StatusCode = HttpStatusCode.Created;

                SetContentLocationHeader(response, RetrieveGroupRouteName, new { groupId = group.Id });
                SetETagHeader(response, group);
            }));
        }
Example #19
0
        public async Task <IActionResult> Put(string id, [FromBody] ScimGroup model)
        {
            var group = await _groupRepository.GetByIdAsync(new Guid(id));

            if (group == null || group.OrganizationId != _orgId)
            {
                throw new NotFoundException();
            }

            group = model.ToGroup(group);
            await _groupService.SaveAsync(group);

            var result = new ScimGroup(group);

            return(new OkObjectResult(result));
        }
Example #20
0
        public virtual async Task<IScimResponse<ScimGroup>> CreateGroup(ScimGroup group)
        {
            _CanonicalizationService.Canonicalize(group, ServerConfiguration.GetScimTypeDefinition(group.GetType()));

            var validator = await _ResourceValidatorFactory.CreateValidator(group);
            var validationResult = (await validator.ValidateCreateAsync(group)).ToScimValidationResult();

            if (!validationResult)
                return new ScimErrorResponse<ScimGroup>(validationResult.Errors.First());

            group.Meta = new ResourceMetadata(ScimConstants.ResourceTypes.Group);
            
            var groupRecord = await _GroupRepository.CreateGroup(group);

            SetResourceVersion(groupRecord);

            return new ScimDataResponse<ScimGroup>(groupRecord);
        }
Example #21
0
        public async Task <IActionResult> Put(int grouId, [FromBody] ScimGroup group)
        {
            try
            {
                //userDto.Id = userId;
                var grp = await groupService.UpdateGroup(group);

                AddLocationHeader("groups", grouId.ToString());
                return(Ok(grp));
            }
            catch (NotFoundException)
            {
                return(new NotFoundObjectResult("Group could not be found"));
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.InnerException.StackTrace));
            }
        }
Example #22
0
        public virtual async Task <IScimResponse <ScimGroup> > CreateGroup(ScimGroup group)
        {
            _CanonicalizationService.Canonicalize(group, ServerConfiguration.GetScimTypeDefinition(group.GetType()));

            var validator = await _ResourceValidatorFactory.CreateValidator(group);

            var validationResult = (await validator.ValidateCreateAsync(group)).ToScimValidationResult();

            if (!validationResult)
            {
                return(new ScimErrorResponse <ScimGroup>(validationResult.Errors.First()));
            }

            group.Meta = new ResourceMetadata(ScimConstants.ResourceTypes.Group);

            var groupRecord = await _GroupRepository.CreateGroup(group);

            SetResourceVersion(groupRecord);

            return(new ScimDataResponse <ScimGroup>(groupRecord));
        }
Example #23
0
        public virtual async Task <IScimResponse <ScimGroup> > UpdateGroup(ScimGroup group)
        {
            return(await(await RetrieveGroup(group.Id))
                   .BindAsync <ScimGroup, ScimGroup>(async groupRecord =>
            {
                group.Meta = new ResourceMetadata(ScimConstants.ResourceTypes.Group)
                {
                    Created = groupRecord.Meta.Created,
                    LastModified = groupRecord.Meta.LastModified
                };

                _CanonicalizationService.Canonicalize(group, ServerConfiguration.GetScimTypeDefinition(group.GetType()));

                var validator = await _ResourceValidatorFactory.CreateValidator(group);
                var validationResult = (await validator.ValidateUpdateAsync(group, groupRecord)).ToScimValidationResult();

                if (!validationResult)
                {
                    return new ScimErrorResponse <ScimGroup>(validationResult.Errors.First());
                }

                SetResourceVersion(group);

                // if both versions are equal, bypass persistence
                if (group.Meta.Version.Equals(groupRecord.Meta.Version))
                {
                    return new ScimDataResponse <ScimGroup>(groupRecord);
                }

                var updatedGroup = await _GroupRepository.UpdateGroup(group);

                // set version of updated entity returned by repository
                SetResourceVersion(updatedGroup);

                return new ScimDataResponse <ScimGroup>(updatedGroup);
            }));
        }
Example #24
0
        public async Task<HttpResponseMessage> Put(string groupId, ScimGroup groupDto)
        {
            groupDto.Id = groupId;

            return (await _GroupService.UpdateGroup(groupDto))
                .Let(group => SetMetaLocation(group, RetrieveGroupRouteName, new { groupId = group.Id }))
                .Let(PopulateMemberRef)
                .ToHttpResponseMessage(Request, (group, response) =>
                {
                    SetContentLocationHeader(response, RetrieveGroupRouteName, new { groupId = group.Id });
                    SetETagHeader(response, group);
                });
        }
        public async Task <ScimGroup> UpdateGroup(ScimGroup group)
        {
            var grp = mapper.Map <ScimGroup, Group>(group);

            return(mapper.Map <Group, ScimGroup>(await repository.UpdateGroup(grp)));
        }
Example #26
0
 private void PopulateMemberRef(ScimGroup group)
 {
     // needed to materialize ienumerable, otherwise it did not work
     var members = group.Members?.ToList();
     members?.ForEach(m =>
         m.Ref = m.Type == ScimConstants.ResourceTypes.User
             ? GetUserUri(UsersController.RetrieveUserRouteName, m.Value)
             : GetGroupUri(RetrieveGroupRouteName, m.Value));
     group.Members = members;
 }