rtsp-stream: Update comment and ASCII art to the latest code
[platform/upstream/gstreamer.git] / examples / test-auth.c
index 15843d1..eff3c74 100644 (file)
@@ -64,7 +64,6 @@ main (int argc, char *argv[])
   GstRTSPAuth *auth;
   GstRTSPToken *token;
   gchar *basic;
-  GstStructure *s;
 
   gst_init (&argc, &argv);
 
@@ -88,10 +87,22 @@ main (int argc, char *argv[])
       "x264enc ! rtph264pay name=pay0 pt=96 "
       "audiotestsrc ! audio/x-raw,rate=8000 ! "
       "alawenc ! rtppcmapay name=pay1 pt=97 " ")");
-
   /* attach the test factory to the /test url */
   gst_rtsp_mount_points_add_factory (mounts, "/test", factory);
 
+  /* allow user and admin to access this resource */
+  gst_rtsp_media_factory_add_role (factory, "user",
+      GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
+      GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
+  gst_rtsp_media_factory_add_role (factory, "admin",
+      GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
+      GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
+  /* admin2 can look at the media but not construct so he gets a
+   * 401 Unauthorized */
+  gst_rtsp_media_factory_add_role (factory, "admin2",
+      GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
+      GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, FALSE, NULL);
+
   /* make another factory */
   factory = gst_rtsp_media_factory_new ();
   gst_rtsp_media_factory_set_launch (factory, "( "
@@ -100,34 +111,48 @@ main (int argc, char *argv[])
   /* attach the test factory to the /test url */
   gst_rtsp_mount_points_add_factory (mounts, "/test2", factory);
 
+  /* allow admin2 to access this resource */
+  /* user and admin have no permissions so they can't even see the
+   * media and get a 404 Not Found */
+  gst_rtsp_media_factory_add_role (factory, "admin2",
+      GST_RTSP_PERM_MEDIA_FACTORY_ACCESS, G_TYPE_BOOLEAN, TRUE,
+      GST_RTSP_PERM_MEDIA_FACTORY_CONSTRUCT, G_TYPE_BOOLEAN, TRUE, NULL);
+
   /* don't need the ref to the mapper anymore */
   g_object_unref (mounts);
 
   /* make a new authentication manager */
   auth = gst_rtsp_auth_new ();
 
+  /* make default token, it has the same permissions as admin2 */
+  token =
+      gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
+      "admin2", NULL);
+  gst_rtsp_auth_set_default_token (auth, token);
+  gst_rtsp_token_unref (token);
+
   /* make user token */
-  token = gst_rtsp_token_new ();
-  s = gst_rtsp_token_writable_structure (token);
-  gst_structure_set (s, "manager.cgroup", G_TYPE_STRING, "user", NULL);
+  token =
+      gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
+      "user", NULL);
   basic = gst_rtsp_auth_make_basic ("user", "password");
   gst_rtsp_auth_add_basic (auth, basic, token);
   g_free (basic);
   gst_rtsp_token_unref (token);
 
   /* make admin token */
-  token = gst_rtsp_token_new ();
-  s = gst_rtsp_token_writable_structure (token);
-  gst_structure_set (s, "manager.cgroup", G_TYPE_STRING, "admin", NULL);
+  token =
+      gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
+      "admin", NULL);
   basic = gst_rtsp_auth_make_basic ("admin", "power");
   gst_rtsp_auth_add_basic (auth, basic, token);
   g_free (basic);
   gst_rtsp_token_unref (token);
 
   /* make admin2 token */
-  token = gst_rtsp_token_new ();
-  s = gst_rtsp_token_writable_structure (token);
-  gst_structure_set (s, "manager.cgroup", G_TYPE_STRING, "admin", NULL);
+  token =
+      gst_rtsp_token_new (GST_RTSP_TOKEN_MEDIA_FACTORY_ROLE, G_TYPE_STRING,
+      "admin2", NULL);
   basic = gst_rtsp_auth_make_basic ("admin2", "power2");
   gst_rtsp_auth_add_basic (auth, basic, token);
   g_free (basic);