public async Task AsyncTest()
		{
			var baseAppAuthUrl = "http://home.mozu-ci.volusion.com/";
			var appId = "5d76bb2a852d4741939fa27d00d98a40";
			var sharedSecret = "348f780339b749b58d3fa27d00d98a40";

			var appAuthInfo = new AppAuthInfo
			{
				ApplicationId = appId,
				SharedSecret = sharedSecret
			};

		    MozuConfig.BaseAppAuthUrl = baseAppAuthUrl;
			await AppAuthenticator.InitializeAsync(appAuthInfo);

			Assert.IsNotNull(AppAuthenticator.Instance);
			Assert.IsNotNull(AppAuthenticator.Instance.AppAuthTicket);
			Assert.IsNotNull(AppAuthenticator.Instance.AppAuthTicket.AccessToken);

			//var tenantResource = new TenantResource();
			//var tenant = await tenantResource.GetTenantAsync(9539);
			//Assert.IsNotNull(tenant);
			//Assert.AreEqual(tenant.Id, 9539);

		}
        /// <summary>
        /// This contructor does application authentication and setups up the necessary timers to keep the app auth ticket valid.
        /// </summary>
        /// <param name="appId">The application version's app id</param>
        /// <param name="sharedSecret">The application version's shared secret</param>
        /// <param name="baseAppAuthUrl">The base URL of the Mozu application authentication service</param>
        private AppAuthenticator(AppAuthInfo appAuthInfo, string baseAppAuthUrl, RefreshInterval refreshInterval = null)
        {
            BaseUrl = baseAppAuthUrl;
            _appAuthInfo = appAuthInfo;
            _refreshInterval = refreshInterval;

            MozuConfig.SharedSecret = appAuthInfo.SharedSecret;
            MozuConfig.ApplicationId = appAuthInfo.ApplicationId;
        }
        public static void AuthApp(string mozuApplicationId, string mozuApplicationSecret)
        {
            MozuApplicationId = mozuApplicationId;
            MozuApplicationSecret = mozuApplicationSecret;
            MozuConfig.ThrowExceptionOn404 = true;

            // auth with mozu
            _mozuAppAuthInfo = new AppAuthInfo()
            {
                ApplicationId = MozuApplicationId,
                SharedSecret = MozuApplicationSecret
            };
            AppAuthenticator.Initialize(_mozuAppAuthInfo);
            isAuthed = true;
        }
Beispiel #4
0
        private void btnAuthenticate_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtApplicationID.Text.Length > 20
                    && txtSharedSecret.Text.Length > 20)
                {
                    btnAuthenticate.Text = "Authenticating...";
                    var appAuthInfo = new AppAuthInfo
                    {
                        ApplicationId = txtApplicationID.Text,
                        SharedSecret = txtSharedSecret.Text
                    };
                    if (txtEmail.Text.Contains("@") &&
                        txtEmail.Text.Contains(".") &&
                        txtPassword.Text.Length > 5)
                    {
                        AppAuthenticator.Initialize(appAuthInfo, GetAuthBaseUrl());
                        btnAuthenticate.Text = "Loading Scopes...";
                        panelAPI.Visible = true;
                        panelTenant.Visible = true;
                        var userAuthInfo = new UserAuthInfo {EmailAddress = txtEmail.Text, Password = txtPassword.Text};
                        _userInfo = UserAuthenticator.Authenticate(userAuthInfo, AuthenticationScope.Tenant);

                        panelTenant.Visible = true;

                        cbTenant.DataSource = _userInfo.AuthorizedScopes;

                        btnAuthenticate.Text = "Renew Authentication";
                    }
                    else
                    {
                        btnAuthenticate.Text = "Authenticate";
                        LogError(new Exception("Not enough User data entered for User Scope Authentication"));
                    }
                }
                else
                {
                   LogError(new Exception("Not enough Application data entered for Authentication"));
                }
            }
            catch (ApiException exc)
            {
                LogError(exc);
                btnAuthenticate.Text = "Authenticate";
            }
        }
        public void SimpleAppAuthLoginTest()
        {
            var baseAppAuthUrl = "http://aus02ndserv001.dev.volusion.com/Mozu.AppDev.WebApi/platform/applications/authtickets/";
            var appId = "158496f0ca114e0b88bda2ed011dc745";
            var sharedSecret = "3c9a6a0bd09b44d1a7c7a2ed011dc745";

            var appAuthInfo = new AppAuthInfo
                              {
                                  ApplicationId = appId,
                                  SharedSecret = sharedSecret
                              };
            MozuConfig.BaseAppAuthUrl = baseAppAuthUrl;
            var authenticator = AppAuthenticator.Initialize(appAuthInfo);
            authenticator.EnsureAuthTicket();

            Assert.IsNotNull(appAuthInfo);
            Assert.IsNotNull(appAuthInfo.ApplicationId);
            Assert.IsNotNull(appAuthInfo.SharedSecret);            
        }
        public static AppAuthenticator Initialize(AppAuthInfo appAuthInfo, RefreshInterval refreshInterval = null)
        {

			var baseAppAuthUrl = MozuConfig.BaseAppAuthUrl;

            if (appAuthInfo == null || string.IsNullOrEmpty(baseAppAuthUrl))
                throw new Exception("AppAuthInfo or Base App auth Url cannot be null or empty");

            if (String.IsNullOrEmpty(appAuthInfo.ApplicationId) || String.IsNullOrEmpty(appAuthInfo.SharedSecret))
                throw new Exception("ApplicationId or Shared Secret is missing");

            if (_auth == null || (_auth != null && _auth.AppAuthInfo.ApplicationId != appAuthInfo.ApplicationId))
            {
                _semaphoreWaiter.Wait();
                lock (_lockObj)
                {
                    try
                    {
                        _log.Info("Initializing App");
                        var uri = new Uri(baseAppAuthUrl);
                        HttpHelper.UrlScheme = uri.Scheme;
                        _auth = new AppAuthenticator(appAuthInfo, baseAppAuthUrl, refreshInterval);
                        _auth.AuthenticateApp();
                        _log.Info("Initializing App..Done");

                    }
                    catch (ApiException exc)
                    {
                        _log.Error(exc.Message, exc);
                        _auth = null;
                        throw exc;
                    }
                    finally
                    {
                        _semaphoreWaiter.Release();
                    }
                }
            }

            return _auth;
        }
		public static async Task<AppAuthenticator> InitializeAsync(AppAuthInfo appAuthInfo,  RefreshInterval refreshInterval = null)
		{
            var baseAppAuthUrl = MozuConfig.BaseAppAuthUrl;
			if (appAuthInfo == null || string.IsNullOrEmpty(baseAppAuthUrl))
				throw new Exception("AppAuthInfo or Base App auth Url cannot be null or empty");

			if (String.IsNullOrEmpty(appAuthInfo.ApplicationId) || String.IsNullOrEmpty(appAuthInfo.SharedSecret))
				throw new Exception("ApplicationId or Shared Secret is missing");

		    if (_auth != null && _auth.AppAuthInfo.ApplicationId == appAuthInfo.ApplicationId)
		        return _auth;
		    try
		    {
                await _semaphoreWaiter.WaitAsync();
                // Double check to make sure that someone else didn't already initialize it while we were waiting
                if (_auth == null || (_auth != null && _auth.AppAuthInfo.ApplicationId != appAuthInfo.ApplicationId))
                {
                    try
		            {
		                _log.Info("Initializing App");
		                var uri = new Uri(baseAppAuthUrl);
		                HttpHelper.UrlScheme = uri.Scheme;
		                var tmp = new AppAuthenticator(appAuthInfo, baseAppAuthUrl, refreshInterval);
		                await tmp.AuthenticateAppAsync();
		                lock (_lockObj)
		                {
		                    _auth = tmp;
		                }
		            }
		            finally
		            {
		                _semaphoreWaiter.Release();
                    }
                    _log.Info("Initializing App..Done");
                }
            }
		    catch (ApiException exc)
		    {
		        _log.Error(exc.Message, exc);
		        lock (_lockObj)
		        {
		            _auth = null;
		        }
		        throw exc;
		    }

		    return _auth;
		}
		public static async Task<AppAuthenticator> InitializeAsync(AppAuthInfo appAuthInfo,  RefreshInterval refreshInterval = null)
		{
            var baseAppAuthUrl = MozuConfig.BaseAppAuthUrl;
			if (appAuthInfo == null || string.IsNullOrEmpty(baseAppAuthUrl))
				throw new Exception("AppAuthInfo or Base App auth Url cannot be null or empty");

			if (String.IsNullOrEmpty(appAuthInfo.ApplicationId) || String.IsNullOrEmpty(appAuthInfo.SharedSecret))
				throw new Exception("ApplicationId or Shared Secret is missing");

		    if (_auth != null && _auth.AppAuthInfo.ApplicationId == appAuthInfo.ApplicationId)
		        return _auth;
		    try
		    {
		        _log.Info("Initializing App");
		        var uri = new Uri(baseAppAuthUrl);
		        HttpHelper.UrlScheme = uri.Scheme;
		        var tmp = new AppAuthenticator(appAuthInfo, baseAppAuthUrl, refreshInterval);
		        await tmp.AuthenticateAppAsync();
		        lock (_lockObj)
		        {
		            _auth = tmp;
		        }
		        _log.Info("Initializing App..Done");

		    }
		    catch (ApiException exc)
		    {
		        _log.Error(exc.Message, exc);
		        lock (_lockObj)
		        {
		            _auth = null;
		        }
		        throw exc;
		    }

		    return _auth;
		}