Esempio n. 1
0
 public async Task<User> CreateUser(User user)
 {
     if (user == null) throw new SimpleIdentityDataException("User object is missing");
     if (user.Name == null || user.Name.Length == 0) throw new SimpleIdentityDataException("User.Name is missing");
     if (user.Email == null || user.Email.Length == 0) throw new SimpleIdentityDataException("User.Email is missing");
     user.Authentication = new UserAuthentication { User = user, Secret = Membership.GeneratePassword(8, 0) };
     AuthorizationManager auth = new AuthorizationManager(_dbContext);
     _dbContext.Users.Add(user);
     await _dbContext.SaveChangesAsync();
     await auth.GrantAuthorizationToUser(Authorization.AuthResourceType.User, user.Id, Authorization.AuthScopes.Full, user);
     await _dbContext.SaveChangesAsync();
     return user;
 }
Esempio n. 2
0
        public async Task<User> UpdateUser(User user)
        {
            if (user == null) throw new SimpleIdentityDataException("User object is missing");
            if (user.Name == null || user.Name.Length == 0) throw new SimpleIdentityDataException("User.Name is missing");
            if (user.Email == null || user.Email.Length == 0) throw new SimpleIdentityDataException("User.Email is missing");

            User userToUpdate = await FindUser(user.Id);

            if (userToUpdate.Name != user.Name) throw new SimpleIdentityDataException("Editing User.Name is not supported");
            userToUpdate.Email = user.Email;
            userToUpdate.IsActive = user.IsActive;

            await _dbContext.SaveChangesAsync();
            return userToUpdate;
        }
Esempio n. 3
0
        public async Task<User> FindUserByToken(string accessToken)
        {
            if (_tokenCache != accessToken)
            {
                var q = await _dbContext.Tokens
                    .Where(b => b.AccessToken == accessToken &&
                        b.TokenTimeout > DateTime.Now &&
                        !b.IsRevoked &&
                        b.User.IsActive)
                    .Select(b => b.User).ToListAsync();
                if (!q.Any()) throw new SimpleIdentityDataNotFoundException("User with Token [" + accessToken + "] cannot be found");

                _tokenCache = accessToken;
                _userCache = q.First();
            }
            return _userCache;
        }
        public async Task<Authorization> GrantAuthorizationToUser(
            Authorization.AuthResourceType resourceType,
            int resourceId,
            Authorization.AuthScopes scopes,
            User user)
        {
            Authorization auth = new Authorization
            {
                ResourceType = resourceType,
                ResourceId = resourceId,
                Scopes = scopes,
                User = user,
                GrantDate = DateTime.Now
            };
            _dbContext.Authorizations.Add(auth);
            await _dbContext.SaveChangesAsync();

            return auth;
        }
        public async void ReturnUser_WhenSucessful()
        {
            var mockDbContext = new MockSimpleIdentityDbContext();
            UserManager userManager = new UserManager(mockDbContext);
            AuthorizationManager authManager = new AuthorizationManager(mockDbContext);

            User user = new User();
            user.Name = "foo";
            user.Email = "*****@*****.**";
            user.IsActive = true;
            user.Id = 1;

            var createdUser = await userManager.CreateUser(user);

            Assert.True(createdUser.Name == user.Name);
            Assert.True(createdUser.Email == user.Email);
            Assert.True(createdUser.Authentication.Secret.Length > 0);
            Assert.True(await authManager.IsUserAuthorized(1, Authorization.AuthResourceType.User, 1, Authorization.AuthScopes.Full));

        }
        public async void ReturnUser_WhenSuccessful()
        {
            var mockDbContext = new MockSimpleIdentityDbContext(MockData.Seed());
            UserManager userManager = new UserManager(mockDbContext);

            var rand = new Random(Guid.NewGuid().GetHashCode());
            User user = mockDbContext.Data.Users[rand.Next(0, mockDbContext.Data.Users.Count - 1)];
            User modifiedUser = new User();
            modifiedUser.Id = user.Id;
            modifiedUser.IsActive = user.IsActive;
            modifiedUser.Email = "*****@*****.**";
            modifiedUser.Name = user.Name;

            user = await userManager.UpdateUser(modifiedUser);

            Assert.Equal(1, mockDbContext.SaveCount);
            Assert.True(user.Id == modifiedUser.Id);
            Assert.True(user.IsActive == modifiedUser.IsActive);
            Assert.True(user.Email == modifiedUser.Email);
            Assert.True(user.Name == modifiedUser.Name);
        }
        public async void ThrowException_WhenMandatoryElementIsMissing()
        {
            Exception ex = null;
            try
            {
                var mockDbContext = new MockSimpleIdentityDbContext(MockData.Seed());
                UserManager userManager = new UserManager(mockDbContext);

                var rand = new Random(Guid.NewGuid().GetHashCode());
                User user = mockDbContext.Data.Users[rand.Next(0, mockDbContext.Data.Users.Count - 1)];
                User modifiedUser = new User();
                modifiedUser.Id = user.Id;
                modifiedUser.IsActive = user.IsActive;
                modifiedUser.Name = user.Name;

                await userManager.UpdateUser(modifiedUser);
            }
            catch (SimpleIdentityDataException e) { ex = e; }

            Assert.True(ex != null);
        }
Esempio n. 8
0
        private async Task<Token> GenerateTokenInternal(User user, Client client, bool requireSignature)
        {
            Token token = new Token
            {
                AccessToken = Convert.ToBase64String(Guid.NewGuid().ToByteArray()),
                RefreshToken = Convert.ToBase64String(Guid.NewGuid().ToByteArray()),
                TokenTimeout = DateTime.Now.AddSeconds(900),
                User = user,
                Client = client,
                RequireSignature = requireSignature
            };
            _dbContext.Tokens.Add(token);
            await _dbContext.SaveChangesAsync();

            return token;
        }