|
| 1 | +#!/bin/bash |
| 2 | +# |
| 3 | +# Usage: git-credential-ghtoken <operation> |
| 4 | +# |
| 5 | +# Allows `git` to authenticate with GitHub using `GH_USER` and `GH_TOKEN` |
| 6 | +# environment variables. |
| 7 | +# |
| 8 | +# To install this: |
| 9 | +# |
| 10 | +# git config --global credential.https://github.com.helper \ |
| 11 | +# "$(pwd)/scripts/git-credential-ghtoken" |
| 12 | +# |
| 13 | +# Or copy it into your path and run: |
| 14 | +# |
| 15 | +# git config --global credential.https://github.com.helper ghtoken |
| 16 | +# |
| 17 | +# |
| 18 | +# Copyright (c) 2018 Faraday, Inc. |
| 19 | +# |
| 20 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 21 | +# of this software and associated documentation files (the "Software"), to deal |
| 22 | +# in the Software without restriction, including without limitation the rights |
| 23 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 24 | +# copies of the Software, and to permit persons to whom the Software is |
| 25 | +# furnished to do so, subject to the following conditions: |
| 26 | +# |
| 27 | +# The above copyright notice and this permission notice shall be included in all |
| 28 | +# copies or substantial portions of the Software. |
| 29 | +# |
| 30 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 31 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 32 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 33 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 34 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 35 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 36 | +# SOFTWARE. |
| 37 | + |
| 38 | +# Standard paranoia. |
| 39 | +set -euo pipefail |
| 40 | + |
| 41 | +# Parse our command-line arguments. |
| 42 | +operation="$1" |
| 43 | + |
| 44 | +# Ignore all operations besides `get`. |
| 45 | +if [ "$operation" != get ]; then |
| 46 | + exit 0 |
| 47 | +fi |
| 48 | + |
| 49 | +# Quit now if we don't have the necessary environment variables. |
| 50 | +if [ ! -v GH_USER ] || [ ! -v GH_TOKEN ]; then |
| 51 | + exit 0 |
| 52 | +fi |
| 53 | + |
| 54 | +# Parse the input we receive from `git`. |
| 55 | +while read line; do |
| 56 | + var="$(echo "$line" | sed 's/=.*$//')" |
| 57 | + val="$(echo "$line" | sed 's/^.*=//')" |
| 58 | + case "$var" in |
| 59 | + # Only send credentials over HTTPS. |
| 60 | + protocol) |
| 61 | + if [ "$val" != https ]; then |
| 62 | + exit 0 |
| 63 | + fi |
| 64 | + ;; |
| 65 | + # Only send credentials to GitHub (just extra paranoia; change as |
| 66 | + # needed). |
| 67 | + host) |
| 68 | + if [ "$val" != github.com ]; then |
| 69 | + exit 0 |
| 70 | + fi |
| 71 | + ;; |
| 72 | + esac |
| 73 | +done |
| 74 | + |
| 75 | +# Output our credentials. |
| 76 | +cat <<EOD |
| 77 | +username=$GH_USER |
| 78 | +password=$GH_TOKEN |
| 79 | +EOD |
0 commit comments