Files
shields/services/github/auth/acceptor.js
DCjanusandcopilot-swe-agent[bot] bd23e5fce6 [GitHub] Store accepted token scopes (#11915)
* feat(github): store token scopes

Assisted-by: Codex <gpt-5.5>

* perf(github): index token scopes

Assisted-by: Codex <gpt-5.5>

* feat(github): persist accepted token scopes

Assisted-by: Codex <gpt-5.5>

* docs(github): clarify OAuth scope capture

Assisted-by: Codex <gpt-5.5>

* docs(github): shorten OAuth scope note

Assisted-by: Codex <gpt-5.5>

* docs(github): clarify default OAuth scopes

Assisted-by: Codex <gpt-5.5>

* docs(github): link OAuth scope discussion

Assisted-by: Codex <gpt-5.5>

* docs(github): mark OAuth scopes as TODO

Assisted-by: Codex <gpt-5.5>

* fix(token-pool): refresh existing token data

Assisted-by: Codex <gpt-5.5>

* fix(token-pool): avoid duplicate revalidated tokens

Assisted-by: Codex:gpt-5.5

* fix(token-pool): remove revalidated tokens from priority queue

Assisted-by: Codex:gpt-5.5

* fix(token-pool): preserve token metadata on re-add

Assisted-by: Codex:gpt-5.5

* chore(github): defer token scopes index

Assisted-by: Codex:gpt-5.5

* fix(github): trim accepted token scopes

Assisted-by: Codex:gpt-5.5

* chore(github): keep token scopes index

Assisted-by: Codex:gpt-5.5

* docs(token-pool): document metadata update semantics

Assisted-by: Codex:gpt-5.5

* chore(github): store token scopes without index

Assisted-by: Codex:gpt-5.5

* chore: rerun ci

* test(token-pool): use mixed scope persistence fixture

Assisted-by: Codex:gpt-5.5

* fix(token-pool): replace invalid tokens on re-add

Keep stale queue references invalid so they can be removed lazily without rebuilding the priority queue.

Assisted-by: Codex:gpt-5.6-sol

* chore: apply Prettier 3.9 formatting

Assisted-by: Codex:gpt-5.6-sol

* chore: retry CI

Assisted-by: Codex:gpt-5.6-sol

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
2026-07-20 15:31:01 +02:00

91 lines
3.0 KiB
JavaScript

import qs from 'qs'
import { fetch } from '../../../core/base-service/got.js'
import log from '../../../core/server/log.js'
function setRoutes({ server, authHelper, onTokenAccepted }) {
const baseUrl = 'https://img.shields.io'
server.route(/^\/github-auth$/, (data, match, end, ask) => {
ask.res.statusCode = 302 // Found.
const query = qs.stringify({
// TODO The `_user` property bypasses security checks in AuthHelper.
// (e.g: enforceStrictSsl and shouldAuthenticateRequest).
// Do not use it elsewhere. It would be better to clean this up so
// it's not setting a bad example.
client_id: authHelper._user,
redirect_uri: `${baseUrl}/github-auth/done`,
// TODO: Request the OAuth scopes needed for scoped GitHub API features.
// Remove this TODO once this authorize URL includes a `scope` parameter.
// See https://github.com/badges/shields/issues/4169.
})
ask.res.setHeader(
'Location',
`https://github.com/login/oauth/authorize?${query}`,
)
end('')
})
server.route(/^\/github-auth\/done$/, async (data, match, end, ask) => {
if (!data.code) {
log.log(`GitHub OAuth data: ${JSON.stringify(data)}`)
return end('GitHub OAuth authentication failed to provide a code.')
}
const options = {
method: 'POST',
headers: {
'Content-type': 'application/x-www-form-urlencoded;charset=UTF-8',
},
form: {
// TODO The `_user` and `_pass` properties bypass security checks in
// AuthHelper (e.g: enforceStrictSsl and shouldAuthenticateRequest).
// Do not use them elsewhere. It would be better to clean
// this up so it's not setting a bad example.
client_id: authHelper._user,
client_secret: authHelper._pass,
code: data.code,
},
}
let resp
try {
resp = await fetch('https://github.com/login/oauth/access_token', options)
} catch (e) {
return end('The connection to GitHub failed.')
}
let content
try {
content = qs.parse(resp.buffer)
} catch (e) {
return end('The GitHub OAuth token could not be parsed.')
}
const { access_token: token, scope = '' } = content
if (!token) {
return end('The GitHub OAuth process did not return a user token.')
}
const scopes = scope
.split(',')
.map(scope => scope.trim())
.filter(Boolean)
ask.res.setHeader('Content-Type', 'text/html')
end(
'<p>Shields.io has received your app-specific GitHub user token. ' +
'You can revoke it by going to ' +
'<a href="https://github.com/settings/applications">GitHub</a>.</p>' +
'<p>Until you do, you have now increased the rate limit for GitHub ' +
'requests going through Shields.io. GitHub-related badges are ' +
'therefore more robust.</p>' +
'<p>Thanks for contributing to a smoother experience for ' +
'everyone!</p>' +
'<p><a href="/">Back to the website</a></p>',
)
onTokenAccepted(token, { scopes })
})
}
export { setRoutes }