示例#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));
        }
示例#2
0
        public async Task <ResourceOwner?> AuthenticateResourceOwner(
            string login,
            string password,
            CancellationToken cancellationToken)
        {
            var confirmationCode =
                await _confirmationCodeStore.Get(password, login, cancellationToken).ConfigureAwait(false);

            if (confirmationCode == null || confirmationCode.Subject != login)
            {
                return(null);
            }

            if (confirmationCode.IssueAt.AddSeconds(confirmationCode.ExpiresIn) <= DateTimeOffset.UtcNow)
            {
                return(null);
            }

            var resourceOwner = await _resourceOwnerRepository.GetResourceOwnerByClaim(
                OpenIdClaimTypes.PhoneNumber,
                login,
                cancellationToken)
                                .ConfigureAwait(false);

            if (resourceOwner != null)
            {
                await _confirmationCodeStore.Remove(password, resourceOwner.Subject !, cancellationToken)
                .ConfigureAwait(false);
            }

            return(resourceOwner);
        }
示例#3
0
        public async Task <ResourceOwner> AuthenticateResourceOwnerAsync(string login, string password)
        {
            if (string.IsNullOrWhiteSpace(login))
            {
                throw new ArgumentNullException(nameof(login));
            }

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

            var resourceOwner = await _resourceOwnerRepository.GetResourceOwnerByClaim(Core.Jwt.Constants.StandardResourceOwnerClaimNames.PhoneNumber, login).ConfigureAwait(false);

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

            var confirmationCode = await _confirmationCodeStore.Get(password).ConfigureAwait(false);

            if (confirmationCode == null || confirmationCode.Subject != resourceOwner.Claims.First(c => c.Type == Core.Jwt.Constants.StandardResourceOwnerClaimNames.PhoneNumber).Value)
            {
                return(null);
            }

            if (confirmationCode.IssueAt.AddSeconds(confirmationCode.ExpiresIn) <= DateTime.UtcNow)
            {
                return(null);
            }

            await _confirmationCodeStore.Remove(password).ConfigureAwait(false);

            return(resourceOwner);
        }
        public async Task <Option <ResourceOwner> > Execute(string phoneNumber, CancellationToken cancellationToken)
        {
            // 1. Send the confirmation code (SMS).
            var option = await _generateAndSendSmsCodeOperation.Execute(phoneNumber, cancellationToken).ConfigureAwait(false);

            if (option is Option <string> .Error e)
            {
                return(new Option <ResourceOwner> .Error(e.Details, e.State));
            }

            // 2. Try to get the resource owner.
            var resourceOwner = await _resourceOwnerRepository.GetResourceOwnerByClaim(
                OpenIdClaimTypes.PhoneNumber,
                phoneNumber,
                cancellationToken)
                                .ConfigureAwait(false);

            if (resourceOwner != null)
            {
                return(new Option <ResourceOwner> .Result(resourceOwner));
            }

            // 3. CreateJwk a new resource owner.
            var claims = new[]
            {
                new Claim(OpenIdClaimTypes.PhoneNumber, phoneNumber),
                new Claim(OpenIdClaimTypes.PhoneNumberVerified, "false")
            };
            var id = await _subjectBuilder.BuildSubject(claims, cancellationToken).ConfigureAwait(false);

            var record = new ResourceOwner {
                Subject = id, Password = Id.Create().ToSha256Hash(_salt), Claims = claims
            };

            // 3.2 Add user.
            await _addUser.Execute(record, cancellationToken).ConfigureAwait(false);

            //}

            var result = await _resourceOwnerRepository.GetResourceOwnerByClaim(
                OpenIdClaimTypes.PhoneNumber,
                phoneNumber,
                cancellationToken)
                         .ConfigureAwait(false);

            return(new Option <ResourceOwner> .Result(result !));
        }
        public Task <ResourceOwner> Execute(string claimKey, string claimValue)
        {
            if (string.IsNullOrWhiteSpace(claimKey))
            {
                throw new ArgumentNullException(nameof(claimKey));
            }

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

            return(_resourceOwnerRepository.GetResourceOwnerByClaim(claimKey, claimValue));
        }