Skip to content

Commit c4ed0f0

Browse files
fix: ensure streamed responses are handled (#288)
1 parent 4d19f84 commit c4ed0f0

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ function fastifyOauth2 (fastify, options, next) {
510510

511511
function onUserinfoResponse (res) {
512512
let rawData = ''
513-
res.on('data', (chunk) => { rawData = chunk })
513+
res.on('data', (chunk) => { rawData += chunk })
514514
res.on('end', () => {
515515
try {
516516
cb(null, JSON.parse(rawData)) // should always be JSON since we don't do jwt auth response

test/index.test.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const t = require('tap')
44
const nock = require('nock')
55
const createFastify = require('fastify')
66
const crypto = require('node:crypto')
7+
const { Readable } = require('node:stream')
78
const fastifyOauth2 = require('..')
89

910
nock.disableNetConnect()
@@ -132,12 +133,31 @@ function makeRequests (t, fastify, userAgentHeaderMatcher, pkce, discoveryHost,
132133
.query({ a: 1 })
133134
.reply(200, { sub: 'github.subjectid' })
134135
}
136+
} else if (discoveryHostOptions.userinfoBadData) {
137+
userinfoScope = nock(gitHost)
138+
.matchHeader('Authorization', 'Bearer my-access-token-refreshed')
139+
.matchHeader('User-Agent', userAgentHeaderMatcher || 'fastify-oauth2')
140+
.get('/me')
141+
.reply(200, 'not a json')
142+
} else if (discoveryHostOptions.userinfoChunks) {
143+
function createStream () {
144+
const stream = new Readable()
145+
stream.push('{"sub":"gith')
146+
stream.push('ub.subjectid"}')
147+
stream.push(null)
148+
return stream
149+
}
150+
userinfoScope = nock(gitHost)
151+
.matchHeader('Authorization', 'Bearer my-access-token-refreshed')
152+
.matchHeader('User-Agent', userAgentHeaderMatcher || 'fastify-oauth2')
153+
.get('/me')
154+
.reply(200, createStream())
135155
} else {
136156
userinfoScope = nock(gitHost)
137157
.matchHeader('Authorization', 'Bearer my-access-token-refreshed')
138158
.matchHeader('User-Agent', userAgentHeaderMatcher || 'fastify-oauth2')
139159
.get('/me')
140-
.reply(200, discoveryHostOptions.userinfoBadData ? 'not a json' : { sub: 'github.subjectid' })
160+
.reply(200, { sub: 'github.subjectid' })
141161
}
142162
}
143163
}
@@ -780,6 +800,44 @@ t.test('fastify-oauth2', t => {
780800
makeRequests(t, fastify, undefined, 'S256', 'https://github.com', false, { userinfoEndpoint: 'https://github.com/me' })
781801
})
782802

803+
t.test('discovery with userinfo -> handles responses with multiple "data" events', t => {
804+
const fastify = createFastify({ logger: { level: 'silent' } })
805+
806+
fastify.register(fastifyOauth2, {
807+
name: 'githubOAuth2',
808+
credentials: {
809+
client: {
810+
id: 'my-client-id',
811+
secret: 'my-secret'
812+
}
813+
},
814+
startRedirectPath: '/login/github',
815+
callbackUri: 'http://localhost:3000/callback',
816+
scope: ['notifications'],
817+
discovery: {
818+
issuer: 'https://github.com'
819+
}
820+
})
821+
822+
fastify.get('/', async function (request, reply) {
823+
const result = await this.githubOAuth2.getAccessTokenFromAuthorizationCodeFlow(request, reply)
824+
const refreshResult = await this.githubOAuth2.getNewAccessTokenUsingRefreshToken(result.token)
825+
await new Promise((resolve) => {
826+
this.githubOAuth2.userinfo(refreshResult.token, {}, (err, userinfo) => {
827+
t.error(err)
828+
t.equal(userinfo.sub, 'github.subjectid', 'should match an id')
829+
resolve()
830+
})
831+
})
832+
833+
return { ...refreshResult.token, expires_at: undefined }
834+
})
835+
836+
t.teardown(fastify.close.bind(fastify))
837+
838+
makeRequests(t, fastify, undefined, 'S256', 'https://github.com', false, { userinfoEndpoint: 'https://github.com/me', userinfoChunks: true })
839+
})
840+
783841
t.test('discovery with userinfo -> fails gracefully when at format is bad', t => {
784842
const fastify = createFastify({ logger: { level: 'silent' } })
785843

0 commit comments

Comments
 (0)