Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
24 changes: 23 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down