public async Task <bool> Execute(ResourceOwner parameter)
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            if (string.IsNullOrWhiteSpace(parameter.Id))
            {
                throw new ArgumentNullException(nameof(parameter.Id));
            }

            if (await _resourceOwnerRepository.GetAsync(parameter.Id) == null)
            {
                throw new IdentityServerManagerException(
                          ErrorCodes.InvalidParameterCode,
                          string.Format(ErrorDescriptions.TheResourceOwnerDoesntExist, parameter.Id));
            }

            if (parameter.Claims != null)
            {
                Claim updatedClaim;
                if (((updatedClaim = parameter.Claims.FirstOrDefault(c => c.Type == SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.UpdatedAt)) != null))
                {
                    parameter.Claims.Remove(updatedClaim);
                }

                parameter.Claims.Add(new Claim(SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.UpdatedAt, DateTime.UtcNow.ToString()));
            }

            return(await _resourceOwnerRepository.UpdateAsync(parameter));
        }
Exemple #2
0
        public async Task <bool> Execute(UpdateResourceOwnerClaimsParameter request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var resourceOwner = await _resourceOwnerRepository.GetAsync(request.Login);

            if (resourceOwner == null)
            {
                throw new IdentityServerManagerException(ErrorCodes.InvalidParameterCode, string.Format(ErrorDescriptions.TheResourceOwnerDoesntExist, request.Login));
            }

            resourceOwner.UpdateDateTime = DateTime.UtcNow;
            var claims         = new List <Claim>();
            var existingClaims = await _claimRepository.GetAllAsync();

            if (existingClaims != null && existingClaims.Any() && request.Claims != null && request.Claims.Any())
            {
                foreach (var claim in request.Claims)
                {
                    var cl = existingClaims.FirstOrDefault(c => c.Code == claim.Key);
                    if (cl == null)
                    {
                        continue;
                    }

                    claims.Add(new Claim(claim.Key, claim.Value));
                }
            }

            resourceOwner.Claims = claims;
            Claim updatedClaim, subjectClaim;

            if (((updatedClaim = resourceOwner.Claims.FirstOrDefault(c => c.Type == SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.UpdatedAt)) != null))
            {
                resourceOwner.Claims.Remove(updatedClaim);
            }

            if (((subjectClaim = resourceOwner.Claims.FirstOrDefault(c => c.Type == SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.Subject)) != null))
            {
                resourceOwner.Claims.Remove(subjectClaim);
            }

            resourceOwner.Claims.Add(new Claim(SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.Subject, request.Login));
            resourceOwner.Claims.Add(new Claim(SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.UpdatedAt, DateTime.UtcNow.ToString()));
            var result = await _resourceOwnerRepository.UpdateAsync(resourceOwner);

            if (!result)
            {
                throw new IdentityServerManagerException(ErrorCodes.InternalErrorCode, ErrorDescriptions.TheClaimsCannotBeUpdated);
            }

            return(true);
        }
        public async Task Execute(ClaimsPrincipal claimsPrincipal)
        {
            if (claimsPrincipal == null)
            {
                throw new ArgumentNullException(nameof(claimsPrincipal));
            }

            if (claimsPrincipal.Identity == null ||
                !claimsPrincipal.Identity.IsAuthenticated ||
                !(claimsPrincipal.Identity is ClaimsIdentity))
            {
                throw new IdentityServerException(
                          Errors.ErrorCodes.UnhandledExceptionCode,
                          Errors.ErrorDescriptions.TheUserNeedsToBeAuthenticated);
            }

            var subject = claimsPrincipal.GetSubject();

            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new IdentityServerException(
                          Errors.ErrorCodes.UnhandledExceptionCode,
                          Errors.ErrorDescriptions.TheSubjectCannotBeRetrieved);
            }

            var result = await _authenticateResourceOwnerService.AuthenticateResourceOwnerAsync(subject);

            if (result == null)
            {
                throw new IdentityServerException(
                          Errors.ErrorCodes.UnhandledExceptionCode,
                          Errors.ErrorDescriptions.TheRoDoesntExist);
            }

            if (result.IsLocalAccount)
            {
                throw new IdentityServerException(
                          Errors.ErrorCodes.UnhandledExceptionCode,
                          Errors.ErrorDescriptions.TheAccountHasAlreadyBeenActivated);
            }

            result.IsLocalAccount = true;
            if (result.Claims != null)
            {
                Claim updatedClaim;
                if (((updatedClaim = result.Claims.FirstOrDefault(c => c.Type == Jwt.Constants.StandardResourceOwnerClaimNames.UpdatedAt)) != null))
                {
                    result.Claims.Remove(updatedClaim);
                }

                result.Claims.Add(new Claim(Jwt.Constants.StandardResourceOwnerClaimNames.UpdatedAt, DateTime.UtcNow.ToString()));
            }

            await _resourceOwnerRepository.UpdateAsync(result);
        }
Exemple #4
0
        public async Task <bool> Execute(string subject, IEnumerable <ClaimAggregate> claims)
        {
            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentNullException(nameof(subject));
            }

            if (claims == null)
            {
                throw new ArgumentNullException(nameof(claims));
            }

            var resourceOwner = await _resourceOwnerRepository.GetAsync(subject);

            if (resourceOwner == null)
            {
                throw new IdentityServerException(Errors.ErrorCodes.InternalError, Errors.ErrorDescriptions.TheRoDoesntExist);
            }

            var supportedClaims = await _claimRepository.GetAllAsync();

            claims = claims.Where(c => supportedClaims.Any(sp => sp.Code == c.Code && !Jwt.Constants.NotEditableResourceOwnerClaimNames.Contains(c.Code)));
            var claimsToBeRemoved = resourceOwner.Claims
                                    .Where(cl => claims.Any(c => c.Code == cl.Type))
                                    .Select(cl => resourceOwner.Claims.IndexOf(cl))
                                    .OrderByDescending(p => p)
                                    .ToList();

            foreach (var index in claimsToBeRemoved)
            {
                resourceOwner.Claims.RemoveAt(index);
            }

            foreach (var claim in claims)
            {
                if (string.IsNullOrWhiteSpace(claim.Value))
                {
                    continue;
                }

                resourceOwner.Claims.Add(new Claim(claim.Code, claim.Value));
            }

            Claim updatedClaim;

            if (((updatedClaim = resourceOwner.Claims.FirstOrDefault(c => c.Type == Jwt.Constants.StandardResourceOwnerClaimNames.UpdatedAt)) != null))
            {
                resourceOwner.Claims.Remove(updatedClaim);
            }

            resourceOwner.Claims.Add(new Claim(Jwt.Constants.StandardResourceOwnerClaimNames.UpdatedAt, DateTime.UtcNow.ToString()));
            return(await _resourceOwnerRepository.UpdateAsync(resourceOwner));
        }
        public async Task <bool> Execute(string subject, string twoFactorAuth)
        {
            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentNullException(nameof(subject));
            }

            var resourceOwner = await _resourceOwnerRepository.GetAsync(subject);

            if (resourceOwner == null)
            {
                throw new IdentityServerException(Errors.ErrorCodes.InternalError, Errors.ErrorDescriptions.TheRoDoesntExist);
            }

            resourceOwner.TwoFactorAuthentication = twoFactorAuth;
            return(await _resourceOwnerRepository.UpdateAsync(resourceOwner));
        }
        public async Task <bool> Execute(UpdateUserParameter updateUserParameter)
        {
            if (updateUserParameter == null)
            {
                throw new ArgumentNullException(nameof(updateUserParameter));
            }

            if (string.IsNullOrWhiteSpace(updateUserParameter.Login))
            {
                throw new ArgumentNullException(nameof(updateUserParameter.Login));
            }

            var resourceOwner = await _authenticateResourceOwnerService.AuthenticateResourceOwnerAsync(updateUserParameter.Login);

            if (resourceOwner == null)
            {
                throw new IdentityServerException(
                          Errors.ErrorCodes.InternalError,
                          Errors.ErrorDescriptions.TheRoDoesntExist);
            }

            resourceOwner.TwoFactorAuthentication = updateUserParameter.TwoFactorAuthentication;
            if (!string.IsNullOrWhiteSpace(updateUserParameter.Password))
            {
                resourceOwner.Password = _authenticateResourceOwnerService.GetHashedPassword(updateUserParameter.Password);
            }

            resourceOwner.Claims = updateUserParameter.Claims;
            if (resourceOwner.Claims != null)
            {
                Claim updatedClaim;
                if (((updatedClaim = resourceOwner.Claims.FirstOrDefault(c => c.Type == Jwt.Constants.StandardResourceOwnerClaimNames.UpdatedAt)) != null))
                {
                    resourceOwner.Claims.Remove(updatedClaim);
                }

                resourceOwner.Claims.Add(new Claim(Jwt.Constants.StandardResourceOwnerClaimNames.UpdatedAt, DateTime.UtcNow.ToString()));
            }

            return(await _resourceOwnerRepository.UpdateAsync(resourceOwner));
        }
Exemple #7
0
        public async Task <bool> Execute(string subject, string newPassword)
        {
            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentNullException(nameof(subject));
            }

            if (string.IsNullOrWhiteSpace(newPassword))
            {
                throw new ArgumentNullException(nameof(newPassword));
            }

            var resourceOwner = await _resourceOwnerRepository.GetAsync(subject);

            if (resourceOwner == null)
            {
                throw new IdentityServerException(Errors.ErrorCodes.InternalError, Errors.ErrorDescriptions.TheRoDoesntExist);
            }

            resourceOwner.Password = PasswordHelper.ComputeHash(newPassword);
            return(await _resourceOwnerRepository.UpdateAsync(resourceOwner));
        }
        public async Task <bool> Execute(UpdateResourceOwnerPasswordParameter request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var resourceOwner = await _resourceOwnerRepository.GetAsync(request.Login);

            if (resourceOwner == null)
            {
                throw new IdentityServerManagerException(ErrorCodes.InvalidParameterCode, string.Format(ErrorDescriptions.TheResourceOwnerDoesntExist, request.Login));
            }

            resourceOwner.Password = PasswordHelper.ComputeHash(request.Password);
            var result = await _resourceOwnerRepository.UpdateAsync(resourceOwner);

            if (!result)
            {
                throw new IdentityServerManagerException(ErrorCodes.InternalErrorCode, ErrorDescriptions.ThePasswordCannotBeUpdated);
            }

            return(true);
        }
Exemple #9
0
        [HttpPut] // User Authentication enabled.
        public async Task <IActionResult> Update([FromBody] JObject json)
        {
            if (json == null)
            {
                throw new ArgumentNullException(nameof(json));
            }

            // 1. Get the subject.
            var subjectResult = await GetSubject();

            if (!subjectResult.IsValid)
            {
                return(subjectResult.Error);
            }

            // 2. Check the user exists.
            var user = await _resourceOwnerRepository.GetAsync(subjectResult.Subject);

            if (user == null)
            {
                return(this.BuildError(ErrorCodes.InvalidRequestCode, ErrorDescriptions.TheRoDoesntExist, HttpStatusCode.NotFound));
            }

            // 4. Construct the request and update user information.
            var updateParameter = _requestBuilder.GetUpdateUserParameter(json);

            updateParameter.Login = subjectResult.Subject;
            var claims         = updateParameter.Claims;
            var assignedClaims = claims.Where(c => c.Type != SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.Picture && c.Type != Constants.Claims.BannerImage && c.Type != SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.Subject).ToList();

            assignedClaims.Add(new Claim(SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.Subject, subjectResult.Subject));
            var pictureClaim = claims.FirstOrDefault(c => c.Type == SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.Picture);
            var bannerClaim  = claims.FirstOrDefault(c => c.Type == Constants.Claims.BannerImage);

            if (pictureClaim != null)
            {
                string path;
                if (AddImage(pictureClaim.Value, subjectResult.Subject, true, out path))
                {
                    assignedClaims.Add(new Claim(SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.Picture, path));
                }
                else
                {
                    assignedClaims.Add(pictureClaim);
                }
            }

            if (bannerClaim != null)
            {
                string path;
                if (AddImage(bannerClaim.Value, subjectResult.Subject, false, out path))
                {
                    assignedClaims.Add(new Claim(Constants.Claims.BannerImage, path));
                }
                else
                {
                    assignedClaims.Add(bannerClaim);
                }
            }


            user.TwoFactorAuthentication = updateParameter.TwoFactorAuthentication;
            if (!string.IsNullOrWhiteSpace(updateParameter.Password) && user.IsLocalAccount)
            {
                if (!Regex.IsMatch(updateParameter.Password, @"^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$"))
                {
                    return(this.BuildError(ErrorCodes.InvalidRequestCode, "the password is not correct", HttpStatusCode.InternalServerError));
                }

                user.Password = _authenticateResourceOwnerService.GetHashedPassword(updateParameter.Password);
            }

            user.Claims = assignedClaims;
            if (user.Claims != null)
            {
                Claim updatedClaim;
                if (((updatedClaim = user.Claims.FirstOrDefault(c => c.Type == SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.UpdatedAt)) != null))
                {
                    user.Claims.Remove(updatedClaim);
                }

                user.Claims.Add(new Claim(SimpleIdentityServer.Core.Jwt.Constants.StandardResourceOwnerClaimNames.UpdatedAt, DateTime.UtcNow.ToString()));
            }

            // 5. Update the information.
            if (!await _resourceOwnerRepository.UpdateAsync(user))
            {
                return(this.BuildError(ErrorCodes.UnhandledExceptionCode, Constants.ErrorMessages.ErrorOccuredWhileTryingToUpdateTheUser));
            }

            return(new OkResult());
        }