rtsp-auth: Add support for Digest authentication
[platform/upstream/gstreamer.git] / examples / test-record-auth.c
1 /* GStreamer
2  * Copyright (C) 2008 Wim Taymans <wim.taymans at gmail.com>
3  * Copyright (C) 2015 Centricular Ltd
4  *     Author: Sebastian Dröge <sebastian@centricular.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  */
21
22 #include <gst/gst.h>
23
24 #include <gst/rtsp-server/rtsp-server.h>
25
26 /* define this if you want the server to use TLS */
27 //#define WITH_TLS
28
29 #define DEFAULT_RTSP_PORT "8554"
30
31 static char *port = (char *) DEFAULT_RTSP_PORT;
32
33 static GOptionEntry entries[] = {
34   {"port", 'p', 0, G_OPTION_ARG_STRING, &port,
35       "Port to listen on (default: " DEFAULT_RTSP_PORT ")", "PORT"},
36   {NULL}
37 };
38
39 int
40 main (int argc, char *argv[])
41 {
42   GMainLoop *loop;
43   GstRTSPServer *server;
44   GstRTSPMountPoints *mounts;
45   GstRTSPMediaFactory *factory;
46   GOptionContext *optctx;
47   GError *error = NULL;
48   GstRTSPAuth *auth;
49   GstRTSPToken *token;
50   gchar *basic;
51 #ifdef WITH_TLS
52   GTlsCertificate *cert;
53 #endif
54
55   optctx = g_option_context_new ("<launch line> - Test RTSP Server, Launch\n\n"
56       "Example: \"( decodebin name=depay0 ! autovideosink )\"");
57   g_option_context_add_main_entries (optctx, entries, NULL);
58   g_option_context_add_group (optctx, gst_init_get_option_group ());
59   if (!g_option_context_parse (optctx, &argc, &argv, &error)) {
60     g_printerr ("Error parsing options: %s\n", error->message);
61     return -1;
62   }
63
64   if (argc < 2) {
65     g_print ("%s\n", g_option_context_get_help (optctx, TRUE, NULL));
66     return 1;
67   }
68   g_option_context_free (optctx);
69
70   loop = g_main_loop_new (NULL, FALSE);
71
72   /* create a server instance */
73   server = gst_rtsp_server_new ();
74   g_object_set (server, "service", port, NULL);
75
76   /* get the mount points for this server, every server has a default object
77    * that be used to map uri mount points to media factories */
78   mounts = gst_rtsp_server_get_mount_points (server);
79
80   /* make a media factory for a test stream. The default media factory can use
81    * gst-launch syntax to create pipelines.
82    * any launch line works as long as it contains elements named depay%d. Each
83    * element with depay%d names will be a stream */
84   factory = gst_rtsp_media_factory_new ();
85   gst_rtsp_media_factory_set_transport_mode (factory,
86       GST_RTSP_TRANSPORT_MODE_RECORD);
87   gst_rtsp_media_factory_set_launch (factory, argv[1]);
88   gst_rtsp_media_factory_set_latency (factory, 2000);
89 #ifdef WITH_TLS
90   gst_rtsp_media_factory_set_profiles (factory,
91       GST_RTSP_PROFILE_SAVP | GST_RTSP_PROFILE_SAVPF);
92 #else
93   gst_rtsp_media_factory_set_profiles (factory,
94       GST_RTSP_PROFILE_AVP | GST_RTSP_PROFILE_AVPF);
95 #endif
96
97   /* allow user to access this resource */
98   gst_rtsp_media_factory_add_role (factory, "user",
99       GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
100       GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
101   /* Anonymous users can see but not construct, so get UNAUTHORIZED */
102   gst_rtsp_media_factory_add_role (factory, "anonymous",
103       GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
104       GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, FALSE, NULL);
105
106   /* attach the test factory to the /test url */
107   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
108
109   /* don't need the ref to the mapper anymore */
110   g_object_unref (mounts);
111
112   /* Set up the auth for user account */
113   /* make a new authentication manager */
114   auth = gst_rtsp_auth_new ();
115 #ifdef WITH_TLS
116   cert = g_tls_certificate_new_from_pem ("-----BEGIN CERTIFICATE-----"
117       "MIICJjCCAY+gAwIBAgIBBzANBgkqhkiG9w0BAQUFADCBhjETMBEGCgmSJomT8ixk"
118       "ARkWA0NPTTEXMBUGCgmSJomT8ixkARkWB0VYQU1QTEUxHjAcBgNVBAsTFUNlcnRp"
119       "ZmljYXRlIEF1dGhvcml0eTEXMBUGA1UEAxMOY2EuZXhhbXBsZS5jb20xHTAbBgkq"
120       "hkiG9w0BCQEWDmNhQGV4YW1wbGUuY29tMB4XDTExMDExNzE5NDcxN1oXDTIxMDEx"
121       "NDE5NDcxN1owSzETMBEGCgmSJomT8ixkARkWA0NPTTEXMBUGCgmSJomT8ixkARkW"
122       "B0VYQU1QTEUxGzAZBgNVBAMTEnNlcnZlci5leGFtcGxlLmNvbTBcMA0GCSqGSIb3"
123       "DQEBAQUAA0sAMEgCQQDYScTxk55XBmbDM9zzwO+grVySE4rudWuzH2PpObIonqbf"
124       "hRoAalKVluG9jvbHI81eXxCdSObv1KBP1sbN5RzpAgMBAAGjIjAgMAkGA1UdEwQC"
125       "MAAwEwYDVR0lBAwwCgYIKwYBBQUHAwEwDQYJKoZIhvcNAQEFBQADgYEAYx6fMqT1"
126       "Gvo0jq88E8mc+bmp4LfXD4wJ7KxYeadQxt75HFRpj4FhFO3DOpVRFgzHlOEo3Fwk"
127       "PZOKjvkT0cbcoEq5whLH25dHoQxGoVQgFyAP5s+7Vp5AlHh8Y/vAoXeEVyy/RCIH"
128       "QkhUlAflfDMcrrYjsmwoOPSjhx6Mm/AopX4="
129       "-----END CERTIFICATE-----"
130       "-----BEGIN PRIVATE KEY-----"
131       "MIIBVAIBADANBgkqhkiG9w0BAQEFAASCAT4wggE6AgEAAkEA2EnE8ZOeVwZmwzPc"
132       "88DvoK1ckhOK7nVrsx9j6TmyKJ6m34UaAGpSlZbhvY72xyPNXl8QnUjm79SgT9bG"
133       "zeUc6QIDAQABAkBRFJZ32VbqWMP9OVwDJLiwC01AlYLnka0mIQZbT/2xq9dUc9GW"
134       "U3kiVw4lL8v/+sPjtTPCYYdzHHOyDen6znVhAiEA9qJT7BtQvRxCvGrAhr9MS022"
135       "tTdPbW829BoUtIeH64cCIQDggG5i48v7HPacPBIH1RaSVhXl8qHCpQD3qrIw3FMw"
136       "DwIga8PqH5Sf5sHedy2+CiK0V4MRfoU4c3zQ6kArI+bEgSkCIQCLA1vXBiE31B5s"
137       "bdHoYa1BXebfZVd+1Hd95IfEM5mbRwIgSkDuQwV55BBlvWph3U8wVIMIb4GStaH8"
138       "W535W8UBbEg=" "-----END PRIVATE KEY-----", -1, &error);
139   if (cert == NULL) {
140     g_printerr ("failed to parse PEM: %s\n", error->message);
141     return -1;
142   }
143   gst_rtsp_auth_set_tls_certificate (auth, cert);
144   g_object_unref (cert);
145 #endif
146
147   /* make default token - anonymous unauthenticated access */
148   token =
149       gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
150       "anonymous", NULL);
151   gst_rtsp_auth_set_default_token (auth, token);
152   gst_rtsp_token_unref (token);
153
154   /* make user token */
155   token =
156       gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
157       "user", NULL);
158   basic = gst_rtsp_auth_make_basic ("user", "password");
159   gst_rtsp_auth_add_basic (auth, basic, token);
160   g_free (basic);
161   gst_rtsp_token_unref (token);
162
163   /* set as the server authentication manager */
164   gst_rtsp_server_set_auth (server, auth);
165   g_object_unref (auth);
166
167   /* attach the server to the default maincontext */
168   gst_rtsp_server_attach (server, NULL);
169
170   /* start serving */
171 #ifdef WITH_TLS
172   g_print ("stream ready at rtsps://127.0.0.1:%s/test\n", port);
173 #else
174   g_print ("stream ready at rtsp://127.0.0.1:%s/test\n", port);
175 #endif
176   g_main_loop_run (loop);
177
178   return 0;
179 }