Example #1
0
        public async Task <Response <Models.User, Status> > AddUserAsync(Models.User user)
        {
            user.Email = user.Email?.ToLowerInvariant();

            _logger.LogTrace("Add user {0}", user.Email);

            // First, if anyone previously used this email, clear the claim that could
            // be used to revert that account back to this email address
            _context.Claims.RemoveRange(_context.Claims.Where(x =>
                                                              x.Type == PasswordlessLoginConstants.Security.PreviousEmailClaimType &&
                                                              x.Value == user.Email));

            var dbUser = new Entities.User()
            {
                SubjectId  = user.SubjectId ?? Guid.NewGuid().ToString("N"),
                Email      = user.Email,
                CreatedUTC = DateTime.UtcNow
            };

            dbUser.Claims = user.Claims?.Select(x => new Entities.UserClaim()
            {
                SubjectId = dbUser.SubjectId, Type = x.Type, Value = x.Value
            }).ToList();
            await _context.AddAsync(dbUser);

            var count = await _context.SaveChangesAsync();

            if (count == 0)
            {
                return(Response.Error <Models.User>(_localizer["Failed to add the user."]));
            }

            user.SubjectId = dbUser.SubjectId;
            return(Response.Success(user, _localizer["User saved."]));
        }
        public async Task <Response <Models.TrustedBrowser, Status> > AddTrustedBrowserAsync(string subjectId, string browserId, string description)
        {
            _logger.LogDebug("Adding trusted browser");

            if (string.IsNullOrEmpty(subjectId) || string.IsNullOrEmpty(browserId))
            {
                _logger.LogDebug("Subject id or browser id was missing.");
                return(Response.Error <Models.TrustedBrowser>(_localizer["Subject id or browser id was missing."]));
            }

            var dbBrowser = new TrustedBrowser()
            {
                SubjectId     = subjectId,
                BrowserIdHash = FastHashService.GetHash(browserId, subjectId),
                Description   = description?.Substring(0, description.Length),
                AddedOn       = DateTime.UtcNow,
            };

            await _context.AddAsync(dbBrowser);

            var count = await _context.SaveChangesAsync();

            if (count == 0)
            {
                return(Response.Error <Models.TrustedBrowser>(_localizer["Failed to save trusted browser."]));
            }

            return(Response.Success(dbBrowser.ToModel(), _localizer["Trusted browser saved."]));
        }
Example #3
0
        public async Task <Status> AddOneTimeCodeAsync(Models.OneTimeCode oneTimeCode)
        {
            _logger.LogTrace("Persisting one time code for {0}", oneTimeCode.SentTo);
            var record = oneTimeCode.ToEntity();
            await _context.AddAsync(record);

            var count = await _context.SaveChangesAsync();

            if (count == 0)
            {
                return(Status.Error(_localizer["One time code was not saved."]));
            }

            return(Status.Success(_localizer["One time code saved."]));
        }
Example #4
0
        public async Task <Status> AddPasswordHashAsync(string uniqueIdentifier, string passwordHash)
        {
            _logger.LogTrace("Add password hash");
            var record = new PasswordHash()
            {
                SubjectId          = uniqueIdentifier,
                Hash               = passwordHash,
                LastChangedUTC     = DateTime.UtcNow,
                FailedAttemptCount = 0,
                TempLockUntilUTC   = null
            };
            await _context.AddAsync(record);

            var count = await _context.SaveChangesAsync();

            if (count == 0)
            {
                return(Status.Error(_localizer["Failed to save the password."]));
            }

            return(Status.Success(_localizer["Password saved."]));
        }