-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-ghapp-token.sh
executable file
·65 lines (51 loc) · 1.8 KB
/
get-ghapp-token.sh
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
63
64
65
#! /bin/bash
# How to use this:
#
# - Go to organization Settings > Developer Settings > GitHub Apps.
# - Create a new github app and give it some useful permissions.
# - On the new app page, note the 'App ID'. Also generate and download a private key.
# - Copy paste contents of the private key into a file, for example: /tmp/my-app.pem
# - On the 'Install App' tab, pick an account and install app on a repo, or every repo.
# - This takes you to the new installation page. Look in URL for installation id.
if [ $# -lt 3 -o $# -gt 4 ] ; then
echo "Usage: $(basename $BASH_SOURCE) <private-key-file> <app-id> <installation-id> [<server-api-url>]"
exit 1
fi
privKeyFile=$1
appId=$2
instId=$3
url=${4:-"$apiUrl"}
url=${4:-"http://api.github.localhost"}
url=${url%/}
if [[ $url != *://api.* ]] ; then
echo -e "*** WARNING: Expected URL to start with 'api.', for example: https://api.github.com\n"
fi
base64Encode() { base64 --wrap=0 | tr '+/' '-_' | tr -d '='; }
pubKeyFile=${privKeyFile%.pem}.pkcs1
if [[ ! -f $pubKeyFile ]]; then
openssl rsa -in $privKeyFile -outform PEM -pubout -out $pubKeyFile
fi
header='{
"alg": "RS256",
"typ": "JWT"
}'
headerB64=$(echo -n $header | base64Encode)
payload='{
"iat": '$(expr `date +%s` - 60)',
"exp": '$(expr `date +%s` + 600)',
"iss": "'$appId'"
}'
payloadB64=$(echo -n $payload | base64Encode)
signedContent="${headerB64}.${payloadB64}"
signature=$(printf %s "$signedContent" | openssl dgst -sha256 -sign $privKeyFile | base64Encode)
jwt="${signedContent}.${signature}"
# Add --fail-with-body when they update curl
curlOutput=$(curl --silent -X POST \
-H "Authorization: Bearer $jwt" \
-H "Accept: application/vnd.github+json" \
"$url/app/installations/$instId/access_tokens")
if [ $? -ne 0 ] ; then
echo "$curlOutput" 1>&2
exit 1
fi
jq -r .token <<< "$curlOutput"