-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvalidate-access-token.ts
More file actions
217 lines (191 loc) · 6.52 KB
/
Copy pathvalidate-access-token.ts
File metadata and controls
217 lines (191 loc) · 6.52 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import { invariant } from 'outvariant'
import { getInfo, type GitInfo } from '#/src/utils/git/get-info.js'
export type GitHubTokenType = 'classic' | 'fine-grained'
export type GitHubRepo = Pick<GitInfo, 'owner' | 'name'>
const FINE_GRAINED_TOKEN_PREFIX = 'github_pat_'
export function getGitHubTokenType(accessToken: string): GitHubTokenType {
if (accessToken.startsWith(FINE_GRAINED_TOKEN_PREFIX)) {
return 'fine-grained'
}
return 'classic'
}
export const requiredGitHubTokenScopes: Array<string> = [
'repo',
'admin:repo_hook',
'admin:org_hook',
]
export const GITHUB_NEW_TOKEN_URL = `https://github.com/settings/tokens/new?scopes=${requiredGitHubTokenScopes.join(
',',
)}`
export interface FineGrainedTokenPermission {
permission: string
access: 'read' | 'write'
/**
* Check whether the given access token is granted this permission.
*/
probe: (accessToken: string, repo: GitHubRepo) => Promise<boolean>
}
export const requiredFineGrainedTokenPermissions: Array<FineGrainedTokenPermission> =
[
{
permission: 'contents',
access: 'write',
async probe(accessToken, repo) {
/**
* Attempt to create a release without any payload.
* GitHub checks the token permissions before validating
* the payload: a validation error (422) means the token can
* create releases while 403/404 mean the permission is missing.
*/
const response = await fetch(
`https://api.github.com/repos/${repo.owner}/${repo.name}/releases`,
{
method: 'POST',
headers: {
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({}),
},
)
return response.status === 422
},
},
{
permission: 'issues',
access: 'write',
async probe(accessToken, repo) {
/**
* Attempt to comment on an issue that can never exist (#0).
* A 404/422 response means the token has passed the permission
* check while 403 means the permission is missing.
*/
const response = await fetch(
`https://api.github.com/repos/${repo.owner}/${repo.name}/issues/0/comments`,
{
method: 'POST',
headers: {
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({}),
},
)
return response.status === 404 || response.status === 422
},
},
]
export const GITHUB_NEW_FINE_GRAINED_TOKEN_URL = `https://github.com/settings/personal-access-tokens/new?${requiredFineGrainedTokenPermissions
.map((requiredPermission) => {
return `${requiredPermission.permission}=${requiredPermission.access}`
})
.join('&')}`
/**
* Check whether the given GitHub access token has sufficient permissions
* for this library to create and publish a new release.
*/
export async function validateAccessToken(accessToken: string): Promise<void> {
if (getGitHubTokenType(accessToken) === 'fine-grained') {
const repo = await getInfo()
await validateFineGrainedAccessToken(accessToken, repo)
return
}
await validateClassicAccessToken(accessToken)
}
/**
* Check whether the given classic GitHub access token (OAuth)
* has sufficient permission scopes.
*/
export async function validateClassicAccessToken(
accessToken: string,
): Promise<void> {
const response = await fetch('https://api.github.com', {
headers: {
Authorization: `Bearer ${accessToken}`,
},
})
const permissions =
response.headers
.get('x-oauth-scopes')
?.split(',')
.map((scope) => scope.trim()) || []
// Handle generic error responses.
invariant(
response.ok,
'Failed to verify GitHub token permissions: GitHub API responded with %d %s. Please double-check your "GITHUB_TOKEN" environmental variable and try again.',
response.status,
response.statusText,
)
invariant(
permissions.length > 0,
'Failed to verify GitHub token permissions: GitHub API responded with an empty "X-OAuth-Scopes" header.',
)
const missingScopes = requiredGitHubTokenScopes.filter((scope) => {
return !permissions.includes(scope)
})
if (missingScopes.length > 0) {
invariant(
false,
'Provided "GITHUB_TOKEN" environment variable has insufficient permissions: missing scopes "%s". Please generate a new GitHub personal access token from this URL: %s',
missingScopes.join(`", "`),
GITHUB_NEW_TOKEN_URL,
)
}
}
/**
* Check whether the given fine-grained GitHub access token has
* sufficient permissions for the given repository.
*/
export async function validateFineGrainedAccessToken(
accessToken: string,
repo: GitHubRepo,
): Promise<void> {
const repoResponse = await fetch(
`https://api.github.com/repos/${repo.owner}/${repo.name}`,
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
)
invariant(
repoResponse.status !== 404,
'Failed to verify GitHub token permissions: the provided fine-grained "GITHUB_TOKEN" cannot access the "%s/%s" repository. Please make sure that the repository access of the token includes that repository and try again.',
repo.owner,
repo.name,
)
// Handle generic error responses.
invariant(
repoResponse.ok,
'Failed to verify GitHub token permissions: GitHub API responded with %d %s. Please double-check your "GITHUB_TOKEN" environmental variable and try again.',
repoResponse.status,
repoResponse.statusText,
)
/**
* @note GitHub provides no API to list the permissions granted
* to a fine-grained token so probe the endpoints behind the
* required permissions instead.
*/
const probedPermissions = await Promise.all(
requiredFineGrainedTokenPermissions.map(async (requiredPermission) => {
const isGranted = await requiredPermission.probe(accessToken, repo)
return {
requiredPermission,
isGranted,
}
}),
)
const missingPermissions = probedPermissions
.filter((probedPermission) => {
return !probedPermission.isGranted
})
.map((probedPermission) => {
return `${probedPermission.requiredPermission.permission}: ${probedPermission.requiredPermission.access}`
})
if (missingPermissions.length > 0) {
invariant(
false,
'Provided "GITHUB_TOKEN" environment variable has insufficient permissions: missing permissions "%s". Please generate a new GitHub fine-grained personal access token from this URL: %s',
missingPermissions.join(`", "`),
GITHUB_NEW_FINE_GRAINED_TOKEN_URL,
)
}
}