Beispiel #1
0
        public void ItSetsTheAnonymousClientIdOnTheApplicationUser()
        {
            const string EXPECTED_TOKEN = "TEST";

            request.Headers.Add(ApiAuthenticationAttribute.AUTH_HEADER, new[] { EXPECTED_TOKEN });
            var expectedUser = new ApplicationUser
            {
                Id = "some id",
                CurrentGamingGroupId = 1
            };
            var expectedUserDeviceAuthToken = new UserDeviceAuthToken
            {
                ApplicationUser = expectedUser
            };

            authTokenValidatorMock.Expect(mock => mock.ValidateAuthToken(EXPECTED_TOKEN)).Return(expectedUser);
            const string EXPECTED_CLIENT_ID = "some client id";

            clientIdCalculatorMock.Expect(mock => mock.GetClientId(request, expectedUser)).Return(EXPECTED_CLIENT_ID);

            attribute.OnActionExecuting(actionContext);
            ApplicationUser actualUser = ((ApiControllerBase)actionContext.ControllerContext.Controller).CurrentUser;

            Assert.That(actualUser.AnonymousClientId, Is.EqualTo(EXPECTED_CLIENT_ID));
        }
Beispiel #2
0
        public void SetUp()
        {
            _autoMocker = new RhinoAutoMocker <AuthTokenGenerator>();
            _autoMocker.PartialMockTheClassUnderTest();

            IAppSettings appSettingsMock = MockRepository.GenerateMock <IAppSettings>();

            appSettingsMock.Expect(mock => mock[AuthTokenGenerator.APP_KEY_AUTH_TOKEN_SALT]).Return(_expectedSalt);

            _autoMocker.Get <IConfigurationManager>().Expect(mock => mock.AppSettings).Return(appSettingsMock);
            _autoMocker.ClassUnderTest.Expect(mock => mock.GenerateNewAuthToken()).Return(_expectedAuthToken);
            _autoMocker.ClassUnderTest.Expect(mock => mock.HashAuthToken(_expectedAuthToken))
            .Return(_expectedSaltedHashedAuthToken);

            _applicationUser = new ApplicationUser
            {
                Id = ApplicationUserId
            };

            _autoMocker.Get <IDataContext>().Expect(mock => mock.FindById <ApplicationUser>(Arg <string> .Is.Anything)).Return(_applicationUser);

            _userDeviceAuthTokenWithNoDeviceId = new UserDeviceAuthToken
            {
                Id = 0,
                ApplicationUserId = ApplicationUserId,
                DeviceId          = null
            };
            _userDeviceAuthTokenThatDoesntExpire = new UserDeviceAuthToken
            {
                Id = 1,
                ApplicationUserId = ApplicationUserId
            };
            _userDeviceAuthTokenThatExpiresInTheFuture = new UserDeviceAuthToken
            {
                Id = 2,
                ApplicationUserId = ApplicationUserId,
                DeviceId          = "device id for future expiration",
                AuthenticationTokenExpirationDate = DateTime.UtcNow.AddDays(1)
            };
            _userDeviceAuthTokenThatExpiresInThePast = new UserDeviceAuthToken
            {
                Id = 3,
                ApplicationUserId = ApplicationUserId,
                DeviceId          = "device id for already expired",
                AuthenticationTokenExpirationDate = DateTime.UtcNow.AddDays(-1)
            };
            var authTokens = new List <UserDeviceAuthToken>
            {
                _userDeviceAuthTokenWithNoDeviceId,
                _userDeviceAuthTokenThatDoesntExpire,
                _userDeviceAuthTokenThatExpiresInTheFuture,
                _userDeviceAuthTokenThatExpiresInThePast,
                new UserDeviceAuthToken
                {
                    ApplicationUserId = "some other applicationUserId"
                }
            }.AsQueryable();

            _autoMocker.Get <IDataContext>().Expect(mock => mock.GetQueryable <UserDeviceAuthToken>()).Return(authTokens);
        }
Beispiel #3
0
        public AuthToken GenerateAuthToken(string applicationUserId, string uniqueDeviceId = null)
        {
            var newAuthTokenString = GenerateNewAuthToken();
            var saltedHash         = HashAuthToken(newAuthTokenString);

            var applicationUser = dataContext.FindById <ApplicationUser>(applicationUserId);

            var userDeviceAuthToken = dataContext.GetQueryable <UserDeviceAuthToken>()
                                      .FirstOrDefault(x => x.ApplicationUserId == applicationUserId && x.DeviceId == uniqueDeviceId);

            if (userDeviceAuthToken == null)
            {
                userDeviceAuthToken = new UserDeviceAuthToken
                {
                    ApplicationUserId = applicationUserId,
                    DeviceId          = uniqueDeviceId
                };
            }

            userDeviceAuthToken.AuthenticationToken = saltedHash;
            userDeviceAuthToken.AuthenticationTokenExpirationDate = DateTime.UtcNow.AddMonths(3);

            dataContext.Save(userDeviceAuthToken, applicationUser);

            return(new AuthToken(newAuthTokenString, userDeviceAuthToken.AuthenticationTokenExpirationDate));
        }
        public AuthToken GenerateAuthToken(string applicationUserId, string uniqueDeviceId = null)
        {
            var newAuthTokenString = GenerateNewAuthToken();
            var saltedHash = HashAuthToken(newAuthTokenString);

            var applicationUser = dataContext.FindById<ApplicationUser>(applicationUserId);

            var userDeviceAuthToken = dataContext.GetQueryable<UserDeviceAuthToken>()
                .FirstOrDefault(x => x.ApplicationUserId == applicationUserId && x.DeviceId == uniqueDeviceId);

            if (userDeviceAuthToken == null)
            {
                userDeviceAuthToken = new UserDeviceAuthToken
                {
                    ApplicationUserId = applicationUserId,
                    DeviceId = uniqueDeviceId
                };
            }

            userDeviceAuthToken.AuthenticationToken = saltedHash;
            userDeviceAuthToken.AuthenticationTokenExpirationDate = DateTime.UtcNow.AddMonths(3);

            dataContext.Save(userDeviceAuthToken, applicationUser);

            return new AuthToken(newAuthTokenString, userDeviceAuthToken.AuthenticationTokenExpirationDate);
        }
        public void SetUp()
        {
            _autoMocker = new RhinoAutoMocker<AuthTokenGenerator>();
            _autoMocker.PartialMockTheClassUnderTest();

            IAppSettings appSettingsMock = MockRepository.GenerateMock<IAppSettings>();
            appSettingsMock.Expect(mock => mock[AuthTokenGenerator.APP_KEY_AUTH_TOKEN_SALT]).Return(_expectedSalt);

            _autoMocker.Get<IConfigurationManager>().Expect(mock => mock.AppSettings).Return(appSettingsMock);
            _autoMocker.ClassUnderTest.Expect(mock => mock.GenerateNewAuthToken()).Return(_expectedAuthToken);
            _autoMocker.ClassUnderTest.Expect(mock => mock.HashAuthToken(_expectedAuthToken))
                      .Return(_expectedSaltedHashedAuthToken);

            _applicationUser = new ApplicationUser
            {
                Id = ApplicationUserId
            };

            _autoMocker.Get<IDataContext>().Expect(mock => mock.FindById<ApplicationUser>(Arg<string>.Is.Anything)).Return(_applicationUser);

            _userDeviceAuthTokenWithNoDeviceId = new UserDeviceAuthToken
            {
                Id = 0,
                ApplicationUserId = ApplicationUserId,
                DeviceId = null
            };
            _userDeviceAuthTokenThatDoesntExpire = new UserDeviceAuthToken
            {
                Id = 1,
                ApplicationUserId = ApplicationUserId
            };
            _userDeviceAuthTokenThatExpiresInTheFuture = new UserDeviceAuthToken
            {
                Id = 2,
                ApplicationUserId = ApplicationUserId,
                DeviceId = "device id for future expiration",
                AuthenticationTokenExpirationDate = DateTime.UtcNow.AddDays(1)
            };
            _userDeviceAuthTokenThatExpiresInThePast = new UserDeviceAuthToken
            {
                Id = 3,
                ApplicationUserId = ApplicationUserId,
                DeviceId = "device id for already expired",
                AuthenticationTokenExpirationDate = DateTime.UtcNow.AddDays(-1)
            };
            var authTokens = new List<UserDeviceAuthToken>
            {
                _userDeviceAuthTokenWithNoDeviceId,
                _userDeviceAuthTokenThatDoesntExpire,
                _userDeviceAuthTokenThatExpiresInTheFuture,
                _userDeviceAuthTokenThatExpiresInThePast,
                new UserDeviceAuthToken
                {
                    ApplicationUserId = "some other applicationUserId"
                }
            }.AsQueryable();
            _autoMocker.Get<IDataContext>().Expect(mock => mock.GetQueryable<UserDeviceAuthToken>()).Return(authTokens);
        }
Beispiel #6
0
        public void SetUp()
        {
            autoMocker = new RhinoAutoMocker <AuthTokenValidator>();

            const string EXPECTED_HASHED_AND_SALTED_AUTH_TOKEN = "some hashed and salted auth token";

            autoMocker.Get <IAuthTokenGenerator>().Expect(mock => mock.HashAuthToken(this.validAuthToken)).Return(
                EXPECTED_HASHED_AND_SALTED_AUTH_TOKEN);

            _expectedUserDeviceAuthTokenThatIsntExpired = new UserDeviceAuthToken()
            {
                AuthenticationToken = EXPECTED_HASHED_AND_SALTED_AUTH_TOKEN,
                AuthenticationTokenExpirationDate = DateTime.UtcNow.AddDays(3)
            };

            _applicationUserWithValidAuthToken = new ApplicationUser
            {
                UserDeviceAuthTokens = new List <UserDeviceAuthToken>
                {
                    _expectedUserDeviceAuthTokenThatIsntExpired
                }
            };

            const string EXPECTED_HASHED_AND_SALTED_AUTH_TOKEN_THAT_IS_EXPIRED = "some hashed and salted auth token that is expired";

            autoMocker.Get <IAuthTokenGenerator>().Expect(mock => mock.HashAuthToken(this.expiredAuthToken)).Return(
                EXPECTED_HASHED_AND_SALTED_AUTH_TOKEN_THAT_IS_EXPIRED);

            _expectedUserDeviceAuthTokenThatIsExpired = new UserDeviceAuthToken()
            {
                AuthenticationToken = EXPECTED_HASHED_AND_SALTED_AUTH_TOKEN,
                AuthenticationTokenExpirationDate = DateTime.UtcNow.AddDays(-1)
            };

            var applicationUserWithExpiredAuthToken = new ApplicationUser
            {
                UserDeviceAuthTokens = new List <UserDeviceAuthToken>
                {
                    _expectedUserDeviceAuthTokenThatIsExpired
                }
            };

            var applicationUsersQueryable = new List <ApplicationUser>
            {
                _applicationUserWithValidAuthToken,
                applicationUserWithExpiredAuthToken
            }.AsQueryable();

            autoMocker.Get <IDataContext>().Expect(mock => mock.GetQueryable <ApplicationUser>()).Return(applicationUsersQueryable);
        }
        public void SetUp()
        {
            autoMocker = new RhinoAutoMocker<AuthTokenValidator>();

            const string EXPECTED_HASHED_AND_SALTED_AUTH_TOKEN = "some hashed and salted auth token";
            autoMocker.Get<IAuthTokenGenerator>().Expect(mock => mock.HashAuthToken(this.validAuthToken)).Return(
                EXPECTED_HASHED_AND_SALTED_AUTH_TOKEN);

            _expectedUserDeviceAuthTokenThatIsntExpired = new UserDeviceAuthToken()
            {
                AuthenticationToken = EXPECTED_HASHED_AND_SALTED_AUTH_TOKEN,
                AuthenticationTokenExpirationDate = DateTime.UtcNow.AddDays(3)
            };

            _applicationUserWithValidAuthToken = new ApplicationUser
            {
                UserDeviceAuthTokens = new List<UserDeviceAuthToken>
                {
                    _expectedUserDeviceAuthTokenThatIsntExpired
                }
            };

            const string EXPECTED_HASHED_AND_SALTED_AUTH_TOKEN_THAT_IS_EXPIRED = "some hashed and salted auth token that is expired";
            autoMocker.Get<IAuthTokenGenerator>().Expect(mock => mock.HashAuthToken(this.expiredAuthToken)).Return(
                EXPECTED_HASHED_AND_SALTED_AUTH_TOKEN_THAT_IS_EXPIRED);

            _expectedUserDeviceAuthTokenThatIsExpired = new UserDeviceAuthToken()
            {
                AuthenticationToken = EXPECTED_HASHED_AND_SALTED_AUTH_TOKEN,
                AuthenticationTokenExpirationDate = DateTime.UtcNow.AddDays(-1)
            };

            var applicationUserWithExpiredAuthToken = new ApplicationUser
            {
                UserDeviceAuthTokens = new List<UserDeviceAuthToken>
                {
                    _expectedUserDeviceAuthTokenThatIsExpired
                }
            };

            var applicationUsersQueryable = new List<ApplicationUser>
            {
                _applicationUserWithValidAuthToken,
                applicationUserWithExpiredAuthToken
            }.AsQueryable();

            autoMocker.Get<IDataContext>().Expect(mock => mock.GetQueryable<ApplicationUser>()).Return(applicationUsersQueryable);
        }
        public void ItSetsTheAnonymousClientIdOnTheApplicationUser()
        {
            const string EXPECTED_TOKEN = "TEST";
            request.Headers.Add(ApiAuthenticationAttribute.AUTH_HEADER, new[] { EXPECTED_TOKEN });
            var expectedUser = new ApplicationUser
            {
                Id = "some id",
                CurrentGamingGroupId = 1
            };
            var expectedUserDeviceAuthToken = new UserDeviceAuthToken
            {
                ApplicationUser = expectedUser
            };
            authTokenValidatorMock.Expect(mock => mock.ValidateAuthToken(EXPECTED_TOKEN)).Return(expectedUser);
            const string EXPECTED_CLIENT_ID = "some client id";
            clientIdCalculatorMock.Expect(mock => mock.GetClientId(request, expectedUser)).Return(EXPECTED_CLIENT_ID);

            attribute.OnActionExecuting(actionContext);
            ApplicationUser actualUser = ((ApiControllerBase)actionContext.ControllerContext.Controller).CurrentUser;

            Assert.That(actualUser.AnonymousClientId, Is.EqualTo(EXPECTED_CLIENT_ID));
        }