setup-composer-auth 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/bin/bash
  2. GLOBAL_AUTH=~/.composer/auth.json
  3. PROJECT_AUTH=./src/auth.json
  4. NEED_AUTH=false
  5. hash composer 2>/dev/null && USE_COMPOSER=true
  6. hash python3 2>/dev/null && USE_PYTHON3=true
  7. [ ! $USE_COMPOSER ] && [ ! $USE_PYTHON3 ] && echo "Failed to setup composer auth, it needs composer or python3" && exit 1
  8. # Get composer auth: username and password
  9. if [ $USE_COMPOSER ]; then
  10. COMPOSER_USER="http-basic.repo.magento.com.username"
  11. COMPOSER_PASS="http-basic.repo.magento.com.password"
  12. PUBLIC_KEY="$(composer config -g $COMPOSER_USER 2>/dev/null)"
  13. PRIVATE_KEY="$(composer config -g $COMPOSER_PASS 2>/dev/null)"
  14. if [ -z "$PUBLIC_KEY" ] || [ -z "$PRIVATE_KEY" ]; then
  15. PUBLIC_KEY="$(composer config -d ./src $COMPOSER_USER 2>/dev/null)"
  16. PRIVATE_KEY="$(composer config -d ./src $COMPOSER_PASS 2>/dev/null)"
  17. NEED_AUTH=true
  18. fi
  19. elif [ $USE_PYTHON3 ]; then
  20. PY3_USER="import sys, json; print(json.load(sys.stdin)['http-basic']['repo.magento.com']['username'])"
  21. PY3_PASS="import sys, json; print(json.load(sys.stdin)['http-basic']['repo.magento.com']['password'])"
  22. if [ -f "$GLOBAL_AUTH" ]; then
  23. PUBLIC_KEY=$(python3 -c "$PY3_USER" 2>/dev/null < "$GLOBAL_AUTH")
  24. PRIVATE_KEY=$(python3 -c "$PY3_USER" 2>/dev/null < "$GLOBAL_AUTH")
  25. fi
  26. if [ -z "$PUBLIC_KEY" ] || [ -z "$PRIVATE_KEY" ]; then
  27. if [ -f "$PROJECT_AUTH" ]; then
  28. PUBLIC_KEY=$(python3 -c "$PY3_USER" 2>/dev/null < "$PROJECT_AUTH")
  29. PRIVATE_KEY=$(python3 -c "$PY3_PASS" 2>/dev/null < "$PROJECT_AUTH")
  30. NEED_AUTH=true
  31. fi
  32. fi
  33. fi
  34. if [ -n "$PUBLIC_KEY" ] && [ -n "$PRIVATE_KEY" ] && [ $NEED_AUTH = false ]; then
  35. echo "Global composer auth already exists" && exit
  36. fi
  37. # The last chance to enter manually
  38. if [ -z "$PUBLIC_KEY" ] || [ -z "$PRIVATE_KEY" ]; then
  39. exec < /dev/tty
  40. echo
  41. echo " Authentication required (repo.magento.com, public_key and private_key):"
  42. read -r -p " Username: " PUBLIC_KEY
  43. read -r -p " Password: " PRIVATE_KEY
  44. echo
  45. exec <&-
  46. fi
  47. if [ -z "$PUBLIC_KEY" ] || [ -z "$PRIVATE_KEY" ]; then
  48. echo "Please enter composer auth for repo.magento.com" && exit 1
  49. fi
  50. # For docker-compose.yml setting: ~/.composer:/var/www/.composer:cached
  51. echo "Authentication will add to host composer global config ($GLOBAL_AUTH) for docker container"
  52. if [ $USE_COMPOSER ]; then
  53. composer global config http-basic.repo.magento.com "$PUBLIC_KEY" "$PRIVATE_KEY"
  54. elif [ $USE_PYTHON3 ]; then
  55. PY3_MERGE_AUTH="""
  56. import sys, json;
  57. data = json.load(sys.stdin)
  58. auth= {
  59. 'http-basic': {
  60. 'repo.magento.com': {
  61. 'username': '${PUBLIC_KEY}',
  62. 'password': '${PRIVATE_KEY}'
  63. }
  64. }
  65. }
  66. def merge(src, dest):
  67. for key, val in src.items():
  68. if isinstance(val, dict):
  69. node = dest.setdefault(key, {})
  70. merge(val, node)
  71. else:
  72. dest[key] = val
  73. return dest
  74. print(json.dumps(merge(auth, data), indent=4))
  75. """
  76. if [ -f "$GLOBAL_AUTH" ]; then
  77. mkdir -p "$(dirname "$GLOBAL_AUTH")"
  78. echo "{}" > "$GLOBAL_AUTH"
  79. fi
  80. mv "$GLOBAL_AUTH" "$GLOBAL_AUTH.bak"
  81. python3 -c "$PY3_MERGE_AUTH" > "$GLOBAL_AUTH" < "$GLOBAL_AUTH.bak"
  82. fi
  83. echo "Success to setup composer auth"