Beispiel #1
0
        //internal static StoreClient CreateStoreClient(ServerCommonOptions options, ILogger logger)
        //{
        //    try
        //    {
        //        var sc = new StoreClient(
        //               new StoreClientOptions
        //               {
        //                   ServerUrl = new Uri(options.ServerUrl)
        //               });
        //        return sc;
        //    }
        //    catch (Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        internal static async Task <string> GetTokenAsync(AuthCommonOptions options, ILogger logger)
        {
            var dotykMe = new DotykClient(
                new FileCertificateStorage(
                    Path.Combine(Environment.GetEnvironmentVariable("temp"), "Kodisoft", "DotykMe", "Certificates")));

            var token = GetStoredToken(dotykMe, logger);

            if (token == null)
            {
                logger?.LogTrace("Token not found in registry. Requesting new token.");

                for (int i = 0; i < 10; i++)
                {
                    string login,
                           password;
                    Dotyk.Extension.Windows.AuthForm authForm = new Dotyk.Extension.Windows.AuthForm();
                    if (authForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    {
                        login    = authForm.textBox1.Text;
                        password = authForm.textBox2.Text;
                    }
                    else
                    {
                        throw new Exception("login and Password are required");
                    }

                    try
                    {
                        logger?.LogTrace("Credentials read. Logging in");
                        await dotykMe.Login(new Me.Model.LoginModel {
                            Email = login, Password = password
                        });

                        logger?.LogTrace("Logged in to dotyk.me. Requesting token");
                        token = (await dotykMe.GetRestrictedToken(Me.Model.TokenDuration.Long)).Token;

                        logger?.LogTrace("Caching token");
                        Registry.SetValue(@"HKEY_CURRENT_USER\Software\Kodisoft\Dotyk\Store\DeploymentCLI", "AuthToken", token);

                        var valid = await dotykMe.ValidateTokenAsync(token);

                        logger?.LogInformation("Successfully logged in as {user}, token valid to {validTo}", valid.UserName, valid.ValidTo);
                        break;
                    }
                    catch (Exception ex)
                    {
                        logger?.LogError(ex.Message, "Failed to login");
                    }
                }
            }

            return(token);
        }
Beispiel #2
0
        private static string GetStoredToken(DotykClient dotykClient, ILogger logger)
        {
            var token = Registry.GetValue(@"HKEY_CURRENT_USER\Software\Kodisoft\Dotyk\Store\DeploymentCLI", "AuthToken", String.Empty)?.ToString();

            if (string.IsNullOrWhiteSpace(token))
            {
                return(null);
            }

            try
            {
                var res = dotykClient.ValidateToken(token);

                logger.LogInformation("Using cached dotyk.me token for user {username}", res.UserName);

                return(token);
            }
            catch (Exception ex)
            {
                logger.LogWarning(default(EventId), ex, "Failed to validate cached token");
                return(null);
            }
        }