/// <summary> /// Enroll TOTP Token for specified user if user does not already have token /// </summary> /// <param name="user">username</param> /// <param name="domain">optional domain which can be mapped to a privacyIDEA realm</param> /// <returns>PIEnrollResponse object or null on error</returns> public PIEnrollResponse TokenInit(string user, string domain = null) { var parameters = new Dictionary <string, string> { { "user", user }, { "type", "totp" }, { "genkey", "1" } }; AddRealmForDomain(domain, parameters); string response = SendRequest("/token/init", parameters, new List <KeyValuePair <string, string> >()); return(PIEnrollResponse.FromJSON(response, this)); }
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); }