/// <summary>
        /// Logs in the user with the provided credentials.
        /// </summary>
        private void Login()
        {
            // Login the user
            System.Diagnostics.Debug.WriteLine("{0}:{1}", this.UserName, this.Password);

            // If already logging in
            if (this.IsBusy)
            {
                return;
            }

            // Validate that the user name is a valid email
            var user    = this.UserName.Trim().ToLower();
            var isEmail = Regex.IsMatch(
                user,
                @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z",
                RegexOptions.IgnoreCase);

            if (!isEmail)
            {
                App.DisplayAlert(
                    Localization.ErrorDialogTitle,
                    Localization.ErrorInvalidUserLogin,
                    Localization.DialogDismiss);
                return;
            }

            // Validate that the password is present
            if (this.Password.Length <= 2)
            {
                App.DisplayAlert(
                    Localization.ErrorDialogTitle,
                    Localization.ErrorInvalidPassword,
                    Localization.DialogDismiss);
                return;
            }

            // Prepare the data to be send to the server
            var deviceId = ((App)Application.Current).DeviceId;
            var request  = new Json.JsonObject
            {
                { "grant_type", "password" },
                { "username", user },
                { "password", this.Password },
                { "scope", "user submit-report" },
                { "device", deviceId }
            };

            // If push token exists
            var pushToken = ((App)Application.Current).PushToken;

            if (!string.IsNullOrEmpty(pushToken))
            {
                request.Add("push_token", pushToken);
            }

            // Setup error handlers
            // - If session is already opened by another device, request user consent
            var handlers = new Dictionary <System.Net.HttpStatusCode, Action <JsonValue> >
            {
                { System.Net.HttpStatusCode.Conflict, resp => this.RetryLogin(request, resp) }
            };

            // Send request to the server
            this.IsBusy = true;
            WebHelper.SendAsync(
                Uris.GetLoginUri(),
                request.AsHttpContent(),
                this.ProcessLoginResult,
                () => this.IsBusy = false,
                handlers);
        }
Ejemplo n.º 2
0
        public static Config ConvertObject(string version, string json)
        {
            var v = new Client.Version(2, 0, 0, Client.VersionType.alpha, 0);

            if (!string.IsNullOrWhiteSpace(version))
            {
                Client.Version.TryParse(version, out v);
            }
            Debug.WriteLine($"Config version change: {v} => {Constant.Verison}");
            switch (v.ToString())
            {
            case "2.0.0-alpha.0": {
                var o = Json.JsonSerializer.Convert <v200a1.ScriptObject>(json);
                Object.KeyObjects keys = new Object.KeyObjects();
                if (o?.Keys?.Keys != null)
                {
                    foreach (var item in o.Keys.Keys)
                    {
                        if (item == null)
                        {
                            continue;
                        }
                        keys.Add(new Object.KeyObject()
                            {
                                Ignore  = item.Ignore,
                                Key     = item.Key,
                                Scripts = new Object.KeyScript(item.Script?.Scripts)
                            });
                    }
                }
                return(ConvertObject("2.0.0-alpha.1", Json.JsonSerializer.Convert(new v200a2.ScriptObject()
                    {
                        Language = o.Language, Keys = keys, Settings = o.Settings
                    })));
            }

            case "2.0.0-alpha.1": {
                var o        = Json.JsonSerializer.Convert <v200a2.ScriptObject>(json);
                var settings = new Json.JsonObject();

                if (o.Settings != null)
                {
                    foreach (var item in o.Settings)
                    {
                        settings.Add(item.Key, ConvertKeys(item.Value));
                    }

                    Json.Keys ConvertKeys(v200a2.JsonObject.Keys keys)
                    {
                        var ks = new Json.Keys();

                        foreach (var item in keys)
                        {
                            ks.Add(item.Key, ConvertKey(item.Value));
                        }
                        return(ks);
                    }

                    Json.Key ConvertKey(v200a2.JsonObject.Key key) => new Json.Key
                    {
                        SubKeys = ConvertKeys(key.SubKeys),
                        Values  = ConvertValues(key.Values)
                    };

                    Json.Values ConvertValues(v200a2.JsonObject.Values values)
                    {
                        var vs = new Json.Values();

                        foreach (var item in values)
                        {
                            vs.Add(item.Key, new Json.Value(item.Value?.Data));
                        }
                        return(vs);
                    }
                }


                return(ConvertObject("2.0.0-alpha.2", Json.JsonSerializer.Convert(new v200a3.ScriptObject()
                    {
                        Language = o.Language,
                        Keys = o.Keys,
                        Settings = settings
                    })));
            }

            case "2.0.0-alpha.2": {
                var o = Json.JsonSerializer.Convert <v200a3.ScriptObject>(json);

                return(new Config()
                    {
                        Keys = o.Keys,
                        Configs = o.Settings,
                        Language = o.Language
                    });
            }

            default:
                return(Json.JsonSerializer.Convert <Config>(json));
            }
        }