public static PIEnrollResponse FromJSON(string json, PrivacyIDEA privacyIDEA)
        {
            if (string.IsNullOrEmpty(json))
            {
                if (privacyIDEA != null)
                {
                    privacyIDEA.Error("Json to parse is empty!");
                }
                return(null);
            }

            PIEnrollResponse ret = new PIEnrollResponse();

            ret.Raw = json;
            try
            {
                JObject jobj   = JObject.Parse(json);
                JToken  result = jobj["result"];
                if (result != null)
                {
                    ret.Status = (bool)result["status"];

                    JToken jVal = result["value"];
                    if (jVal != null)
                    {
                        ret.Value = (bool)jVal;
                    }

                    JToken error = result["error"];
                    if (error != null)
                    {
                        ret.ErrorCode    = (int)error["code"];
                        ret.ErrorMessage = (string)error["message"];
                    }
                }

                JToken detail = jobj["detail"];
                if (detail != null && detail.Type != JTokenType.Null)
                {
                    // ret.Type = (string)detail["type"];
                    ret.Serial = (string)detail["serial"];

                    JToken googleTotp = detail["googleurl"];
                    if (googleTotp != null && googleTotp.Type != JTokenType.Null)
                    {
                        ret.TotpUrl         = (string)googleTotp["value"];
                        ret.Base64TotpImage = (string)googleTotp["img"];
                    }
                }
            }
            catch (JsonException je)
            {
                if (privacyIDEA != null)
                {
                    privacyIDEA.Error(je);
                }
                return(null);
            }

            return(ret);
        }
示例#2
0
        public static PIResponse FromJSON(string json, PrivacyIDEA privacyIDEA)
        {
            if (string.IsNullOrEmpty(json))
            {
                if (privacyIDEA != null)
                {
                    privacyIDEA.Error("Json to parse is empty!");
                }
                return(null);
            }

            PIResponse ret = new PIResponse();

            ret.Raw = json;
            try
            {
                JObject jobj   = JObject.Parse(json);
                JToken  result = jobj["result"];
                if (result != null)
                {
                    ret.Status = (bool)result["status"];

                    JToken jVal = result["value"];
                    if (jVal != null)
                    {
                        ret.Value = (bool)jVal;
                    }

                    JToken error = result["error"];
                    if (error != null)
                    {
                        ret.ErrorCode    = (int)error["code"];
                        ret.ErrorMessage = (string)error["message"];
                    }
                }

                JToken detail = jobj["detail"];
                if (detail != null && detail.Type != JTokenType.Null)
                {
                    ret.TransactionID = (string)detail["transaction_id"];
                    ret.Message       = (string)detail["message"];
                    ret.Type          = (string)detail["type"];
                    ret.Serial        = (string)detail["serial"];

                    JArray multiChallenge = detail["multi_challenge"] as JArray;
                    if (multiChallenge != null)
                    {
                        foreach (JToken element in multiChallenge.Children())
                        {
                            string message       = (string)element["message"];
                            string transactionid = (string)element["transaction_id"];
                            string type          = (string)element["type"];
                            string serial        = (string)element["serial"];
                            if (type == "webauthn")
                            {
                                PIWebAuthnSignRequest tmp = new PIWebAuthnSignRequest();
                                JToken attr = element["attributes"];
                                tmp.WebAuthnSignRequest = attr["webAuthnSignRequest"].ToString(Formatting.None);
                                tmp.WebAuthnSignRequest.Replace("\n", "");
                                tmp.Message       = message;
                                tmp.Serial        = serial;
                                tmp.TransactionID = transactionid;
                                tmp.Type          = type;
                                ret.Challenges.Add(tmp);
                            }
                            else
                            {
                                PIChallenge tmp = new PIChallenge();
                                tmp.Message       = message;
                                tmp.Serial        = serial;
                                tmp.TransactionID = transactionid;
                                tmp.Type          = type;
                                ret.Challenges.Add(tmp);
                            }
                        }
                    }
                }
            }
            catch (JsonException je)
            {
                if (privacyIDEA != null)
                {
                    privacyIDEA.Error(je);
                }
                return(null);
            }

            return(ret);
        }