d0976c9d524ee428053266757aa17c7ef1b66a12
chmalee
  Wed Feb 18 12:20:45 2026 -0800
Libify the api key set/revoke methods, refs #36517

diff --git src/hg/hgHubConnect/hgHubConnect.c src/hg/hgHubConnect/hgHubConnect.c
index 2ebe53860ac..6a80d7399fe 100644
--- src/hg/hgHubConnect/hgHubConnect.c
+++ src/hg/hgHubConnect/hgHubConnect.c
@@ -417,56 +417,45 @@
         jsInlineF("%s", line);
     pipelineClose(&pl);
     // the 'false' below prevents a few hub-search specific jstree configuration options
     jsInline("hubSearchTree.init(false);");
     }
 errCatchEnd(errCatch);
 if (errCatch->gotError || errCatch->gotWarning)
     {
     printf("hubCheck timed out after running for %d minute%s. Please try on a Unix command line", hubCheckTimeout / 60, hubCheckTimeout/60 > 1 ? "s" : "");
     ret = 1;
     }
 errCatchFree(&errCatch);
 return ret;
 }
 
-static char *getApiKey(char *userName)
-/* Grab the already created api key if it exists */
-{
-char *tableName = cfgOptionDefault("authTableName", AUTH_TABLE_DEFAULT);
-struct sqlConnection *conn = hConnectCentral();
-struct dyString *query = sqlDyStringCreate("select apiKey from %s where userName='%s'", tableName, userName);
-char *apiKey = sqlQuickString(conn, dyStringCannibalize(&query));
-hDisconnectCentral(&conn);
-return apiKey;
-}
-
 void printApiKeySection()
 {
 puts("<div id='apiKeySection' class='tabSection'>");
 puts("<h4>API key</h4>");
 char *userName = wikiLinkUserName();
 char *userId = wikiLinkUserId();
 if (userName==NULL || userId==NULL)
     {
     char *hgsid = cartSessionId(cart);
     char *loginUrl = wikiLinkUserLoginUrlReturning(hgsid, wikiLinkEncodeReturnUrl(hgsid, "hgHubConnect", "#dev"));
     printf("<div class='help'>You are not logged in. Please <a href='%s'>Login</a> now, then this page will show the API key.</div>", loginUrl);
     }
 else
     {
-    char *existingKey = getApiKey(userName);
+    char *existingKey = hubSpaceGetApiKey(userName);
     if (existingKey)
         {
         puts("<div id='apiKeyInstructions' class='help'>You have <span id='removeOnGenerate'>already</span> generated an API key. If you would like to generate a new key (which revokes old keys), click 'Generate key'. To use your API key with Hubtools, copy and paste the below key to your ~/.hubtools.conf file. This is not necessary for URL use to <a href='/FAQ/FAQdownloads.html#CAPTCHA'>bypass the CAPTCHA</a>.<br><br>");
         puts("<div id='apiKey' style='margin-left: 15px; font-family: monospace'>");
         printf("%s\n", existingKey);
         puts("</div>");
         puts("</div>");
         puts("<div id='generateDiv' class='help'>Generate an API key <button id='generateApiKey'>Generate key</button></div>");
         }
     else
         {
         puts("<div id='generateDiv' class='help'>Generate an API key to <a href='/FAQ/FAQdownloads.html#CAPTCHA'>bypass the CAPTCHA</a> or use the <tt>hubtools up</tt> command");
         puts("<button id='generateApiKey'>Generate key</button></div>");
         printf("<div id='apiKeyInstructions' style='display: %s'>To use your API key with hubtools, create a file ~/.hubtools.conf and add the key. This is not necessary for URL use to <a href='/FAQ/FAQdownloads.html#CAPTCHA'>bypass the CAPTCHA</a>.<br><br>\n", existingKey != NULL ? "block" : "none");
         puts("<div id='apiKey' style='margin-left: 15px; font-family: monospace'>");
@@ -1757,32 +1746,32 @@
 
 printf("</div>"); // #tabs
 
 
 cartWebEnd();
 }
 
 void doAsync(struct cart *theCart)
 /* Execute the async request */
 {
 cart = theCart;
 struct cartJson *cj = cartJsonNew(cart);
 cartJsonRegisterHandler(cj, hgHubGetHubSpaceUIState, getHubSpaceUIState);
 cartJsonRegisterHandler(cj, hgHubDeleteFile, doRemoveFile);
 cartJsonRegisterHandler(cj, hgHubMoveFile, doMoveFile);
-cartJsonRegisterHandler(cj, hgHubGenerateApiKey, generateApiKey);
-cartJsonRegisterHandler(cj, hgHubRevokeApiKey, revokeApiKey);
+cartJsonRegisterHandler(cj, hgHubGenerateApiKey, cjGenerateApiKey);
+cartJsonRegisterHandler(cj, hgHubRevokeApiKey, cjRevokeApiKey);
 cartJsonExecute(cj);
 }
 
 char *excludeVars[] = {"Submit", "submit", "hc_one_url", hgHubDoHubCheck,
     hgHubCheckUrl, hgHubDoClear, hgHubDoRefresh, hgHubDoDisconnect,hgHubDoRedirect, hgHubDataText, 
     hgHubConnectRemakeTrackHub, NULL};
 
 int main(int argc, char *argv[])
 /* Process command line. */
 {
 long enteredMainTime = clock1000();
 
 oldVars = hashNew(10);
 cgiSpoof(&argc, argv);
 if (cfgOptionBooleanDefault("storeUserFiles", FALSE) && cgiOptionalString(CARTJSON_COMMAND))