Repository navigation
Expand file tree
/
Copy pathcomposer_token.sh
More file actions
62 lines (52 loc) · 1.71 KB
/
Copy pathcomposer_token.sh
File metadata and controls
62 lines (52 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env bash
# composer_token.sh
# Securely sets the GitHub OAuth token for Composer private package access.
# Usage: bash composer_token.sh
set -euo pipefail
COMPOSER_AUTH_FILE="${COMPOSER_HOME:-$HOME/.config/composer}/auth.json"
echo "Composer GitHub OAuth Token Setup"
echo "==================================="
echo "This sets the token used by Composer to access private GitHub repositories."
echo "Token is stored in: $COMPOSER_AUTH_FILE"
echo ""
printf "Token: "
TOKEN=""
while IFS= read -r -s -n1 c; do
if [[ -z "$c" ]]; then
echo
break
elif [[ "$c" == $'\x7f' ]]; then
if [[ -n "$TOKEN" ]]; then
TOKEN="${TOKEN%?}"
printf '\b \b'
fi
else
TOKEN+="$c"
printf '*'
fi
done
if [[ -z "$TOKEN" ]]; then
echo "Error: No token entered. Aborting." >&2
exit 1
fi
# Basic sanity check — GitHub PATs start with github_pat_ or ghp_
if [[ "$TOKEN" != github_pat_* && "$TOKEN" != ghp_* ]]; then
echo "Warning: Token does not look like a GitHub PAT (expected github_pat_ or ghp_ prefix)."
read -r -p "Continue anyway? [y/N]: " confirm
[[ "${confirm,,}" == "y" ]] || { echo "Aborted."; exit 1; }
fi
composer config --global github-oauth.github.com "$TOKEN"
unset TOKEN
echo ""
echo "Token saved to $COMPOSER_AUTH_FILE"
# Verify without exposing the value
SAVED=$(php -r "
\$auth = json_decode(file_get_contents('$COMPOSER_AUTH_FILE'), true);
echo \$auth['github-oauth']['github.com'] ?? '';
" 2>/dev/null)
if [[ -n "$SAVED" ]]; then
echo "Verification: Token is set. Length: ${#SAVED} characters, starts with: ${SAVED:0:10}..."
else
echo "Verification failed: token does not appear to be saved." >&2
exit 1
fi