Use auth.json for composer authentication

This commit is contained in:
Shivam Mathur
2025-07-28 20:11:02 +05:30
parent c1c6c51867
commit 50ad25710d
2 changed files with 54 additions and 2 deletions

View File

@ -28,6 +28,39 @@ Function Edit-ComposerConfig() {
Set-ComposerAuth Set-ComposerAuth
} }
# Function to merge auth.json fragments.
Function Get-MergedAuthJson {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string[]] $ComposerAuth
)
if (Test-Path $composer_home\auth.json) {
try {
$existing = Get-Content $composer_home\auth.json -Raw | ConvertFrom-Json
} catch {
$existing = [PSCustomObject]@{}
}
} else {
$existing = [PSCustomObject]@{}
}
foreach ($fragment in $ComposerAuth) {
$piece = ('{' + $fragment + '}') | ConvertFrom-Json
foreach ($prop in $piece.PSObject.Properties) {
if ($prop.Name -eq 'http-basic') {
if (-not $existing.'http-basic') {
$existing | Add-Member -MemberType NoteProperty -Name 'http-basic' -Value ([PSCustomObject]@{}) -Force
}
foreach ($domainProp in $prop.Value.PSObject.Properties) {
$existing.'http-basic' | Add-Member -MemberType NoteProperty -Name $domainProp.Name -Value $domainProp.Value -Force
}
} else {
$existing | Add-Member -MemberType NoteProperty -Name $prop.Name -Value $prop.Value -Force
}
}
}
return $existing | ConvertTo-Json -Depth 5
}
# Function to setup authentication in composer. # Function to setup authentication in composer.
Function Set-ComposerAuth() { Function Set-ComposerAuth() {
if(Test-Path env:COMPOSER_AUTH_JSON) { if(Test-Path env:COMPOSER_AUTH_JSON) {
@ -48,7 +81,7 @@ Function Set-ComposerAuth() {
$composer_auth += '"github-oauth": {"github.com": "' + $env:GITHUB_TOKEN + '"}' $composer_auth += '"github-oauth": {"github.com": "' + $env:GITHUB_TOKEN + '"}'
} }
if($composer_auth.length) { if($composer_auth.length) {
Add-Env COMPOSER_AUTH ('{' + ($composer_auth -join ',') + '}') Set-Content -Path $composer_home\auth.json -Value (Get-MergedAuthJson $composer_auth)
} }
} }

View File

@ -46,6 +46,25 @@ configure_composer() {
set_composer_auth set_composer_auth
} }
# Function to merge auth.json fragments.
get_merged_auth_json() {
local auth_file="$composer_home/auth.json"
local merged
[[ -f "$auth_file" ]] && merged=$(<"$auth_file") || merged='{}'
for frag in "$@"; do
local obj="{$frag}"
merged=$(jq -n --argjson b "$merged" --argjson n "$obj" '
if $n|has("http-basic") then
(($b["http-basic"]//{}) + $n["http-basic"]) as $hb
| ($b + $n) | .["http-basic"] = $hb
else
$b + $n
end
')
done
printf '%s' "$merged"
}
# Function to setup authentication in composer. # Function to setup authentication in composer.
set_composer_auth() { set_composer_auth() {
if [ -n "$COMPOSER_AUTH_JSON" ]; then if [ -n "$COMPOSER_AUTH_JSON" ]; then
@ -63,7 +82,7 @@ set_composer_auth() {
composer_auth+=( '"github-oauth": {"github.com": "'"${GITHUB_TOKEN:-$COMPOSER_TOKEN}"'"}' ) composer_auth+=( '"github-oauth": {"github.com": "'"${GITHUB_TOKEN:-$COMPOSER_TOKEN}"'"}' )
fi fi
if ((${#composer_auth[@]})); then if ((${#composer_auth[@]})); then
add_env COMPOSER_AUTH "{$(IFS=$','; echo "${composer_auth[*]}")}" get_merged_auth_json "${composer_auth[@]}" | tee "$composer_home/auth.json" >/dev/null
fi fi
} }