Commit c0a81e06 authored by Francesco Carraro's avatar Francesco Carraro
Browse files

added check for login and token valid at start

parent 6ca0aa03
Loading
Loading
Loading
Loading
+5 −1
Original line number Diff line number Diff line
@@ -68,12 +68,15 @@ namespace INAF.Apps.Uwp.SLabDataManager

        private ActivationService CreateActivationService()
        {
            /* hide left navigation pane to avoid navigation to main page if user is not allowed */
            Ioc.Default.GetService<ShellViewModel>().IsPaneVisible = false;

            var settingsHelper = Ioc.Default.GetService<SettingsHelper>();
            if (settingsHelper.contains(Constants.Constants.AUTHENTICATION_TOKEN))
            {
                AuthenticationManager authenticationManager = Ioc.Default.GetService<AuthenticationManager>();
                if (!authenticationManager.isAuthenticationExpired(Constants.Constants.AUTHENTICATION_TOKEN))
                    return new ActivationService(this, typeof(Views.MainPage), new Lazy<UIElement>(CreateShell));
                    return new ActivationService(this, typeof(Views.LoginCheckPage), new Lazy<UIElement>(CreateShell));
            }

            return new ActivationService(this, typeof(Views.LoginPage), new Lazy<UIElement>(CreateShell));
@@ -116,6 +119,7 @@ namespace INAF.Apps.Uwp.SLabDataManager
                /* view models */
                .AddTransient<ChartViewModel>()
                .AddTransient<LoginViewModel>()
                .AddTransient<LoginCheckViewModel>()
                .AddSingleton<MainViewModel>()
                .AddSingleton<SettingsViewModel>()
                .AddSingleton<ShellViewModel>()
+2 −0
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@
	<baseUrl>http://localhost:81</baseUrl>
	<operationUrls>
		<operation type="Authentication">Account/LoginForJwt</operation>
		<operation type="IsTokenValid">Account/IsTokenValid</operation>
		<operation type="IsLoginValid">Account/IsLoginValid</operation>
		<operation type="SaveAlignedFile">AppSpectraFilesActions/SaveAlignedSpectrum</operation>
		<operation type="SaveFileOfType">AppSpectraFilesActions/SaveSpectrumOfType</operation>
		<operation type="IsSavedOnCloud">AppSpectraFilesActions/IsSpectrumSavedOnCloud</operation>
+43 −0
Original line number Diff line number Diff line
@@ -29,6 +29,16 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.RemoteOperations
            this.logger = logger;
        }

        public async Task<bool> isLoginValidAsync()
        {
            return await getAsync(RemoteOperationType.IsLoginValid);
        }

        public async Task<bool> isTokenValidAsync()
        {
            return await getAsync(RemoteOperationType.IsTokenValid);
        }

        public async Task<bool> isSavedOnCloudAsync(IsFileSavedOnCloudRequestModel model)
        {
            return await postAsync(model, RemoteOperationType.IsSavedOnCloud);
@@ -50,6 +60,39 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.RemoteOperations
            return new HttpStringContent(JsonConvert.SerializeObject(objectToBeSerialized), Windows.Storage.Streams.UnicodeEncoding.Utf8, "application/json");
        }

        private async Task<bool> getAsync(RemoteOperationType remoteOperationType)
        {
            bool isOk = true;

            try
            {
                using (var httpClient = new HttpClient())
                {
                    JwtTokenModel tokenModel = settingsHelper.get<JwtTokenModel>(Constants.Constants.AUTHENTICATION_TOKEN);
                    httpClient.DefaultRequestHeaders.Authorization = new HttpCredentialsHeaderValue("Bearer", tokenModel.Token);

                    var url = remoteOperationsRepository.getUrl(remoteOperationType);
                    using (var response = await httpClient.GetAsync(new Uri(url)))
                    {
                        if (response.IsSuccessStatusCode)
                        {
                            var responseContent = await response.Content.ReadAsStringAsync();
                            //System.Diagnostics.Debug.WriteLine($"responseContent: {responseContent}");

                            isOk = JsonConvert.DeserializeObject<bool>(responseContent);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                isOk = false;
                logger.Write<RemoteOperationsManager>($"{nameof(postAsync)} - ex: {ex.Message}", Serilog.Events.LogEventLevel.Error);
            }

            return isOk;
        }

        private async Task<bool> postAsync(object model, RemoteOperationType remoteOperationType)
        {
            bool isOk = true;
+4 −0
Original line number Diff line number Diff line
@@ -10,16 +10,20 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.RemoteOperations
        public RemoteOperationsRepository()
        {
            RemoteOperations = new List<RemoteOperationModel>();
            IsInitialized = false;
        }

        public string BaseUrl { get; private set; }

        public bool IsInitialized { get; private set; }

        public List<RemoteOperationModel> RemoteOperations { get; private set; }

        public void add(string url,
                        string type)
        {
            RemoteOperations.Add(new RemoteOperationModel() { Url = url, Type = type.ToRemoteOperationType() });
            IsInitialized = false;
        }

        public string getUrl(RemoteOperationType type)
+3 −0
Original line number Diff line number Diff line
@@ -25,6 +25,9 @@ namespace INAF.Apps.Uwp.SLabDataManager.Helpers.RemoteOperations

        public async Task readXmlAsync()
        {
            if (remoteOperationsRepository.IsInitialized)
                return;

            logger.Write<RemoteOperationsXmlReader>($"Reading remote operations file...", Serilog.Events.LogEventLevel.Information);
            var file = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/xml/remoteoperations.xml"));

Loading