Skip to content

📖 GetAuthConfig

Reads all authentication configuration from auth.json in the Composer home directory and returns the structured AuthConfig.

When to use

Use it when you need to inspect all currently configured tokens and credentials (GitHub, GitLab, Bitbucket, Bearer, HTTP Basic, AWS), or to read the current state before performing bulk additions/deletions.

Signature

go
func (c *Composer) GetAuthConfig() (*AuthConfig, error)

Parameters

This method does not accept any parameters.

Return value

  • *AuthConfig: structured authentication configuration. If auth.json does not exist, an empty AuthConfig is returned (with each map being nil), which is not treated as an error.
  • error: returns the corresponding error message when reading the file or parsing JSON fails.

AuthConfig structure:

go
type AuthConfig struct {
	GitHub       map[string]string `json:"github-oauth,omitempty"`
	GitLab       map[string]string `json:"gitlab-oauth,omitempty"`
	GitLabToken  map[string]string `json:"gitlab-token,omitempty"`
	Bitbucket    map[string]string `json:"bitbucket-oauth,omitempty"`
	Bearer       map[string]string `json:"bearer,omitempty"`
	HTTPBasic    map[string]string `json:"http-basic,omitempty"`
	AWSAccessKey map[string]string `json:"aws-access-key,omitempty"`
}

Example

go
package main

import (
	"fmt"
	"log"

	"github.com/scagogogo/composer-skills/pkg/composer"
)

func main() {
	comp, err := composer.New(composer.DefaultOptions())
	if err != nil {
		log.Fatalf("initialization failed: %v", err)
	}

	config, err := comp.GetAuthConfig()
	if err != nil {
		log.Fatalf("failed to read auth config: %v", err)
	}

	for domain, token := range config.GitHub {
		fmt.Printf("GitHub[%s] = %s\n", domain, token)
	}
}

Advanced

  • This method is the read foundation for all Add*Token / AddHTTPBasicAuth operations: they each first call GetAuthConfig then SaveAuthConfig.
  • The auth.json path is ComposerHome()/auth.json, which can be obtained via GetComposerHome.
  • After modifying the configuration, be sure to call SaveAuthConfig to persist it.

Released under the MIT License