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/hgHubConnect.c src/hg/hgHubConnect/hgHubConnect.c index e9fdd0d..2daf923 100644 --- src/hg/hgHubConnect/hgHubConnect.c +++ src/hg/hgHubConnect/hgHubConnect.c @@ -18,33 +18,34 @@ #include "cart.h" #include "dbDb.h" #include "web.h" #include "trackHub.h" #include "hubConnect.h" #include "dystring.h" #include "hPrint.h" #include "jsHelper.h" #include "obscure.h" #include "hgConfig.h" #include "trix.h" #include "net.h" #include "hubSearchText.h" #include "pipeline.h" #include "hubPublic.h" -#include "wikiLink.h" #include "hgHubConnect.h" #include "cartJson.h" +#include "wikiLink.h" +#include "hubSpaceKeys.h" static boolean measureTiming; struct cart *cart; /* The user's ui state. */ struct hash *oldVars = NULL; static char *pageTitle = "Track Data Hubs"; char *database = NULL; char *organism = NULL; struct hubOutputStructure { struct hubOutputStructure *next; struct dyString *metaTags; struct dyString *descriptionMatch; @@ -405,48 +406,78 @@ 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 */ +{ +struct sqlConnection *conn = hConnectCentral(); +struct dyString *query = sqlDyStringCreate("select apiKey from %s where userName='%s'", HUBSPACE_AUTH_TABLE, userName); +char *apiKey = sqlQuickString(conn, dyStringCannibalize(&query)); +hDisconnectCentral(&conn); +return apiKey; +} + void printApiKeySection() { -puts("
"); +puts("
"); puts("

Hubtools API key

"); char *userName = wikiLinkUserName(); char *userId = wikiLinkUserId(); if (userName==NULL || userId==NULL) { char *hgsid = cartSessionId(cart); char *loginUrl = wikiLinkUserLoginUrlReturning(hgsid, wikiLinkEncodeReturnUrl(hgsid, "hgHubConnect", "#dev")); printf("
You are not logged in. Please Login now, then this page will show the API key.
", loginUrl); } else { - puts("
To use the hubtools up command, create a file ~/.hubtools.conf and add this line:
"); - puts("
"); - printf("apiKey=%s@%s", userName, userId); + char *existingKey = getApiKey(userName); + if (existingKey) + { + puts("
You have already generated an api key for use in hubtools. If you would like to generate a new key (which automatically revokes old keys), please click 'generate key'. Otherwise, you can copy and paste the below key to your ~/.hubtools.conf file:
"); + puts("
"); + printf("%s\n", existingKey); + puts("
"); puts("
"); + puts("
Generate an api key
"); + } + else + { + puts("
To use the hubtools up command, click 'generate key'"); + puts("
"); + printf("
Now, create a file ~/.hubtools.conf and add the key:
\n", existingKey != NULL ? "block" : "none"); + puts("
"); + puts("
"); + } + printf("
\nTo revoke any apiKeys associated with your account, click the revoke button: \n
", existingKey != NULL ? "block" : "none"); + // add the event handlers for clicking the generate/revoke buttons + jsInlineF("" + "document.getElementById('generateApiKey').addEventListener('click', generateApiKey);\n" + "document.getElementById('revokeApiKeys').addEventListener('click', revokeApiKeys);\n" + ); } puts("
"); // tabSection apiKey } void hgHubConnectDeveloperMode() /* Put up the controls for the "Hub Development" Tab, which includes a button to run the * hubCheck utility on a hub and load a hub with the udcTimeout and measureTiming * variables turned on */ { // put out the top of our page char *hubUrl = cartOptionalString(cart, "validateHubUrl"); // the outer div for all the elements in the tab puts("
"); @@ -1709,30 +1740,32 @@ printf("
"); // #tabs cartWebEnd(); } void doAsync(struct cart *theCart) /* Execute the async request */ { cart = theCart; struct cartJson *cj = cartJsonNew(cart); cartJsonRegisterHandler(cj, hgHubDeleteFile, doRemoveFile); cartJsonRegisterHandler(cj, hgHubCreateHub, doCreateHub); cartJsonRegisterHandler(cj, hgHubMoveFile, doMoveFile); +cartJsonRegisterHandler(cj, hgHubGenerateApiKey, generateApiKey); +cartJsonRegisterHandler(cj, hgHubRevokeApiKey, revokeApiKey); 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 (cgiOptionalString(CARTJSON_COMMAND))