[libsoup] docs: Rewrite client certificate example



commit d4171ba3a3b9cbd1e7b9d368f76db93881cd8366
Author: Patrick Griffis <pgriffis igalia com>
Date:   Wed Jul 14 15:01:29 2021 -0500

    docs: Rewrite client certificate example

 docs/reference/client-tls.xml | 70 ++++++++++++-------------------------------
 1 file changed, 19 insertions(+), 51 deletions(-)
---
diff --git a/docs/reference/client-tls.xml b/docs/reference/client-tls.xml
index 6f80b112..2e329e65 100644
--- a/docs/reference/client-tls.xml
+++ b/docs/reference/client-tls.xml
@@ -57,56 +57,15 @@ int main (int argc, char **argv)
   <sect2>
     <title>Using Client Certificates</title>
 <informalexample><programlisting><![CDATA[
-// We must create a custom GTlsInteraction so lets do that inline
-typedef struct _GetTlsCertInteraction        GetTlsCertInteraction;
-typedef struct _GetTlsCertInteractionClass   GetTlsCertInteractionClass;
-
-static GType                    _get_tls_cert_interaction_get_type    (void) G_GNUC_CONST;
-static GetTlsCertInteraction *  _get_tls_cert_interaction_new         (GTlsCertificate *cert);
-
-struct _GetTlsCertInteraction
-{
-       GTlsInteraction parent_instance;
-       GTlsCertificate *cert;
-};
-
-struct _GetTlsCertInteractionClass
-{
-       GTlsInteractionClass parent_class;
-};
-
-G_DEFINE_TYPE (GetTlsCertInteraction, _get_tls_cert_interaction, G_TYPE_TLS_INTERACTION);
-
-static GTlsInteractionResult
-request_certificate (GTlsInteraction              *interaction,
-                     GTlsConnection               *connection,
-                     GTlsCertificateRequestFlags   flags,
-                     GCancellable                 *cancellable,
-                     GError                      **error)
-{
-       GetTlsCertInteraction *self = (GetTlsCertInteraction*)interaction;
-       g_tls_connection_set_certificate (connection, self->cert);
-       return G_TLS_INTERACTION_HANDLED;
-}
-
-static void
-_get_tls_cert_interaction_init (GetTlsCertInteraction *interaction)
+static gboolean
+on_request_certificate (SoupMessage *msg, GTlsClientConnection *conn, gpointer user_data)
 {
-}
+    GTlsCertificate *client_cert = user_data;
 
-static void
-_get_tls_cert_interaction_class_init (GetTlsCertInteractionClass *klass)
-{
-       GTlsInteractionClass *interaction_class = G_TLS_INTERACTION_CLASS (klass);
-       interaction_class->request_certificate = request_certificate;
-}
+    /* We immediately set this however you can set this later in an async function. */
+    soup_message_set_tls_client_certificate (msg, client_cert);
 
-GetTlsCertInteraction *
-_get_tls_cert_interaction_new (GTlsCertificate *cert)
-{
-       GetTlsCertInteraction *self = g_object_new (_get_tls_cert_interaction_get_type (), NULL);
-       self->cert = g_object_ref (cert);
-       return self;
+    return TRUE; /* We handled the request */
 }
 
 int main (int argc, char **argv)
@@ -120,12 +79,21 @@ int main (int argc, char **argv)
         return 1;
     }
 
-    GTlsInteraction *interaction = _get_tls_cert_interaction_new (cert);
-    SoupSession *session = soup_session_new_with_options ("tls-interaction", interaction, NULL);
+    SoupSession *session = soup_session_new ();
+    SoupMessage *msg = soup_message_new ("GET", "https://example.org";);
+
+    /* We can set the certificate ahead of time if we already have one */
+    // soup_message_set_tls_client_certificate (msg, client_cert)
+
+    /* However we can also dynamically request one which is useful in
+     * applications that show a graphical prompt for example. */
+    g_signal_connect (msg, "request-certificate",
+                      G_CALLBACK (on_request_certificate), client_cert);
 
-    // Send a message
+    // Send the message...
 
-    g_object_unref (interaction);
+    g_object_unref (msg);
+    g_object_unref (session);
     g_object_unref (client_cert);
     return 0;
 }]]>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]