Ejemplo n.º 1
0
        public static async Task <CustomerAuthenticationProfile> RefreshUserAuthTicket(AuthTicket authTicket)
        {
            var resourceUrl = CustomerAuthTicketUrl.RefreshUserAuthTicketUrl(authTicket.RefreshToken).Url;

            var authUrl = await GetAuthUrl(authTicket.TenantId.Value).ConfigureAwait(false);

            var client = new HttpClient {
                BaseAddress = new Uri(authUrl)
            };

            AppAuthenticator.AddHeader(client);

            if (authTicket.SiteId.HasValue)
            {
                client.DefaultRequestHeaders.Add(Headers.X_VOL_SITE, authTicket.SiteId.Value.ToString());
            }

            var stringContent = JsonConvert.SerializeObject(authTicket);
            var response      = client.PutAsync(resourceUrl, new StringContent(stringContent, Encoding.UTF8, "application/json")).Result;

            ResponseHelper.EnsureSuccess(response);

            var userInfo = SetUserAuth(response.Content.ReadAsStringAsync().Result, authTicket.AuthenticationScope, authTicket.TenantId.Value, authTicket.SiteId.Value);

            return(userInfo);
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        public static async Task LogoutAsync(AuthTicket authTicket)
        {
            var url    = GetLogoutUrl(authTicket);
            var client = new HttpClient {
                BaseAddress = new Uri(AppAuthenticator.Instance.BaseUrl)
            };

            AppAuthenticator.AddHeader(client);

            var response = await client.DeleteAsync(url);

            ResponseHelper.EnsureSuccess(response);
        }
Ejemplo n.º 4
0
        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);
        }
Ejemplo n.º 5
0
        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);
        }
Ejemplo n.º 6
0
        public static async Task <AuthenticationProfile> AuthenticateAsync(UserAuthInfo authInfo, AuthenticationScope scope, int?id = null)
        {
            var resourceUrl = GetResourceUrl(scope, id);

            var client = new HttpClient {
                BaseAddress = new Uri(AppAuthenticator.Instance.BaseUrl)
            };


            var stringContent = JsonConvert.SerializeObject(authInfo);

            AppAuthenticator.AddHeader(client);

            var response = await client.PostAsync(resourceUrl, new StringContent(stringContent, Encoding.UTF8, "application/json"));

            ResponseHelper.EnsureSuccess(response);


            return(SetUserAuth(response.Content.ReadAsStringAsync().Result, scope, null));
        }
Ejemplo n.º 7
0
        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;
        }
Ejemplo n.º 8
0
        public static CustomerAuthenticationProfile Authenticate(CustomerUserAuthInfo customerUserAuthInfo,
                                                                 int tenantId, int siteId)
        {
            var resourceUrl = CustomerAuthTicketUrl.CreateUserAuthTicketUrl().Url;


            var client = new HttpClient {
                BaseAddress = new Uri(GetAuthUrl(tenantId))
            };

            client.DefaultRequestHeaders.Add(Headers.X_VOL_SITE, siteId.ToString());

            var stringContent = JsonConvert.SerializeObject(customerUserAuthInfo);

            AppAuthenticator.AddHeader(client);

            var response = client.PostAsync(resourceUrl, new StringContent(stringContent, Encoding.UTF8, "application/json")).Result;

            ResponseHelper.EnsureSuccess(response);


            return(SetUserAuth(response.Content.ReadAsStringAsync().Result, AuthenticationScope.Customer, tenantId, siteId));
        }
Ejemplo n.º 9
0
        public static async Task <AuthenticationProfile> RefreshUserAuthTicketAsync(AuthTicket authTicket, int?id = null)
        {
            var resourceUrl = GetResourceRefreshUrl(authTicket, id);

            var client = new HttpClient {
                BaseAddress = new Uri(AppAuthenticator.Instance.BaseUrl)
            };

            AppAuthenticator.AddHeader(client);

            if (authTicket.SiteId.HasValue)
            {
                client.DefaultRequestHeaders.Add(Headers.X_VOL_SITE, authTicket.SiteId.Value.ToString());
            }

            var stringContent = JsonConvert.SerializeObject(authTicket);
            var response      = client.PutAsync(resourceUrl, new StringContent(stringContent, Encoding.UTF8, "application/json")).Result;

            ResponseHelper.EnsureSuccess(response);

            var userInfo = SetUserAuth(response.Content.ReadAsStringAsync().Result, authTicket.AuthenticationScope);

            return(userInfo);
        }
Ejemplo n.º 10
0
		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;
		}
Ejemplo n.º 11
0
		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;
		}