Skip to content

🔑 Authentication

Manage Composer's auth.json credentials file, configuring GitHub / GitLab / Bitbucket / Bearer / HTTP Basic authentication tokens for private repositories and protected APIs.

Composer Skills wraps auth.json read/write operations into a set of type-safe methods. All Add methods follow the atomic "read → modify → write back" pattern: first call GetAuthConfig to get the current configuration (returns empty config if file doesn't exist), modify entries in memory, then call SaveAuthConfig to write back to ~/.composer/auth.json with 0600 permissions.

When to Use

  • 🗝️ Project depends on private Git repositories (self-hosted GitLab, Bitbucket team spaces); composer install requires tokens to pull.
  • 🤖 CI scripts dynamically inject deployment credentials without hardcoding tokens in auth.json.
  • 🔄 Token rotation: remove old token, write new token.
  • 🔍 Audit: list currently configured authentication entries to confirm no stale credentials remain.

Structured Types

AuthConfig

Top-level structure operated on by GetAuthConfig / SaveAuthConfig, corresponds to the root object of auth.json.

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"`
}

Each field is a "domain → credentials" mapping. Values for Bitbucket and HTTPBasic are stored in consumer:token / username:password format.

authType String Constants

The authType parameter of RemoveToken / GetToken must be one of: github-oauth, gitlab-oauth, bitbucket-oauth, bearer, http-basic. Passing other values returns ErrInvalidAuthType.

Method Signatures

MethodSignatureDescription
🔑 GetAuthConfigfunc (c *Composer) GetAuthConfig() (*AuthConfig, error)Read auth.json; returns empty config if file doesn't exist
💾 SaveAuthConfigfunc (c *Composer) SaveAuthConfig(config *AuthConfig) errorWrite back to auth.json with 0600 permissions
🐙 AddGitHubTokenfunc (c *Composer) AddGitHubToken(domain string, token string) errorAdd GitHub OAuth token
🦊 AddGitLabTokenfunc (c *Composer) AddGitLabToken(domain string, token string) errorAdd GitLab OAuth token
🪣 AddBitbucketTokenfunc (c *Composer) AddBitbucketToken(domain string, consumer string, token string) errorAdd Bitbucket OAuth token (consumer:token)
🎟️ AddBearerTokenfunc (c *Composer) AddBearerToken(domain string, token string) errorAdd Bearer token
🔐 AddHTTPBasicAuthfunc (c *Composer) AddHTTPBasicAuth(domain string, username string, password string) errorAdd HTTP Basic auth (username:password)
🗑️ RemoveTokenfunc (c *Composer) RemoveToken(authType string, domain string) errorRemove token by type and domain
🔍 GetTokenfunc (c *Composer) GetToken(authType string, domain string) (string, error)Read token by type and domain

Parameters

AddBitbucketToken

ParameterTypeDescription
domainstringRepository domain, e.g., bitbucket.org or self-hosted instance domain
consumerstringOAuth consumer key
tokenstringOAuth access token

RemoveToken / GetToken

ParameterTypeDescription
authTypestringAuthentication type, see the constant list above
domainstringDomain the token corresponds to

Examples

Inject GitHub Token

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("Failed to initialize Composer: %v", err)
	}

	// Write OAuth token for github.com
	if err := comp.AddGitHubToken("github.com", "ghp_xxxxxxxxxxxxxxxxxxxxxxxx"); err != nil {
		log.Fatalf("Failed to write GitHub token: %v", err)
	}

	// Read back to verify
	token, err := comp.GetToken("github-oauth", "github.com")
	if err != nil {
		log.Fatalf("Failed to read token: %v", err)
	}
	fmt.Printf("github.com token prefix: %s...\n", token[:10])
}

Configure Multiple Credentials for Self-hosted GitLab

go
// Self-hosted GitLab instance
if err := comp.AddGitLabToken("gitlab.example.com", "glpat-xxxxxxxxxxxxxxxxxxxx"); err != nil {
	log.Fatal(err)
}

// Private Composer repository using HTTP Basic
if err := comp.AddHTTPBasicAuth("packages.example.com", "ci-deploy", "s3cret-pass"); err != nil {
	log.Fatal(err)
}

// Another repository using Bearer
if err := comp.AddBearerToken("api.example.com", "Bearer-token-value"); err != nil {
	log.Fatal(err)
}

Rotate Token: Remove Old, Write New

go
// Remove old GitHub token
if err := comp.RemoveToken("github-oauth", "github.com"); err != nil {
	if err == composer.ErrInvalidAuthType {
		log.Fatal("Unsupported authentication type passed")
	}
	log.Fatal(err)
}

// Write new token
if err := comp.AddGitHubToken("github.com", "ghp_newrotatedtoken"); err != nil {
	log.Fatal(err)
}

Full Read and Iterate

go
config, err := comp.GetAuthConfig()
if err != nil {
	log.Fatal(err)
}
for domain, token := range config.GitHub {
	fmt.Printf("github-oauth  %s  %s***\n", domain, token[:6])
}
for domain := range config.HTTPBasic {
	fmt.Printf("http-basic   %s\n", domain)
}

Advanced

0600 Permissions

SaveAuthConfig writes auth.json with 0600 (owner read/write only) to prevent token leakage to other users on the same machine. Do not manually chmod to loosen permissions.

No Convenience Method for AWS Tokens

AuthConfig includes the AWSAccessKey field for compatibility, but currently no AddAWSAccessKey convenience method is provided. To write, manually construct *AuthConfig and call SaveAuthConfig.

Relationship with GetComposerHome

All methods resolve the auth.json path via c.GetComposerHome(). If the COMPOSER_HOME environment variable is set, the file goes in that directory; otherwise, Composer's default home directory is used.

Released under the MIT License