예제 #1
0
        public async Task <ResourceOwner> Execute(string phoneNumber)
        {
            if (string.IsNullOrWhiteSpace(phoneNumber))
            {
                throw new ArgumentNullException(nameof(phoneNumber));
            }

            // 1. Send the confirmation code (SMS).
            await _generateAndSendSmsCodeOperation.Execute(phoneNumber);

            // 2. Try to get the resource owner.
            var resourceOwner = await _resourceOwnerRepository.GetResourceOwnerByClaim(Core.Jwt.Constants.StandardResourceOwnerClaimNames.PhoneNumber, phoneNumber);

            if (resourceOwner != null)
            {
                return(resourceOwner);
            }

            // 3. Create a new resource owner.
            var claims = new List <Claim>
            {
                new Claim(Core.Jwt.Constants.StandardResourceOwnerClaimNames.PhoneNumber, phoneNumber),
                new Claim(Core.Jwt.Constants.StandardResourceOwnerClaimNames.PhoneNumberVerified, "false")
            };
            var record = new AddUserParameter(Guid.NewGuid().ToString(), claims);
            await _userActions.AddUser(record).ConfigureAwait(false);

            return(await _resourceOwnerRepository.GetResourceOwnerByClaim(Core.Jwt.Constants.StandardResourceOwnerClaimNames.PhoneNumber, phoneNumber));
        }
        public async Task <ResourceOwner> Execute(string phoneNumber, string authenticatedUserSubject = null)
        {
            if (string.IsNullOrWhiteSpace(phoneNumber))
            {
                throw new ArgumentNullException(nameof(phoneNumber));
            }

            // 1. Check user exists
            ResourceOwner resourceOwner = null;

            if (!string.IsNullOrWhiteSpace(authenticatedUserSubject))
            {
                resourceOwner = await _userActions.GetUser(authenticatedUserSubject).ConfigureAwait(false);

                if (!resourceOwner.Claims.Any(c => c.Type == Core.Jwt.Constants.StandardResourceOwnerClaimNames.PhoneNumber && c.Value == phoneNumber))
                {
                    throw new InvalidOperationException("the phone number is not valid");
                }
            }
            else
            {
                resourceOwner = await _userActions.GetUserByClaim(Core.Jwt.Constants.StandardResourceOwnerClaimNames.PhoneNumber, phoneNumber);
            }

            if (!_smsAuthenticationOptions.IsSelfProvisioningEnabled && resourceOwner == null)
            {
                throw new InvalidOperationException("the user doesn't exist");
            }

            // 2. Send the confirmation code (SMS).
            await _generateAndSendSmsCodeOperation.Execute(phoneNumber);

            if (resourceOwner != null)
            {
                return(resourceOwner);
            }

            // 3. Create a new resource owner.
            var claims = new List <Claim>
            {
                new Claim(Core.Jwt.Constants.StandardResourceOwnerClaimNames.PhoneNumber, phoneNumber),
                new Claim(Core.Jwt.Constants.StandardResourceOwnerClaimNames.PhoneNumberVerified, "false")
            };
            var record = new AddUserParameter(claims);
            await _userActions.AddUser(record).ConfigureAwait(false);

            return(await _userActions.GetUserByClaim(Core.Jwt.Constants.StandardResourceOwnerClaimNames.PhoneNumber, phoneNumber));
        }
        public async Task <ActionResult> Edit(UpdateResourceOwnerViewModel viewModel)
        {
            if (viewModel == null)
            {
                throw new ArgumentNullException(nameof(viewModel));
            }

            await TranslateUserEditView(DefaultLanguage);

            var authenticatedUser = await SetUser();

            if (!ModelState.IsValid)
            {
                ViewBag.IsUpdated = false;
                return(View(viewModel));
            }

            var resourceOwner = await _userActions.GetUser(authenticatedUser);

            var subject = authenticatedUser.GetSubject();

            if (resourceOwner == null)
            {
                var record = viewModel.ToAddUserParameter();
                await _userActions.AddUser(record);
            }
            else
            {
                var parameter = viewModel.ToParameter();
                foreach (var newClaim in resourceOwner.Claims.Where(uc => !parameter.Claims.Any(pc => pc.Type == uc.Type)))
                {
                    parameter.Claims.Add(newClaim);
                }

                parameter.Login = subject;
                await _userActions.UpdateUser(parameter);
            }

            ViewBag.IsUpdated = true;
            return(View(viewModel));
        }
예제 #4
0
        /// <summary>
        /// Add an external account.
        /// </summary>
        /// <param name="authenticatedUser"></param>
        /// <returns></returns>
        protected async Task <string> AddExternalUser(ClaimsPrincipal authenticatedUser)
        {
            var openidClaims = authenticatedUser.Claims.ToOpenidClaims().ToList();

            if (_basicAuthenticateOptions.ClaimsIncludedInUserCreation != null && _basicAuthenticateOptions.ClaimsIncludedInUserCreation.Any())
            {
                var lstIndexesToRemove = openidClaims.Where(oc => !_basicAuthenticateOptions.ClaimsIncludedInUserCreation.Contains(oc.Type))
                                         .Select(oc => openidClaims.IndexOf(oc))
                                         .OrderByDescending(oc => oc);
                foreach (var index in lstIndexesToRemove)
                {
                    openidClaims.RemoveAt(index);
                }
            }

            var record = new AddUserParameter(openidClaims)
            {
                ExternalLogin = authenticatedUser.Claims.First(c => c.Type == ClaimTypes.NameIdentifier).Value
            };

            return(await _userActions.AddUser(record, authenticatedUser.Identity.AuthenticationType).ConfigureAwait(false));
        }