Beispiel #1
0
        /// <summary>
        ///     Determines whether a given set of resource owner credentials is valid based on the authorization server's user
        ///     database
        ///     and if so records an authorization entry such that subsequent calls to <see cref="IsAuthorizationValid" /> would
        ///     return <c>true</c>.
        /// </summary>
        /// <param name="userName">Username on the account.</param>
        /// <param name="password">The user's password.</param>
        /// <param name="accessRequest">
        ///     The access request the credentials came with.
        ///     This may be useful if the authorization server wishes to apply some policy based on the client that is making the
        ///     request.
        /// </param>
        /// <returns>A value that describes the result of the authorization check.</returns>
        /// <exception cref="NotSupportedException">
        ///     May be thrown if the authorization server does not support the resource owner password credential grant type.
        /// </exception>
        public AutomatedUserAuthorizationCheckResponse CheckAuthorizeResourceOwnerCredentialGrant(string userName,
                                                                                                  string password, IAccessTokenRequest accessRequest)
        {
            Guard.NotNullOrEmpty(() => userName, userName);
            Guard.NotNullOrEmpty(() => password, password);
            Guard.NotNull(() => accessRequest, accessRequest);

            bool approved = false;

            //Ensure client exists
            if (IsClientExist(accessRequest.ClientIdentifier))
            {
                // Ensure user exists
                IUserAuthInfo user = UserAccountStore.GetUserAuthInfo(userName);
                if (user != null)
                {
                    if (IsValidScope(accessRequest))
                    {
                        if (user.VerifyPassword(password))
                        {
                            approved = true;

                            //TODO: audit the passed authentication
                        }

                        //TODO: audit the failed authentication
                    }
                }
            }

            return(new AutomatedUserAuthorizationCheckResponse(accessRequest, approved, (approved) ? userName : null));
        }
Beispiel #2
0
        public void LoadByIdAsync_WhenUserAccountExists_ExpectUserAccountRetured(DbContextOptions <UserAccountDbContext> options)
        {
            var testUserAccount = new UserAccount
            {
                Id    = Guid.NewGuid(),
                Email = "*****@*****.**"
            };

            using (var context = new UserAccountDbContext(options, StoreOptions))
            {
                context.UserAccounts.Add(testUserAccount.ToEntity());
                context.SaveChanges();
            }

            UserAccount userAccount;

            using (var context = new UserAccountDbContext(options, StoreOptions))
            {
                var store = new UserAccountStore(context, NullLogger <UserAccountStore> .Create());

                userAccount = store.LoadByIdAsync(testUserAccount.Id).Result;
            }

            Assert.NotNull(userAccount);
        }
        public void LoadByEmailWithExternalAsync_WhenUserAccountExists_ExpectUserAccountRetured(
            DbContextOptions <UserAccountDbContext> options)
        {
            var testUserAccount = new UserAccount
            {
                Id       = Guid.NewGuid(),
                Email    = "*****@*****.**",
                Accounts = new List <ExternalAccount>
                {
                    new ExternalAccount
                    {
                        Provider = "provider",
                        Email    = "*****@*****.**",
                        Subject  = "123456789"
                    },
                    new ExternalAccount
                    {
                        Provider = "provider",
                        Email    = "*****@*****.**",
                        Subject  = "asda5sd4a564da6"
                    }
                }
            };

            using (var context =
                       new UserAccountDbContext(options, StoreOptions))
            {
                context.UserAccounts.Add(testUserAccount.ToEntity());
                context.SaveChanges();
            }

            UserAccount userAccount;

            using (var context =
                       new UserAccountDbContext(options, StoreOptions))
            {
                var store = new UserAccountStore(
                    context,
                    NullLogger <UserAccountStore> .Create()
                    );

                userAccount = store
                              .LoadByEmailWithExternalAsync(testUserAccount.Email)
                              .Result;
            }

            Assert.NotNull(userAccount);
        }
        public void LoadByExternalProviderAsync_WhenUserAccountExists_ExpectUserAccountRetured(
            DbContextOptions <UserAccountDbContext> options)
        {
            var testExternalAccount = new ExternalAccount
            {
                Email    = "*****@*****.**",
                Provider = "yahoo",
                Subject  = "123456789"
            };

            var testUserAccount = new UserAccount
            {
                Id       = Guid.NewGuid(),
                Email    = "*****@*****.**",
                Accounts = new List <ExternalAccount>
                {
                    testExternalAccount
                }
            };

            using (var context =
                       new UserAccountDbContext(options, StoreOptions))
            {
                context.UserAccounts.Add(testUserAccount.ToEntity());
                context.SaveChanges();
            }

            UserAccount userAccount;

            using (var context =
                       new UserAccountDbContext(options, StoreOptions))
            {
                var store = new UserAccountStore(
                    context,
                    NullLogger <UserAccountStore> .Create()
                    );

                userAccount = store.LoadByExternalProviderAsync(
                    testExternalAccount.Provider,
                    testExternalAccount.Subject).Result;
            }

            Assert.NotNull(userAccount);
        }
        public void WriteAsync_NewUserAccount_ExpectUserAccountRetured(
            DbContextOptions <UserAccountDbContext> options)
        {
            var testUserAccount1 = new UserAccount
            {
                Id       = Guid.NewGuid(),
                Email    = "foo2@localhost",
                Accounts = new List <ExternalAccount>
                {
                    new ExternalAccount
                    {
                        Email    = "foo2@provider",
                        Provider = "facebook",
                        Subject  = "123456712",
                    },
                    new ExternalAccount
                    {
                        Email    = "bar2@provider",
                        Provider = "google",
                        Subject  = "789456111",
                    }
                },
                Claims = new List <UserAccountClaim>
                {
                    new UserAccountClaim("name", "foo2"),
                    new UserAccountClaim("email", "foo2@localhost"),
                }
            };

            using (var context =
                       new UserAccountDbContext(options, StoreOptions))
            {
                var store = new UserAccountStore(
                    context,
                    NullLogger <UserAccountStore> .Create()
                    );

                var userAccount = store.WriteAsync(testUserAccount1).Result;
                Assert.NotNull(userAccount);
            }
        }
Beispiel #6
0
        public void WriteAsync_UpdateUserAccount_ExpectUserAccountRetured(DbContextOptions <UserAccountDbContext> options)
        {
            var testUserAccount1 = new UserAccount
            {
                Id       = Guid.NewGuid(),
                Email    = "foox@localhost",
                Accounts = new List <ExternalAccount>
                {
                    new ExternalAccount
                    {
                        Email    = "foox@provider",
                        Provider = "facebook",
                        Subject  = "123456789",
                    },
                    new ExternalAccount
                    {
                        Email    = "bar@provider",
                        Provider = "google",
                        Subject  = "789456123",
                    }
                },
                Claims = new List <UserAccountClaim>
                {
                    new UserAccountClaim("name", "foo"),
                    new UserAccountClaim("email", "foo@localhost"),
                    new UserAccountClaim("w00t", "some junk"),
                }
            };

            using (var context = new UserAccountDbContext(options, StoreOptions))
            {
                context.UserAccounts.Add(testUserAccount1.ToEntity());
                context.SaveChanges();
                testUserAccount1 = context.UserAccounts.AsNoTracking()
                                   .FirstOrDefault(c => c.Id == testUserAccount1.Id).ToModel();
            }

            UserAccount userAccount;

            using (var context = new UserAccountDbContext(options, StoreOptions))
            {
                var store = new UserAccountStore(context, NullLogger <UserAccountStore> .Create());

                testUserAccount1.VerificationKeySentAt = DateTime.Now;
                testUserAccount1.VerificationPurpose   = 1;
                testUserAccount1.VerificationStorage   = "hallo welt";

                userAccount = store.WriteAsync(testUserAccount1).Result;

                Assert.NotNull(userAccount);
            }

            using (var context = new UserAccountDbContext(options, StoreOptions))
            {
                var updatedAccount = testUserAccount1 = context.UserAccounts
                                                        .Include(c => c.Accounts)
                                                        .Include(c => c.Claims)
                                                        .FirstOrDefault(c => c.Id == testUserAccount1.Id).ToModel();

                Assert.NotNull(updatedAccount);
                Assert.Equal(updatedAccount.VerificationStorage, userAccount.VerificationStorage);
                Assert.Equal(2, updatedAccount.Accounts.Count());
                Assert.Equal(3, updatedAccount.Claims.Count());
            }
        }
Beispiel #7
0
 private IUserAuthInfo GetUserAuthInfo(string username)
 {
     return(UserAccountStore.GetUserAuthInfo(username));
 }