예제 #1
0
 public void Setup() {
     this.Valid = new AccessTokenModel() {
         Account = new AccountModel() {
             PasswordHash = "Password Hash"
         }
     };
 }
예제 #2
0
        public void TestNullTokenReturnedWhenAccountEmpty() {
            AccessTokenModel accessToken = new AccessTokenModel();

            var token = accessToken.Generate(TestGenerate.Identifer);

            Assert.IsNull(token);
        }
        public void TestCannotAuthenticateAgainstDifferentTokenId() {
            const string identifier = "192.168.1.1";

            AccessTokenModel accessToken = new AccessTokenModel() {
                Account = new AccountModel() {
                    Username = "******",
                    PasswordHash = "MyPasswordHash"
                }
            };

            var token = accessToken.Generate(identifier);

            var security = new SecurityController();
            security.Tunnel(CommandBuilder.SecurityAddGroup("GroupName").SetOrigin(CommandOrigin.Local));
            security.Tunnel(CommandBuilder.SecurityGroupAddAccount("GroupName", "Phogue").SetOrigin(CommandOrigin.Local));
            security.Tunnel(CommandBuilder.SecurityAccountSetPasswordHash("Phogue", "MyPasswordHash").SetOrigin(CommandOrigin.Local));

            // Now append the token onto the account.
            security.Tunnel(CommandBuilder.SecurityAccountAppendAccessToken("Phogue", accessToken.Id, accessToken.TokenHash, accessToken.LastTouched).SetOrigin(CommandOrigin.Local));

            // Now validate that we can authenticate against the newly appended token hash
            ICommandResult result = security.Tunnel(CommandBuilder.SecurityAccountAuthenticateToken(Guid.NewGuid(), token, identifier).SetOrigin(CommandOrigin.Local));

            Assert.IsFalse(result.Success);
            Assert.AreEqual(CommandResultType.Failed, result.CommandResultType);
        }
예제 #4
0
        public void TestNullTokenReturnedWhenIdentiferEmpty() {
            AccessTokenModel accessToken = new AccessTokenModel() {
                Account = new AccountModel() {
                    PasswordHash = "password"
                }
            };

            var token = accessToken.Generate("");

            Assert.IsNull(token);
        }
예제 #5
0
        public void TestNullTokenReturnedWhenAccountHashNull() {
            AccessTokenModel accessToken = new AccessTokenModel() {
                Account = new AccountModel() {
                    PasswordHash = null
                }
            };

            var token = accessToken.Generate(TestGenerate.Identifer);

            Assert.IsNull(token);
        }
예제 #6
0
        public void TestAllValuesDefaulted() {
            AccessTokenModel accessToken = new AccessTokenModel() {
                Account = new AccountModel(),
                TokenHash = "Something",
                LastTouched = DateTime.Now,
                Id = Guid.NewGuid()
            };

            accessToken.Dispose();

            Assert.IsNull(accessToken.Account);
            Assert.IsNull(accessToken.TokenHash);
            Assert.AreEqual(Guid.Empty, accessToken.Id);
        }
예제 #7
0
        /// <summary>
        /// Helper for creating an access token.
        /// </summary>
        /// <param name="account">The account to create the access token for</param>
        /// <param name="identifier">The identifying peice of information to mixin with the token</param>
        /// <returns>An access token for transport, or null if the user can't have tokens or something went wrong while making the token.</returns>
        protected AccessTokenTransportModel GenerateAccessToken(AccountModel account, String identifier) {
            AccessTokenTransportModel accessTokenTransport = null;

            var accessToken = new AccessTokenModel() {
                Account = account,
                ExpiredWindowSeconds = this.Shared.Variables.Get(CommonVariableNames.SecurityMaximumAccessTokenLastTouchedLengthSeconds, 172800)
            };

            var token = accessToken.Generate(identifier);

            if (String.IsNullOrEmpty(token) == false) {
                // Save the token hash for future authentication.
                this.Tunnel(CommandBuilder.SecurityAccountAppendAccessToken(account.Username, accessToken.Id, accessToken.TokenHash, accessToken.LastTouched).SetOrigin(CommandOrigin.Local));

                accessTokenTransport = new AccessTokenTransportModel() {
                    Id = accessToken.Id,
                    Token = token
                };
            }

            return accessTokenTransport;
        }
예제 #8
0
        public void TestGoodTokenReturnedWhenAllCredentialsPassed() {
            AccessTokenModel accessToken = new AccessTokenModel() {
                Account = new AccountModel() {
                    PasswordHash = "password"
                }
            };

            var token = accessToken.Generate("192.168.1.1");

            Assert.IsNotNull(token);
        }
예제 #9
0
        public void TestLastTouchedResetToCurrentDateTime() {
            AccessTokenModel accessToken = new AccessTokenModel() {
                Account = new AccountModel() {
                    PasswordHash = "password"
                },
                LastTouched = DateTime.Now.AddDays(-1)
            };

            var token = accessToken.Generate("192.168.1.1");

            Assert.IsNotNull(token);
            Assert.GreaterOrEqual(accessToken.LastTouched, DateTime.Now.AddMinutes(-1));
        }
예제 #10
0
        public void TestReturnedTokenIsRandom() {
            AccessTokenModel accessToken = new AccessTokenModel() {
                Account = new AccountModel() {
                    PasswordHash = "password"
                }
            };

            var tokenA = accessToken.Generate("192.168.1.1");
            var tokenB = accessToken.Generate("192.168.1.1");

            Assert.IsNotNull(tokenA);
            Assert.IsNotNull(tokenB);
            Assert.AreNotEqual(tokenA, tokenB);
        }