public async Task When_Passing_Null_Parameters_Then_Exceptions_Are_Thrown()
 {
     await Assert
     .ThrowsAsync <ArgumentNullException>(
         () => _resourceOwnerRepositoryStub.Update(null, CancellationToken.None))
     .ConfigureAwait(false);
 }
        public async Task <Option> Execute(string subject, string twoFactorAuth, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(subject))
            {
                _logger.LogError("Subject is null");
                throw new ArgumentNullException(nameof(subject));
            }

            var resourceOwner = await _resourceOwnerRepository.Get(subject, cancellationToken).ConfigureAwait(false);

            if (resourceOwner == null)
            {
                _logger.LogError(Strings.TheRoDoesntExist);
                return(new Option.Error(
                           new ErrorDetails
                {
                    Title = ErrorCodes.InternalError,
                    Detail = Strings.TheRoDoesntExist,
                    Status = HttpStatusCode.InternalServerError
                }));
            }

            resourceOwner.TwoFactorAuthentication = twoFactorAuth;
            return(await _resourceOwnerRepository.Update(resourceOwner, cancellationToken).ConfigureAwait(false));
        }
Example #3
0
        public async Task <IActionResult> Update(string id, UpdateResourceOwnerClaimsRequest?request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                return(BadRequest(
                           new ErrorDetails
                {
                    Title = ErrorCodes.InvalidRequest,
                    Detail = Strings.ParameterInRequestBodyIsNotValid,
                    Status = HttpStatusCode.BadRequest
                }));
            }

            request = request with {
                Subject = id
            };

            var resourceOwner =
                await _resourceOwnerRepository.Get(request.Subject, cancellationToken).ConfigureAwait(false);

            if (resourceOwner == null)
            {
                return(BadRequest(
                           new ErrorDetails
                {
                    Status = HttpStatusCode.BadRequest,
                    Title = ErrorCodes.InvalidParameterCode,
                    Detail = Strings.TheRoDoesntExist
                }));
            }

            var claims = request.Claims
                         .Where(c => c.Type != OpenIdClaimTypes.Subject)
                         .Where(c => c.Type != OpenIdClaimTypes.UpdatedAt)
                         .Select(claim => new Claim(claim.Type, claim.Value))
                         .Concat(
                new[]
            {
                new Claim(OpenIdClaimTypes.Subject, request.Subject),
                new Claim(
                    OpenIdClaimTypes.UpdatedAt,
                    DateTimeOffset.UtcNow.ConvertToUnixTimestamp().ToString())
            });

            resourceOwner.Claims = claims.ToArray();

            var result = await _resourceOwnerRepository.Update(resourceOwner, cancellationToken).ConfigureAwait(false);

            return(result switch
            {
                Option.Error => BadRequest(Strings.TheClaimsCannotBeUpdated),
                _ => RedirectToAction("Get", "ResourceOwners", new { id })
            });
Example #4
0
        private async Task <Option> UnlinkProfile(
            string localSubject,
            string externalSubject,
            string issuer,
            CancellationToken cancellationToken)
        {
            var resourceOwner =
                await _resourceOwnerRepository.Get(localSubject, cancellationToken).ConfigureAwait(false);

            if (resourceOwner == null)
            {
                return(new Option.Error(
                           new ErrorDetails
                {
                    Title = ErrorCodes.InternalError,
                    Detail = Strings.TheRoDoesntExist,
                    Status = HttpStatusCode.InternalServerError
                }));
            }

            var unlink = resourceOwner.ExternalLogins.Where(
                x => x.Subject == externalSubject && (x.Issuer == issuer || issuer == CookieNames.CookieName))
                         .ToArray();

            if (unlink.Length <= 0)
            {
                return(new Option.Error(new ErrorDetails {
                    Title = ErrorCodes.InvalidRequest
                }));
            }

            resourceOwner.ExternalLogins = resourceOwner.ExternalLogins.Remove(unlink);
            var result = await _resourceOwnerRepository.Update(resourceOwner, cancellationToken)
                         .ConfigureAwait(false);

            return(result);
        }
Example #5
0
        public async Task <Option> Execute(string subject, IEnumerable <Claim> claims, CancellationToken cancellationToken)
        {
            var resourceOwner = await _resourceOwnerRepository.Get(subject, cancellationToken).ConfigureAwait(false);

            if (resourceOwner == null)
            {
                _logger.LogError(Strings.TheRoDoesntExist);
                return(new Option.Error(new ErrorDetails
                {
                    Title = ErrorCodes.InternalError,
                    Detail = Strings.TheRoDoesntExist,
                    Status = HttpStatusCode.InternalServerError
                }));
            }

            var claimsToBeRemoved = resourceOwner.Claims.Where(cl => claims.Any(c => c.Type == cl.Type))
                                    .ToArray();

            resourceOwner.Claims = resourceOwner.Claims.Remove(claimsToBeRemoved);

            resourceOwner.Claims =
                resourceOwner.Claims.Add(claims.Where(x => !string.IsNullOrWhiteSpace(x.Value)).ToArray());

            Claim?updatedClaim;

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

            resourceOwner.Claims = resourceOwner.Claims.Add(
                new Claim(OpenIdClaimTypes.UpdatedAt, DateTimeOffset.UtcNow.ConvertToUnixTimestamp().ToString()));
            return(await _resourceOwnerRepository.Update(resourceOwner, cancellationToken).ConfigureAwait(false));
        }
Example #6
0
 public bool Update(Domain.ResourceOwner entity)
 {
     return(_resourceOwnerRepository.Update(entity));
 }