@@ -4,6 +4,7 @@ const t = require('tap')
4
4
const nock = require ( 'nock' )
5
5
const createFastify = require ( 'fastify' )
6
6
const crypto = require ( 'node:crypto' )
7
+ const { Readable } = require ( 'node:stream' )
7
8
const fastifyOauth2 = require ( '..' )
8
9
9
10
nock . disableNetConnect ( )
@@ -132,12 +133,31 @@ function makeRequests (t, fastify, userAgentHeaderMatcher, pkce, discoveryHost,
132
133
. query ( { a : 1 } )
133
134
. reply ( 200 , { sub : 'github.subjectid' } )
134
135
}
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 ( ) )
135
155
} else {
136
156
userinfoScope = nock ( gitHost )
137
157
. matchHeader ( 'Authorization' , 'Bearer my-access-token-refreshed' )
138
158
. matchHeader ( 'User-Agent' , userAgentHeaderMatcher || 'fastify-oauth2' )
139
159
. get ( '/me' )
140
- . reply ( 200 , discoveryHostOptions . userinfoBadData ? 'not a json' : { sub : 'github.subjectid' } )
160
+ . reply ( 200 , { sub : 'github.subjectid' } )
141
161
}
142
162
}
143
163
}
@@ -780,6 +800,44 @@ t.test('fastify-oauth2', t => {
780
800
makeRequests ( t , fastify , undefined , 'S256' , 'https://github.com' , false , { userinfoEndpoint : 'https://github.com/me' } )
781
801
} )
782
802
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
+
783
841
t . test ( 'discovery with userinfo -> fails gracefully when at format is bad' , t => {
784
842
const fastify = createFastify ( { logger : { level : 'silent' } } )
785
843
0 commit comments