File tree Expand file tree Collapse file tree 1 file changed +71
-0
lines changed
Expand file tree Collapse file tree 1 file changed +71
-0
lines changed Original file line number Diff line number Diff line change 1+ #! /bin/bash
2+ #
3+ # Fetch OAuth scopes granted to a personal access token.
4+ #
5+ # Usage:
6+ # script/get-token-scopes --token=...
7+ #
8+ # Requires a token to be provided explicitly via --token.
9+
10+ set -euo pipefail
11+
12+ HOST=" https://api.github.com"
13+ TOKEN=" "
14+
15+ usage () {
16+ cat << 'EOF '
17+ Usage:
18+ script/get-token-scopes --token=...
19+
20+ Options:
21+ --token=TOKEN Personal access token (required)
22+ -h, --help Show this help message
23+ EOF
24+ }
25+
26+ for arg in " $@ " ; do
27+ case " $arg " in
28+ --token=* )
29+ TOKEN=" ${arg#* =} "
30+ ;;
31+ -h|--help)
32+ usage
33+ exit 0
34+ ;;
35+ * )
36+ echo " Unknown argument: $arg " >&2
37+ usage
38+ exit 1
39+ ;;
40+ esac
41+ done
42+
43+ if [[ -z " ${TOKEN} " ]]; then
44+ echo " --token is required." >&2
45+ exit 1
46+ fi
47+
48+ API=" ${HOST%/ } /user"
49+
50+ headers=$( curl -fsSL -D - -o /dev/null -H " Authorization: Bearer ${TOKEN} " " ${API} " || true)
51+
52+ if [[ -z " $headers " ]]; then
53+ echo " Failed to fetch headers from ${API} . Check connectivity and host URL." >&2
54+ exit 1
55+ fi
56+
57+ status=$( printf " %s\n" " $headers " | head -n1)
58+ if ! printf " %s" " $status " | grep -q " 200 " ; then
59+ echo " Request failed (${status} ). Check that the token is valid for ${HOST} ." >&2
60+ exit 1
61+ fi
62+
63+ scopes=$( printf " %s\n" " $headers " | grep -i ' ^x-oauth-scopes:' | cut -d' :' -f2- | sed ' s/^[[:space:]]*//' | tr -d ' \r' )
64+
65+ if [[ -z " $scopes " ]]; then
66+ echo " No X-OAuth-Scopes header returned. The token may be invalid or lacks scopes." >&2
67+ exit 1
68+ fi
69+
70+ echo " Scopes for token:"
71+ printf ' %s\n' " $scopes " | tr ' ,' ' \n' | sed ' s/^[[:space:]]*//' | sed ' /^$/d'
You can’t perform that action at this time.
0 commit comments