diff scid/scid.c @ 44:576f4b1ec79f

scid: implement API authentication
author David Demelier <markand@malikania.fr>
date Thu, 11 Aug 2022 21:24:07 +0200
parents 1d0ddf9e6efd
children 85c59fbf1407
line wrap: on
line diff
--- a/scid/scid.c	Thu Aug 11 11:34:32 2022 +0200
+++ b/scid/scid.c	Thu Aug 11 21:24:07 2022 +0200
@@ -28,16 +28,43 @@
 	.dbpath = VARDIR "/db/sci/sci.db"
 };
 
-void
-scid_init(void)
+static void
+init_misc(void)
 {
 	log_open("scid: version " VERSION);
 	log_info("scid: opening database %s", scid.dbpath);
 
+	theme_open(scid.themedir);
+}
+
+static void
+init_database(void)
+{
 	if (db_open(scid.dbpath) < 0)
-		log_die("scid: abort: unable to open database");
+		log_die("scid: unable to open database");
+
+	/* Retrieve or create the API key if not existing. */
+	switch (db_key_get(scid.apikey, sizeof (scid.apikey))) {
+	case 0:
+		/* Not found, create immediately. */
+		if (db_key_init(scid.apikey, sizeof (scid.apikey)) < 0)
+			log_die("scid: unable to insert key");
 
-	theme_open(scid.themedir);
+		break;
+	case -1:
+		log_die("scid: unable to retrieve API key in database");
+		break;
+	default:
+		/* We already fetched it. */
+		break;
+	}
+}
+
+void
+scid_init(void)
+{
+	init_misc();
+	init_database();
 }
 
 void