parse_login_reply_challenge (GHashTable *headers,
                             gchar **out_id,
                             gchar **out_prompt)
{
  gchar *original = NULL;
  gchar *line;
  gchar *next;
  gchar *id = NULL;
  gchar *prompt = NULL;
  gboolean ret = FALSE;
  gpointer key = NULL;
  gsize length;

  if (!g_hash_table_lookup_extended (headers, "WWW-Authenticate", &key, (gpointer *)&original))
    goto out;

  line = original;

  // Check challenge type
  line = str_skip (line, ' ');
  if (g_ascii_strncasecmp (line, "X-Conversation ", strlen("X-Conversation ")) != 0)
    goto out;

  next = strchr (line, ' ');
  if (!next)
    goto out;

  // Get id
  line = next;
  line = str_skip (line, ' ');
  next = strchr (line, ' ');
  if (!next)
    goto out;
  id = g_strndup (line, next - line);

  // Rest should be the base64 prompt
  next = str_skip (next, ' ');
  prompt = g_strdup (next);
  if (g_base64_decode_inplace (prompt, &length) == NULL)
    goto out;
  prompt[length] = '\0';
  ret = TRUE;

out:
  if (ret)
    {
      *out_id = id;
      *out_prompt = prompt;
    }
  else
    {
      g_warning ("Got invalid WWW-Authenticate header: %s", original);
      g_free (id);
      g_free (prompt);
    }

  return ret;
}
parse_login_reply_challenge (GHashTable *headers,
                             gchar **out_id,
                             gchar **out_prompt)
{
  gchar *original = NULL;
  gchar *line;
  gchar *next;
  gchar *id = NULL;
  gchar *prompt = NULL;
  gboolean ret = FALSE;
  gpointer key = NULL;
  gsize length;

  if (!g_hash_table_lookup_extended (headers, "WWW-Authenticate", &key, (gpointer *)&original))
    goto out;

  line = original;

   
  line = str_skip (line, ' ');
  if (g_ascii_strncasecmp (line, "X-Conversation ", strlen("X-Conversation ")) != 0)
    goto out;

  next = strchr (line, ' ');
  if (!next)
    goto out;

   
  line = next;
  line = str_skip (line, ' ');
  next = strchr (line, ' ');
  if (!next)
    goto out;
  id = g_strndup (line, next - line);

   
  next = str_skip (next, ' ');
  prompt = g_strdup (next);
  if (g_base64_decode_inplace (prompt, &length) == NULL)
    goto out;
  prompt[length] = '\0';
  ret = TRUE;

out:
  if (ret)
    {
      *out_id = id;
      *out_prompt = prompt;
    }
  else
    {
      g_warning ("Got invalid WWW-Authenticate header: %s", original);
      g_free (id);
      g_free (prompt);
    }

  return ret;
}