diff --git a/src/Core/Utilities/DuoApi.cs b/src/Core/Utilities/DuoApi.cs index aff938e14..0aa906f8b 100644 --- a/src/Core/Utilities/DuoApi.cs +++ b/src/Core/Utilities/DuoApi.cs @@ -175,22 +175,21 @@ namespace Bit.Core.Utilities.Duo var res = ApiCall(method, path, parameters, timeout, out var statusCode); try { + // TODO: We should deserialize this into our own DTO and not work on dictionaries. var dict = JsonSerializer.Deserialize>(res); - if (dict["stat"] as string == "OK") + if (dict["stat"].ToString() == "OK") { - return dict["response"] as T; + return JsonSerializer.Deserialize(dict["response"].ToString()); } - else + + var check = ToNullableInt(dict["code"].ToString()); + var code = check.GetValueOrDefault(0); + var messageDetail = string.Empty; + if (dict.ContainsKey("message_detail")) { - var check = dict["code"] as int?; - var code = check.GetValueOrDefault(0); - var messageDetail = string.Empty; - if (dict.ContainsKey("message_detail")) - { - messageDetail = dict["message_detail"] as string; - } - throw new ApiException(code, (int)statusCode, dict["message"] as string, messageDetail); + messageDetail = dict["message_detail"].ToString(); } + throw new ApiException(code, (int)statusCode, dict["message"].ToString(), messageDetail); } catch (ApiException) { @@ -202,6 +201,16 @@ namespace Bit.Core.Utilities.Duo } } + private int? ToNullableInt(string s) + { + int i; + if (int.TryParse(s, out i)) + { + return i; + } + return null; + } + private string HmacSign(string data) { var keyBytes = Encoding.ASCII.GetBytes(_skey);