e8ffc5e45bd8e8de9f1c78347ea6b11fd4817a40
chmalee
  Mon Dec 2 12:37:53 2024 -0800
Generate api keys for each user and store them in hgcentral.hubSpaceKeys

diff --git src/hg/hgHubConnect/hooks/pre-create.c src/hg/hgHubConnect/hooks/pre-create.c
index 577acbb..bd27714 100644
--- src/hg/hgHubConnect/hooks/pre-create.c
+++ src/hg/hgHubConnect/hooks/pre-create.c
@@ -4,30 +4,31 @@
  * to be uploaded file will not exceed the quota. Regardless
  * of success, return a JSON encoded response back to the
  * client. */
 #include "common.h"
 #include "linefile.h"
 #include "hash.h"
 #include "options.h"
 #include "wikiLink.h"
 #include "customTrack.h"
 #include "userdata.h"
 #include "jsonQuery.h"
 #include "jsHelper.h"
 #include "errCatch.h"
 #include "obscure.h"
 #include "hooklib.h"
+#include "hubSpaceKeys.h"
 
 void usage()
 /* Explain usage and exit. */
 {
 errAbort(
   "pre-create - tus daemon pre-create hook program\n"
   "usage:\n"
   "   pre-create < input\n"
   );
 }
 
 /* Command line validation table. */
 static struct optionSpec options[] = {
    {NULL, 0},
 };
@@ -56,57 +57,58 @@
     struct errCatch *errCatch = errCatchNew(0);
     if (errCatchStart(errCatch))
         {
         struct lineFile *lf = lineFileStdin(FALSE);
         char *request = lineFileReadAll(lf);
         struct jsonElement *req = jsonParse(request);
         fprintf(stderr, "Hook request:\n");
         jsonPrintToFile(req, NULL, stderr, 0);
         char *reqCookie= jsonQueryString(req, "", "Event.HTTPRequest.Header.Cookie[0]", NULL);
         if (reqCookie)
             {
             setenv("HTTP_COOKIE", reqCookie, 0);
             }
         fprintf(stderr, "reqCookie='%s'\n", reqCookie);
         char *userName = getUserName();
-        fprintf(stderr, "userName='%s'\n'", userName);
         if (!userName)
             {
+            // maybe an apiKey was provided, use that instead to look up the userName
+            char *apiKey = jsonQueryString(req, "", "Event.Upload.MetaData.apiKey", NULL);
+            userName = userNameForApiKey(apiKey);
+            if (!userName)
                 errAbort("You are not logged in. Please navigate to My Data -> My Sessions and log in or create an account.");
             }
-        else
-            {
+        fprintf(stderr, "userName='%s'\n'", userName);
         long reqFileSize = jsonQueryInt(req, "", "Event.Upload.Size", 0, NULL);
         char *reqFileName = jsonQueryString(req, "", "Event.Upload.MetaData.fileName", NULL);
         long currQuota = checkUserQuota(userName);
         long newQuota = currQuota + reqFileSize;
         long maxQuota = getMaxUserQuota(userName);
         if (newQuota > maxQuota)
             {
             errAbort("File '%s' is too large, need %s free space but current used space is %s out of %s", reqFileName, prettyFileSize(reqFileSize), prettyFileSize(currQuota), prettyFileSize(maxQuota));
             }
         char *reqFileType = jsonQueryString(req, "", "Event.Upload.MetaData.fileType", NULL);
         if (!isFileTypeRecognized(reqFileType))
             {
             errAbort("File type '%s' for file '%s' is not accepted at this time", reqFileType, reqFileName);
             }
         char *reqGenome = jsonQueryString(req, "", "Event.Upload.MetaData.genome", NULL);
         if (!reqGenome)
             {
             errAbort("Genome selection '%s' for file '%s' is invalid. Please choose the correct genome", reqGenome, reqFileName);
             }
-            }
 
         // we've passed all the checks so we can return that we are good to upload the file
         if (exitStatus == 0)
             fillOutHttpResponseSuccess(response);
         }
     if (errCatch->gotError)
         {
         rejectUpload(response, errCatch->message->string);
         exitStatus = 1;
         }
     errCatchEnd(errCatch);
     }
 // always print a response no matter what
 jsonPrintToFile(response, NULL, stdout, 0);
 return 0;