1111
1212set -o pipefail
1313
14- # APP_ID=$(cat $1) # Path to appid.env
15- # PRIVATE_KEY_PATH=$2 # Path to key_private.pem
16- echo " APP_PRIVATE_KEY path: $APP_PRIVATE_KEY "
14+ _GITHUB_HOST=${GITHUB_HOST:= " github.com" }
1715
18- # Generate JWT
19- header=' {"alg":"RS256","typ":"JWT"}'
20- payload=" {\" iat\" :$( date +%s) ,\" exp\" :$(( $(date +% s) + 600 )) ,\" iss\" :${APP_ID} }"
16+ # If URL is not github.com then use the enterprise api endpoint
17+ if [[ ${GITHUB_HOST} = " github.com" ]]; then
18+ URI=" https://api.${_GITHUB_HOST} "
19+ else
20+ URI=" https://${_GITHUB_HOST} /api/v3"
21+ fi
2122
22- header_base64=$( echo -n " $header " | openssl base64 | tr -d ' =' | tr ' /+' ' _-' | tr -d ' \n' )
23- payload_base64=$( echo -n " $payload " | openssl base64 | tr -d ' =' | tr ' /+' ' _-' | tr -d ' \n' )
23+ API_VERSION=v3
24+ API_HEADER=" Accept: application/vnd.github.${API_VERSION} +json"
25+ CONTENT_LENGTH_HEADER=" Content-Length: 0"
26+ APP_INSTALLATIONS_URI=" ${URI} /app/installations"
2427
25- signature=$( echo -n " ${header_base64} .${payload_base64} " | \
26- openssl dgst -sha256 -sign " ${APP_PRIVATE_KEY} " | \
27- openssl base64 | tr -d ' =' | tr ' /+' ' _-' | tr -d ' \n' )
2828
29- echo " Contents of APP_PRIVATE_KEY:"
30- cat " $APP_PRIVATE_KEY "
29+ # JWT parameters based off
30+ # https://docs.github.com/en/developers/apps/building-github-apps/authenticating-with-github-apps#authenticating-as-a-github-app
31+ #
32+ # JWT token issuance and expiration parameters
33+ JWT_IAT_DRIFT=60
34+ JWT_EXP_DELTA=600
35+
36+ JWT_JOSE_HEADER=' {
37+ "alg": "RS256",
38+ "typ": "JWT"
39+ }'
40+
3141
42+ build_jwt_payload () {
43+ now=$( date +%s)
44+ iat=$(( now - JWT_IAT_DRIFT))
45+ jq -c \
46+ --arg iat_str " ${iat} " \
47+ --arg exp_delta_str " ${JWT_EXP_DELTA} " \
48+ --arg app_id_str " ${APP_ID} " \
49+ '
50+ ($iat_str | tonumber) as $iat
51+ | ($exp_delta_str | tonumber) as $exp_delta
52+ | ($app_id_str | tonumber) as $app_id
53+ | .iat = $iat
54+ | .exp = ($iat + $exp_delta)
55+ | .iss = $app_id
56+ ' <<< " {}" | tr -d ' \n'
57+ }
3258
33- generated_jwt=" ${header_base64} .${payload_base64} .${signature} "
59+ base64url () {
60+ base64 | tr ' +/' ' -_' | tr -d ' =\n'
61+ }
3462
35- echo $generated_jwt
36- # API_VERSION=v3
37- # API_HEADER="Accept: application/vnd.github+json"
63+ rs256_sign () {
64+ openssl dgst -binary -sha256 -sign <( echo " $1 " )
65+ }
3866
39- # auth_header="Authorization: Bearer ${generated_jwt}"
67+ request_access_token () {
68+ jwt_payload=$( build_jwt_payload)
69+ encoded_jwt_parts=$( base64url <<< " ${JWT_JOSE_HEADER}" ) .$( base64url <<< " ${jwt_payload}" )
70+ encoded_mac=$( echo -n " $encoded_jwt_parts " | rs256_sign " ${APP_PRIVATE_KEY} " | base64url)
71+ generated_jwt=" ${encoded_jwt_parts} .${encoded_mac} "
4072
41- # app_installations_response=$(curl -sX POST \
42- # -H "${auth_header}" \
43- # -H "${API_HEADER}" \
44- # --url "https://api.github.com/app/installations/${INSTALL_ID}/access_tokens" \
45- # )
73+ auth_header=" Authorization: Bearer ${generated_jwt} "
4674
47- # echo "$app_installations_response" | jq --raw-output '.token'
75+ app_installations_response=$( curl -sX POST \
76+ -H " ${auth_header} " \
77+ -H " ${API_HEADER} " \
78+ --header " X-GitHub-Api-Version: 2022-11-28" \
79+ --url " https://api.github.com/app/installations/${INSTALL_ID} /access_tokens" \
80+ )
81+ echo " $app_installations_response " | jq --raw-output ' .token'
82+ }
4883
49- # echo "ACCESS_TOKEN=${jwt}" > "${DST_FILE}"
84+ request_access_token
0 commit comments