Skip to content

Commit 50ad257

Browse files
committed
Use auth.json for composer authentication
1 parent c1c6c51 commit 50ad257

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/scripts/tools/add_tools.ps1

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,39 @@ Function Edit-ComposerConfig() {
2828
Set-ComposerAuth
2929
}
3030

31+
# Function to merge auth.json fragments.
32+
Function Get-MergedAuthJson {
33+
[CmdletBinding()]
34+
param(
35+
[Parameter(Mandatory)][string[]] $ComposerAuth
36+
)
37+
if (Test-Path $composer_home\auth.json) {
38+
try {
39+
$existing = Get-Content $composer_home\auth.json -Raw | ConvertFrom-Json
40+
} catch {
41+
$existing = [PSCustomObject]@{}
42+
}
43+
} else {
44+
$existing = [PSCustomObject]@{}
45+
}
46+
foreach ($fragment in $ComposerAuth) {
47+
$piece = ('{' + $fragment + '}') | ConvertFrom-Json
48+
foreach ($prop in $piece.PSObject.Properties) {
49+
if ($prop.Name -eq 'http-basic') {
50+
if (-not $existing.'http-basic') {
51+
$existing | Add-Member -MemberType NoteProperty -Name 'http-basic' -Value ([PSCustomObject]@{}) -Force
52+
}
53+
foreach ($domainProp in $prop.Value.PSObject.Properties) {
54+
$existing.'http-basic' | Add-Member -MemberType NoteProperty -Name $domainProp.Name -Value $domainProp.Value -Force
55+
}
56+
} else {
57+
$existing | Add-Member -MemberType NoteProperty -Name $prop.Name -Value $prop.Value -Force
58+
}
59+
}
60+
}
61+
return $existing | ConvertTo-Json -Depth 5
62+
}
63+
3164
# Function to setup authentication in composer.
3265
Function Set-ComposerAuth() {
3366
if(Test-Path env:COMPOSER_AUTH_JSON) {
@@ -48,7 +81,7 @@ Function Set-ComposerAuth() {
4881
$composer_auth += '"github-oauth": {"github.com": "' + $env:GITHUB_TOKEN + '"}'
4982
}
5083
if($composer_auth.length) {
51-
Add-Env COMPOSER_AUTH ('{' + ($composer_auth -join ',') + '}')
84+
Set-Content -Path $composer_home\auth.json -Value (Get-MergedAuthJson $composer_auth)
5285
}
5386
}
5487

src/scripts/tools/add_tools.sh

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,25 @@ configure_composer() {
4646
set_composer_auth
4747
}
4848

49+
# Function to merge auth.json fragments.
50+
get_merged_auth_json() {
51+
local auth_file="$composer_home/auth.json"
52+
local merged
53+
[[ -f "$auth_file" ]] && merged=$(<"$auth_file") || merged='{}'
54+
for frag in "$@"; do
55+
local obj="{$frag}"
56+
merged=$(jq -n --argjson b "$merged" --argjson n "$obj" '
57+
if $n|has("http-basic") then
58+
(($b["http-basic"]//{}) + $n["http-basic"]) as $hb
59+
| ($b + $n) | .["http-basic"] = $hb
60+
else
61+
$b + $n
62+
end
63+
')
64+
done
65+
printf '%s' "$merged"
66+
}
67+
4968
# Function to setup authentication in composer.
5069
set_composer_auth() {
5170
if [ -n "$COMPOSER_AUTH_JSON" ]; then
@@ -63,7 +82,7 @@ set_composer_auth() {
6382
composer_auth+=( '"github-oauth": {"github.com": "'"${GITHUB_TOKEN:-$COMPOSER_TOKEN}"'"}' )
6483
fi
6584
if ((${#composer_auth[@]})); then
66-
add_env COMPOSER_AUTH "{$(IFS=$','; echo "${composer_auth[*]}")}"
85+
get_merged_auth_json "${composer_auth[@]}" | tee "$composer_home/auth.json" >/dev/null
6786
fi
6887
}
6988

0 commit comments

Comments
 (0)