示例#1
0
        /// <summary>
        /// Create a WebServiceContext instance.
        /// </summary>
        /// <param name='clientInformation'>Client information.</param>
        public WebServiceContext(WebClientInformation clientInformation)
        {
            // Init context.
            _database    = null;
            _clientToken = new WebClientToken(clientInformation.Token,
                                              WebServiceData.WebServiceManager.Key);
            _locale = clientInformation.Locale;
            if (_locale.IsNull())
            {
                _locale = GetDefaultLocale();
            }
            _currentRole = clientInformation.Role;
            _requestId   = GetNextRequestId();
            if (_isTracing)
            {
                _traceStart = DateTime.Now;
            }
            _transactionTimeout = Settings.Default.TransactionDefaultTimeout; // Unit is seconds.

            // Check arguments.
            try
            {
                clientInformation.CheckNotNull("clientInformation");
                CheckUser();
                CheckClientIpAddress();
                CheckHttpsProtocol();
                CheckWebServiceName();
                CheckCurrentRole();
            }
            catch (Exception exception)
            {
                WebServiceData.LogManager.LogSecurityError(this, exception);
                throw;
            }
        }
示例#2
0
        public void CheckData()
        {
            WebClientToken clientToken;

            clientToken = new WebClientToken(TEST_USER_NAME, ApplicationIdentifier, WebServiceData.WebServiceManager.Key);
            clientToken.CheckData();
        }
示例#3
0
        /// <summary>
        /// Create a WebServiceContext instance.
        /// This contructor should only be used during login.
        /// </summary>
        /// <param name="userName">User name.</param>
        /// <param name="applicationIdentifier">
        /// Identifier of the application that the user uses.
        /// </param>
        public WebServiceContext(String userName,
                                 String applicationIdentifier)
        {
            // Init object.
            _database    = null;
            _clientToken = new WebClientToken(userName,
                                              applicationIdentifier,
                                              WebServiceData.WebServiceManager.Key);
            _requestId = GetNextRequestId();
            if (_isTracing)
            {
                _traceStart = DateTime.Now;
            }
            _transactionTimeout = Settings.Default.TransactionDefaultTimeout; // Unit is seconds.

            // Check arguments.
            try
            {
                CheckHttpsProtocol();
            }
            catch (Exception exception)
            {
                WebServiceData.LogManager.LogSecurityError(this, exception);
                throw;
            }
            _currentRole = null;

            // This is only a temporary value.
            // Real value is set by UserManager when user has logged in.
            _locale = GetDefaultLocale();
        }
示例#4
0
        public void GetClientIPAddressMaxLength()
        {
            Int32 maxLength = 0;

            maxLength = WebClientToken.GetClientIPAddressMaxLength();
            Assert.IsTrue(0 < maxLength);
        }
示例#5
0
        /// <summary>
        /// Add information about client to Application Insights.
        /// </summary>
        /// <param name="clientInformation">Information about the client that makes this web service call.</param>
        private void LogClientToken(WebClientInformation clientInformation)
        {
            RequestTelemetry telemetry;
            WebClientToken   clientToken;
            WebUser          user;

            try
            {
                if (Configuration.InstallationType == InstallationType.Production)
                {
                    clientToken = new WebClientToken(clientInformation.Token, WebServiceData.WebServiceManager.Key);
                    if (clientToken.IsNotNull())
                    {
                        telemetry = OperationContext.Current.GetRequestTelemetry();
                        if (telemetry.IsNotNull())
                        {
                            telemetry.Properties[TelemetryProperty.ApplicationIdentifier.ToString()] = clientToken.ApplicationIdentifier;
                            telemetry.Properties[TelemetryProperty.ClientIpAddress.ToString()]       = clientToken.ClientIpAddress;
                            telemetry.Properties[TelemetryProperty.LoginDateTime.ToString()]         = clientToken.CreatedDate.WebToString();

                            user = WebServiceData.UserManager.GetUser(clientToken.UserName);
                            if (user.IsNotNull())
                            {
                                telemetry.Properties[TelemetryProperty.UserId.ToString()] = user.Id.WebToString();
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                // Do nothing. We don't want calls to fail because of logging problems.
            }
        }
示例#6
0
        public void CheckData()
        {
            WebClientToken clientToken;

            clientToken = new WebClientToken(Settings.Default.TestUserName, Settings.Default.TestClientApplicationName, WebServiceData.WebServiceManager.Key);
            clientToken.CheckData();
            clientToken = new WebClientToken(Settings.Default.TestUserName, Settings.Default.TestClientApplicationName, WebServiceData.WebServiceManager.Key);
            clientToken.CheckData();
        }
示例#7
0
        private static WebClientToken GetWebClientToken(Boolean refresh = false)
        {
            if (mClientToken.IsNull() || refresh)
            {
                mClientToken = new WebClientToken(Settings.Default.TestUserName, Settings.Default.TestClientApplicationName, WebServiceData.WebServiceManager.Key);
            }

            return(mClientToken);
        }
示例#8
0
 /// <summary>
 /// Create a WebServiceContext instance.
 /// </summary>
 /// <param name='clientToken'>Token with information about current user and connection</param>
 /// <param name='checkUser'>Information about if user authority should be checked.
 ///                         It should only be set to false during login.</param>
 /// <param name='traceMethod'>Name of calling method. Used during tracing.</param>
 /// <param name='traceArgument1'>Argument 1 to the calling method. Used during tracing.</param>
 /// <param name='traceArgument2'>Argument 2 to the calling method. Used during tracing.</param>
 /// <param name='traceArgument3'>Argument 3 to the calling method. Used during tracing.</param>
 public WebServiceContextCached(WebClientToken clientToken,
                                Boolean checkUser,
                                String traceMethod,
                                Object traceArgument1,
                                Object traceArgument2,
                                Object traceArgument3)
     : base(clientToken, checkUser, traceMethod, traceArgument1, traceArgument2, traceArgument3)
 {
 }
示例#9
0
        public void Constructor()
        {
            WebClientToken clientToken;

            clientToken = new WebClientToken(TEST_USER_NAME, ApplicationIdentifier, WebServiceData.WebServiceManager.Key);
            Assert.IsNotNull(clientToken);

            clientToken = new WebClientToken(GetWebClientToken(true).Token, WebServiceData.WebServiceManager.Key);
            Assert.IsNotNull(clientToken);
        }
示例#10
0
        public void TokenNullError()
        {
            String         token;
            CipherString   cipherString;
            WebClientToken clientToken;

            cipherString = new CipherString();
            token        = cipherString.EncryptText(null);
            clientToken  = new WebClientToken(token, WebServiceData.WebServiceManager.Key);
            clientToken.CheckData();
        }
示例#11
0
        public void CheckDataSessionIdFormatError()
        {
            String token;

            token = TEST_USER_NAME + Settings.Default.ClientTokenDelimitor +
                    ApplicationIdentifier + Settings.Default.ClientTokenDelimitor +
                    "Hej" + Settings.Default.ClientTokenDelimitor +
                    GetString(WebClientToken.GetClientIPAddressMaxLength()) + Settings.Default.ClientTokenDelimitor +
                    WebServiceData.WebServiceManager.Name;
            GetWebClientToken(token).CheckData();
        }
示例#12
0
        public void TokenFormatError()
        {
            String         token;
            CipherString   cipherString;
            WebClientToken clientToken;

            token        = "Hej hopp i lingon skogen!";
            cipherString = new CipherString();
            token        = cipherString.EncryptText(token);
            clientToken  = new WebClientToken(token, WebServiceData.WebServiceManager.Key);
            clientToken.CheckData();
        }
示例#13
0
        public void GetRandomText()
        {
            Int32  index;
            String randomText;

            for (index = 0; index < 20; index++)
            {
                randomText = WebClientToken.GetRandomText();
                Assert.IsTrue(randomText.IsNotEmpty());
                Assert.IsFalse(randomText.Contains(ArtDatabanken.WebService.Settings.Default.ClientTokenDelimiter.ToString()));
            }
        }
示例#14
0
        public void Constructor()
        {
            WebClientToken clientToken;

            clientToken = new WebClientToken(Settings.Default.TestUserName,
                                             Settings.Default.TestClientApplicationName,
                                             WebServiceData.WebServiceManager.Key);
            Assert.IsNotNull(clientToken);

            clientToken = new WebClientToken(GetWebClientToken(true).Token, WebServiceData.WebServiceManager.Key);
            Assert.IsNotNull(clientToken);
        }
        public void TestInitialize()
        {
            Configuration.SetInstallationType();
            WebServiceData.ApplicationManager          = new ApplicationManager();
            WebServiceData.AuthorizationManager        = new AuthorizationManager();
            WebServiceData.CoordinateConversionManager = new CoordinateConversionManager();
            WebServiceData.DatabaseManager             = new DatabaseManager();
            WebServiceData.LogManager                = new LogManager();
            WebServiceData.RegionManager             = new RegionManager();
            WebServiceData.UserManager               = new UserManager();
            WebServiceData.WebServiceManager         = new WebServiceManager();
            WebServiceData.SpeciesActivityManager    = new SpeciesActivityManager();
            WebServiceData.SpeciesObservationManager = new SpeciesObservationManager();
            WebServiceData.TaxonManager              = new TaxonManager();
            WebServiceData.AnalysisManager           = new AnalysisManager();
            WebServiceData.SpeciesFactManager        = new ArtDatabanken.WebService.Data.SpeciesFactManager();

            WebClientInformation clientInformation;
            WebClientToken       clientToken;

            clientToken = new WebClientToken(Settings.Default.TestUserName,
                                             ApplicationIdentifier.EVA.ToString(),
                                             WebServiceData.WebServiceManager.Key);
            clientInformation                   = new WebClientInformation();
            clientInformation.Locale            = new WebLocale();
            clientInformation.Locale.Id         = 581;
            clientInformation.Locale.ISOCode    = "se-SV";
            clientInformation.Locale.Name       = "Swedish (Sweden)";
            clientInformation.Locale.NativeName = "svenska (Sverige)";
            clientInformation.Token             = clientToken.Token;
            _context = new WebServiceContextCached(Settings.Default.TestUserName, ApplicationIdentifier.EVA.ToString());

            var roles = _context.GetRoles();

            foreach (var webRole in roles)
            {
                if (webRole.Id == 701)
                {
                    clientInformation.Role      = new WebRole();
                    clientInformation.Role.Id   = webRole.Id;
                    clientInformation.Role.Name = webRole.Name;
                    _context = new WebServiceContextCached(clientInformation);
                    break;
                }
            }

            if (_useTransaction)
            {
                _context.StartTransaction(_transactionTimeout);
            }
        }
示例#16
0
        public void GetUserUnknownUserError()
        {
            WebClientToken clientToken;

            ArtDatabanken.WebService.Data.WebUser user;

            // Get none existing user.
            clientToken = new WebClientToken("None existing user", "No application identifier", WebServiceData.WebServiceManager.Key);
            using (WebServiceContext context = new WebServiceContextCached(clientToken, false))
            {
                user = GetUserManager().GetUser(context);
                Assert.IsNull(user);
            }
        }
        public static WebServiceContext GetOneWebServiceContext()
        {
            WebClientInformation clientInformation;
            WebClientToken       clientToken;

            clientToken = new WebClientToken(Settings.Default.TestUserName,
                                             Settings.Default.UserAdminApplicationIdentifier,
                                             WebServiceData.WebServiceManager.Key);
            clientInformation                   = new WebClientInformation();
            clientInformation.Locale            = new WebLocale();
            clientInformation.Locale.Id         = 581;
            clientInformation.Locale.ISOCode    = "se-SV";
            clientInformation.Locale.Name       = "Swedish (Sweden)";
            clientInformation.Locale.NativeName = "svenska (Sverige)";
            clientInformation.Token             = clientToken.Token;
            return(new WebServiceContext(clientInformation));
        }
        private static WebServiceContext GetOneWebServiceContext()
        {
            WebClientInformation clientInformation;
            WebClientToken       clientToken;

            clientToken = new WebClientToken(Settings.Default.TestUserName,
                                             ApplicationIdentifier.EVA.ToString(),
                                             null);
            clientInformation                   = new WebClientInformation();
            clientInformation.Locale            = new WebLocale();
            clientInformation.Locale.Id         = 581;
            clientInformation.Locale.ISOCode    = "se-SV";
            clientInformation.Locale.Name       = "Swedish (Sweden)";
            clientInformation.Locale.NativeName = "svenska (Sverige)";
            clientInformation.Token             = clientToken.Token;
            return(new WebServiceContext(clientInformation));
        }
示例#19
0
        /// <summary>
        /// Create a WebServiceContext instance.
        /// </summary>
        /// <param name='clientToken'>Token with information about current user and connection</param>
        /// <param name='checkUser'>Information about if user authority should be checked.
        ///                         It should only be set to false during login.</param>
        /// <param name='traceMethod'>Name of calling method. Used during tracing.</param>
        /// <param name='traceArgument1'>Argument 1 to the calling method. Used during tracing.</param>
        /// <param name='traceArgument2'>Argument 2 to the calling method. Used during tracing.</param>
        /// <param name='traceArgument3'>Argument 3 to the calling method. Used during tracing.</param>
        public WebServiceContext(WebClientToken clientToken,
                                 Boolean checkUser,
                                 String traceMethod,
                                 Object traceArgument1,
                                 Object traceArgument2,
                                 Object traceArgument3)
        {
            // Init context.
            _databases      = new DataServer[Enum.GetNames(typeof(DataServer.DatabaseId)).Length];
            _clientToken    = clientToken;
            _locale         = null;
            _requestId      = GetNextRequestId();
            _traceArgument1 = traceArgument1;
            _traceArgument2 = traceArgument2;
            _traceArgument3 = traceArgument3;
            _traceMethod    = traceMethod;
            if (_isTracing)
            {
                _traceStart = DateTime.Now;
            }
            else
            {
                _traceStart = DateTime.MinValue;
            }
            _transactionTimeout = DEFAULT_TRANSACTION_TIMEOUT;

            // Check arguments.
            try
            {
                clientToken.CheckNotNull("clientToken");
                if (checkUser)
                {
                    clientToken.CheckData();
                    CheckUser();
                }
                CheckClientIPAddress();
                CheckHttpsProtocol();
            }
            catch (Exception exception)
            {
                LogManager.LogSecurityError(this, exception);
                throw exception;
            }
        }
示例#20
0
        public void CheckDataSessionIdFormatError()
        {
            CipherString   cipherString;
            String         token;
            WebClientToken clientToken;

            token = WebClientToken.GetRandomText() + WebService.Settings.Default.ClientTokenDelimiter +
                    DateTime.Now.WebToString() + WebService.Settings.Default.ClientTokenDelimiter +
                    Settings.Default.TestUserName + WebService.Settings.Default.ClientTokenDelimiter +
                    Settings.Default.TestClientApplicationName + WebService.Settings.Default.ClientTokenDelimiter +
                    "Hej" + WebService.Settings.Default.ClientTokenDelimiter +
                    WebServiceContext.GetClientIpAddress() + WebService.Settings.Default.ClientTokenDelimiter +
                    WebServiceData.WebServiceManager.Name + WebService.Settings.Default.ClientTokenDelimiter +
                    WebClientToken.GetRandomText();
            cipherString = new CipherString();
            token        = cipherString.EncryptText(token);
            clientToken  = new WebClientToken(token, WebServiceData.WebServiceManager.Key);
            clientToken.CheckData();
        }
示例#21
0
        public void Constructor()
        {
            WebClientToken clientToken;
            String         token;

            Configuration.InstallationType = InstallationType.ServerTest;
            clientToken = new WebClientToken(WebServiceData.WebServiceManager.Name,
                                             ApplicationIdentifier.PrintObs.ToString(),
                                             WebServiceData.WebServiceManager.Key);
            token = clientToken.Token;
            Assert.IsTrue(token.IsNotEmpty());

            Configuration.InstallationType = InstallationType.Production;
            clientToken = new WebClientToken(token,
                                             WebServiceData.WebServiceManager.Key);
            token = clientToken.Token;
            Assert.IsTrue(token.IsNotEmpty());
            Configuration.InstallationType = InstallationType.ServerTest;
        }
示例#22
0
        public WebClientToken GetWebClientToken(Boolean refresh, String token)
        {
            CipherString cipherString;

            if (_clientToken.IsNull() || refresh || token.IsNotEmpty())
            {
                if (token.IsEmpty())
                {
                    _clientToken = new WebClientToken(TEST_USER_NAME, ApplicationIdentifier, WebServiceData.WebServiceManager.Key);
                }
                else
                {
                    cipherString = new CipherString();
                    token        = cipherString.EncryptText(token);
                    _clientToken = new WebClientToken(token, WebServiceData.WebServiceManager.Key);
                }
            }
            return(_clientToken);
        }
示例#23
0
        public void CheckDataClientIpAddressToLongError()
        {
            CipherString   cipherString;
            String         token;
            WebClientToken clientToken;

            token = WebClientToken.GetRandomText() + WebService.Settings.Default.ClientTokenDelimiter +
                    DateTime.Now.WebToString() + WebService.Settings.Default.ClientTokenDelimiter +
                    Settings.Default.TestUserName + WebService.Settings.Default.ClientTokenDelimiter +
                    Settings.Default.TestPassword + WebService.Settings.Default.ClientTokenDelimiter +
                    42 + WebService.Settings.Default.ClientTokenDelimiter +
                    GetString(WebService.Settings.Default.IPv6MaxStringLength + 1) + WebService.Settings.Default.ClientTokenDelimiter +
                    WebServiceData.WebServiceManager.Name + WebService.Settings.Default.ClientTokenDelimiter +
                    WebClientToken.GetRandomText();
            cipherString = new CipherString();
            token        = cipherString.EncryptText(token);
            clientToken  = new WebClientToken(token, WebServiceData.WebServiceManager.Key);
            clientToken.CheckData();
        }
        public void ConstructorUserError()
        {
            WebClientInformation clientInformation;
            WebClientToken       clientToken;
            WebServiceContext    context;

            clientToken = new WebClientToken("NoUser",
                                             Settings.Default.UserAdminApplicationIdentifier,
                                             WebServiceData.WebServiceManager.Key);
            clientInformation                   = new WebClientInformation();
            clientInformation.Locale            = new WebLocale();
            clientInformation.Locale.Id         = 581;
            clientInformation.Locale.ISOCode    = "se-SV";
            clientInformation.Locale.Name       = "Swedish (Sweden)";
            clientInformation.Locale.NativeName = "svenska (Sverige)";
            clientInformation.Token             = clientToken.Token;
            context = new WebServiceContext(clientInformation);
            Assert.IsNotNull(context);
        }
        public void ConstructorUserError()
        {
            WebClientInformation clientInformation;
            WebClientToken       clientToken;
            WebServiceContext    context;

            clientToken = new WebClientToken("NoUser",
                                             ApplicationIdentifier.EVA.ToString(),
                                             null);
            clientInformation                   = new WebClientInformation();
            clientInformation.Locale            = new WebLocale();
            clientInformation.Locale.Id         = 581;
            clientInformation.Locale.ISOCode    = "se-SV";
            clientInformation.Locale.Name       = "Swedish (Sweden)";
            clientInformation.Locale.NativeName = "svenska (Sverige)";
            clientInformation.Token             = clientToken.Token;
            context = new WebServiceContext(clientInformation);
            Assert.IsNotNull(context);
        }
        public void CurrentRoles()
        {
            WebClientInformation clientInformation;
            WebClientToken       clientToken;
            WebServiceContext    context;

            // Test without selected role
            clientToken = new WebClientToken(Settings.Default.TestUserName,
                                             ApplicationIdentifier.UserAdmin.ToString(),
                                             WebServiceData.WebServiceManager.Key);
            clientInformation                   = new WebClientInformation();
            clientInformation.Locale            = new WebLocale();
            clientInformation.Locale.Id         = 581;
            clientInformation.Locale.ISOCode    = "se-SV";
            clientInformation.Locale.Name       = "Swedish (Sweden)";
            clientInformation.Locale.NativeName = "svenska (Sverige)";
            clientInformation.Token             = clientToken.Token;
            context = new WebServiceContextCached(clientInformation);
            Assert.IsTrue(context.CurrentRoles.IsNotEmpty());
            Assert.IsTrue(1 < context.CurrentRoles.Count);

            // Test with selected role.
            clientToken = new WebClientToken(Settings.Default.TestUserName,
                                             ApplicationIdentifier.UserAdmin.ToString(),
                                             WebServiceData.WebServiceManager.Key);
            clientInformation                   = new WebClientInformation();
            clientInformation.Role              = new WebRole();
            clientInformation.Role.Id           = Settings.Default.TestRoleId;
            clientInformation.Role.Name         = "No role name";
            clientInformation.Locale            = new WebLocale();
            clientInformation.Locale.Id         = 581;
            clientInformation.Locale.ISOCode    = "se-SV";
            clientInformation.Locale.Name       = "Swedish (Sweden)";
            clientInformation.Locale.NativeName = "svenska (Sverige)";
            clientInformation.Token             = clientToken.Token;
            context = new WebServiceContextCached(clientInformation);
            Assert.IsTrue(context.CurrentRoles.IsNotEmpty());
            Assert.AreEqual(1, context.CurrentRoles.Count);
            Assert.AreEqual(Settings.Default.TestRoleId, context.CurrentRoles[0].Id);
        }
        /// <summary>
        /// Log out current user and log in requested user.
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="applicationIdentifier"></param>
        protected void SetUserAndApplicationIdentifier(String userName, String applicationIdentifier)
        {
            TestCleanup();

            WebClientInformation clientInformation;
            WebClientToken       clientToken;

            clientToken                         = new WebClientToken(userName, applicationIdentifier, WebServiceData.WebServiceManager.Key);
            clientInformation                   = new WebClientInformation();
            clientInformation.Locale            = new WebLocale();
            clientInformation.Locale.Id         = 581;
            clientInformation.Locale.ISOCode    = "se-SV";
            clientInformation.Locale.Name       = "Swedish (Sweden)";
            clientInformation.Locale.NativeName = "svenska (Sverige)";
            clientInformation.Token             = clientToken.Token;
            _context = new WebServiceContextCached(clientInformation);

            if (_useTransaction)
            {
                _context.StartTransaction(_transactionTimeout);
            }
        }
        public void ConstructorCurrentRoleError()
        {
            WebClientInformation clientInformation;
            WebClientToken       clientToken;
            WebServiceContext    context;

            clientToken = new WebClientToken(Settings.Default.TestUserName,
                                             ApplicationIdentifier.EVA.ToString(),
                                             WebServiceData.WebServiceManager.Key);
            clientInformation                   = new WebClientInformation();
            clientInformation.Role              = new WebRole();
            clientInformation.Role.Id           = -42;
            clientInformation.Role.Name         = "No role name";
            clientInformation.Locale            = new WebLocale();
            clientInformation.Locale.Id         = 581;
            clientInformation.Locale.ISOCode    = "se-SV";
            clientInformation.Locale.Name       = "Swedish (Sweden)";
            clientInformation.Locale.NativeName = "svenska (Sverige)";
            clientInformation.Token             = clientToken.Token;
            context = new WebServiceContextCached(clientInformation);
            Assert.IsNotNull(context);
        }
        public void CurrentRole()
        {
            WebClientInformation clientInformation;
            WebClientToken       clientToken;
            WebServiceContext    context;

            clientToken = new WebClientToken(Settings.Default.TestUserName,
                                             ApplicationIdentifier.UserAdmin.ToString(),
                                             null);
            clientInformation                   = new WebClientInformation();
            clientInformation.Role              = new WebRole();
            clientInformation.Role.Id           = Settings.Default.TestRoleId;
            clientInformation.Role.Name         = "No role name";
            clientInformation.Locale            = new WebLocale();
            clientInformation.Locale.Id         = 581;
            clientInformation.Locale.ISOCode    = "se-SV";
            clientInformation.Locale.Name       = "Swedish (Sweden)";
            clientInformation.Locale.NativeName = "svenska (Sverige)";
            clientInformation.Token             = clientToken.Token;
            context = new WebServiceContextCached(clientInformation);
            Assert.IsNotNull(context.CurrentRole);
            Assert.AreEqual(Settings.Default.TestRoleId, context.CurrentRole.Id);
        }
示例#30
0
 /// <summary>
 /// Create a WebServiceContext instance.
 /// </summary>
 /// <param name='clientToken'>Token with information about current user and connection</param>
 /// <param name='checkUser'>Information about if user authority should be checked.
 ///                         It should only be set to false during login.</param>
 public WebServiceContext(WebClientToken clientToken,
                          Boolean checkUser)
     : this(clientToken, checkUser, null, null, null, null)
 {
 }