Esempio n. 1
0
        public async Task <Guid> SendMessageAsync(
            string country,
            string phone,
            string ipAddress,
            string userAgent,
            CancellationToken ct)
        {
            var now           = DateTime.UtcNow;
            var code          = new Random().Next(0, 9999).ToString("0000");
            var identityTypes = new[] { IdentityType.PhoneAndPassword };
            var identity      = await _identitiesService.GetByKeyAndTypesAsync(phone, identityTypes, ct);

            var token = new IdentityToken
            {
                IdentityId         = identity.Id,
                Type               = IdentityTokenType.PhoneValidation,
                Value              = code,
                CreateDateTime     = now,
                ExpirationDateTime = now.AddMinutes(10),
                IpAddress          = ipAddress,
                UserAgent          = userAgent
            };

            var id = await _identityTokensService.CreateAsync(token, ct);

            var phoneWithPrefix = country.GetInternationalPhonePrefix() + phone;

            await SendCodeAsync(phoneWithPrefix, code);

            return(id);
        }
Esempio n. 2
0
        public async Task <Guid> SendMessageAsync(string key, string ipAddress, string userAgent, CancellationToken ct)
        {
            var now           = DateTime.UtcNow;
            var code          = Generator.GenerateAlphaNumericString(256);
            var identityTypes = IdentityTypeExtensions.TypesWithPassword;
            var identity      = await _identitiesService.GetByKeyAndTypesAsync(key, identityTypes, ct);

            var emailIdentity = await _identitiesService.GetByProfileIdAndTypeAsync(identity.ProfileId,
                                                                                    IdentityType.EmailAndPassword, ct);

            var token = new IdentityToken
            {
                IdentityId         = emailIdentity.Id,
                Type               = IdentityTokenType.PasswordReset,
                Value              = code,
                CreateDateTime     = now,
                ExpirationDateTime = now.AddDays(1),
                IpAddress          = ipAddress,
                UserAgent          = userAgent
            };

            var id = await _identityTokensService.CreateAsync(token, ct);

            await SendCodeAsync(emailIdentity.Key, id, code);

            return(id);
        }
Esempio n. 3
0
        public async Task <PostChangePhoneResponse> ChangeAsync(
            string country,
            string oldPhone,
            string newPhone,
            string password,
            string ipAddress,
            string userAgent,
            CancellationToken ct)
        {
            var identityTypes = new[] { IdentityType.PhoneAndPassword };
            var identity      = await _identitiesService.GetByKeyAndTypesAsync(oldPhone, identityTypes, ct);

            if (identity == null)
            {
                return(new PostChangePhoneResponse(true));
            }

            var profile = await _profilesService.GetAsync(identity.ProfileId, ct);

            if (profile == null)
            {
                return(new PostChangePhoneResponse(true));
            }

            var isPasswordCorrect = _identitiesService.IsPasswordCorrect(identity, password);

            if (!isPasswordCorrect)
            {
                return(new PostChangePhoneResponse(true));
            }

            var newIdentity = new Identities.Models.Identity
            {
                Key = newPhone
            };

            await _identitiesService.UpdateAsync(identity, newIdentity, ct);

            var tokenId = await _phoneConfirmationService.SendMessageAsync(country, newPhone, ipAddress, userAgent, ct);

            return(new PostChangePhoneResponse(tokenId));
        }
Esempio n. 4
0
        public async Task <Guid> SendMessageAsync(string email, string ipAddress, string userAgent, CancellationToken ct)
        {
            var now           = DateTime.UtcNow;
            var code          = Generator.GenerateAlphaNumericString(256);
            var identityTypes = new[] { IdentityType.EmailAndPassword };
            var identity      = await _identitiesService.GetByKeyAndTypesAsync(email, identityTypes, ct);

            var token = new IdentityToken
            {
                IdentityId         = identity.Id,
                Type               = IdentityTokenType.EmailValidation,
                Value              = code,
                CreateDateTime     = now,
                ExpirationDateTime = now.AddDays(1),
                IpAddress          = ipAddress,
                UserAgent          = userAgent
            };

            var id = await _identityTokensService.CreateAsync(token, ct);

            await SendCodeAsync(email, id, code);

            return(id);
        }