From 994a8add2287eeab4fa960ee82848fff9d4f131c Mon Sep 17 00:00:00 2001 From: Panagiotis Georgakopoulos Date: Thu, 30 Oct 2025 13:17:02 +0200 Subject: [PATCH] feat: jh auth base64 --- auth.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 24 +++++++++++++++++++++++- 2 files changed, 78 insertions(+), 1 deletion(-) diff --git a/auth.go b/auth.go index f8e2528..05b7757 100644 --- a/auth.go +++ b/auth.go @@ -421,3 +421,58 @@ func authEnvCommand() error { return nil } + +func authBase64Command() error { + // Ensure we have a valid token (login if needed, refresh if possible) + token, err := ensureValidToken() + if err != nil { + return fmt.Errorf("failed to ensure valid token: %w", err) + } + + // Parse the ID token to get expiration time and claims + claims, err := decodeJWT(token.IDToken) + if err != nil { + return fmt.Errorf("failed to decode ID token: %w", err) + } + + // Calculate refresh URL + var authServer string + if token.Server == "juliahub.com" { + authServer = "auth.juliahub.com" + } else { + authServer = token.Server + } + refreshURL := fmt.Sprintf("https://%s/dex/token", authServer) + + // Create auth.toml content + content := fmt.Sprintf(`expires_at = %d +id_token = "%s" +access_token = "%s" +refresh_token = "%s" +refresh_url = "%s" +expires_in = %d +user_email = "%s" +expires = %d +user_name = "%s" +name = "%s" +`, + claims.ExpiresAt, + token.IDToken, + token.AccessToken, + token.RefreshToken, + refreshURL, + token.ExpiresIn, + token.Email, + claims.ExpiresAt, + claims.PreferredUsername, + token.Name, + ) + + // Encode to base64 + encoded := base64.StdEncoding.EncodeToString([]byte(content)) + + // Print to stdout + fmt.Println(encoded) + + return nil +} diff --git a/main.go b/main.go index 71b31c2..913bd94 100644 --- a/main.go +++ b/main.go @@ -318,6 +318,28 @@ environment variables that can be used by other tools or scripts.`, }, } +var authBase64Cmd = &cobra.Command{ + Use: "base64", + Short: "Print base64-encoded auth.toml to stdout", + Long: `Create auth.toml content and print it as base64 to stdout. + +This command: +1. Ensures you have a valid authentication token (refreshes if needed) +2. Generates auth.toml content with all required fields +3. Encodes the content to base64 +4. Prints the encoded string to stdout + +This is useful for passing authentication configuration to other systems +or tools that expect auth.toml in base64 format.`, + Example: " jh auth base64\n jh auth base64 > encoded_auth.txt", + Run: func(cmd *cobra.Command, args []string) { + if err := authBase64Command(); err != nil { + fmt.Printf("Failed to generate base64 auth: %v\n", err) + os.Exit(1) + } + }, +} + var jobCmd = &cobra.Command{ Use: "job", Short: "Job management commands", @@ -991,7 +1013,7 @@ func init() { pullCmd.Flags().StringP("server", "s", "juliahub.com", "JuliaHub server") updateCmd.Flags().Bool("force", false, "Force update even if current version is newer than latest release") - authCmd.AddCommand(authLoginCmd, authRefreshCmd, authStatusCmd, authEnvCmd) + authCmd.AddCommand(authLoginCmd, authRefreshCmd, authStatusCmd, authEnvCmd, authBase64Cmd) jobCmd.AddCommand(jobListCmd, jobStartCmd) datasetCmd.AddCommand(datasetListCmd, datasetDownloadCmd, datasetUploadCmd, datasetStatusCmd) projectCmd.AddCommand(projectListCmd)