@@ -57,7 +57,7 @@ test("verifyRequestByKeyId()", async (t) => {
57
57
"content-type" : "application/json" ,
58
58
"x-request-id" : "<request-id>" ,
59
59
} ,
60
- } ,
60
+ }
61
61
) ;
62
62
const testRequest = defaultRequest . defaults ( {
63
63
request : { fetch : fetchMock } ,
@@ -70,6 +70,55 @@ test("verifyRequestByKeyId()", async (t) => {
70
70
t . deepEqual ( result , true ) ;
71
71
} ) ;
72
72
73
+ test ( "verifyRequestByKeyId() - throws if keyId not present in verification keys list" , async ( t ) => {
74
+ const mockAgent = new MockAgent ( ) ;
75
+ function fetchMock ( url , opts ) {
76
+ opts ||= { } ;
77
+ opts . dispatcher = mockAgent ;
78
+ return fetch ( url , opts ) ;
79
+ }
80
+
81
+ mockAgent . disableNetConnect ( ) ;
82
+ const mockPool = mockAgent . get ( "https://api.github.com" ) ;
83
+ mockPool
84
+ . intercept ( {
85
+ method : "get" ,
86
+ path : `/meta/public_keys/copilot_api` ,
87
+ } )
88
+ . reply (
89
+ 200 ,
90
+ {
91
+ public_keys : [
92
+ {
93
+ key : CURRENT_PUBLIC_KEY ,
94
+ key_identifier : KEY_ID ,
95
+ is_current : true ,
96
+ } ,
97
+ ] ,
98
+ } ,
99
+ {
100
+ headers : {
101
+ "content-type" : "application/json" ,
102
+ "x-request-id" : "<request-id>" ,
103
+ } ,
104
+ }
105
+ ) ;
106
+ const testRequest = defaultRequest . defaults ( {
107
+ request : { fetch : fetchMock } ,
108
+ } ) ;
109
+
110
+ await t . throwsAsync (
111
+ verifyRequestByKeyId ( RAW_BODY , SIGNATURE , "wrong_key" , {
112
+ request : testRequest ,
113
+ } ) ,
114
+ {
115
+ name : "Error" ,
116
+ message :
117
+ "[@copilot-extensions/preview-sdk] No public key found matching key identifier" ,
118
+ }
119
+ ) ;
120
+ } ) ;
121
+
73
122
test ( "verifyRequestByKeyId() - invalid arguments" , async ( t ) => {
74
123
t . throwsAsync ( verifyRequestByKeyId ( RAW_BODY , SIGNATURE ) , {
75
124
name : "Error" ,
@@ -170,13 +219,69 @@ test("fetchVerificationKeys()", async (t) => {
170
219
"content-type" : "application/json" ,
171
220
"x-request-id" : "<request-id>" ,
172
221
} ,
222
+ }
223
+ ) ;
224
+ const testRequest = defaultRequest . defaults ( {
225
+ request : { fetch : fetchMock } ,
226
+ } ) ;
227
+
228
+ const result = await fetchVerificationKeys ( {
229
+ request : testRequest ,
230
+ } ) ;
231
+
232
+ t . deepEqual ( result , publicKeys ) ;
233
+ } ) ;
234
+
235
+ test ( "fetchVerificationKeys() - with token" , async ( t ) => {
236
+ const mockAgent = new MockAgent ( ) ;
237
+ function fetchMock ( url , opts ) {
238
+ opts ||= { } ;
239
+ opts . dispatcher = mockAgent ;
240
+ return fetch ( url , opts ) ;
241
+ }
242
+
243
+ const publicKeys = [
244
+ {
245
+ key : "<key 1>" ,
246
+ key_identifier : "<key-id 1>" ,
247
+ is_current : true ,
248
+ } ,
249
+ {
250
+ key : "<key 2>" ,
251
+ key_identifier : "<key-id 2>" ,
252
+ is_current : true ,
253
+ } ,
254
+ ] ;
255
+
256
+ mockAgent . disableNetConnect ( ) ;
257
+ const mockPool = mockAgent . get ( "https://api.github.com" ) ;
258
+ const token = "secr3t" ;
259
+ mockPool
260
+ . intercept ( {
261
+ method : "get" ,
262
+ path : `/meta/public_keys/copilot_api` ,
263
+ headers : {
264
+ Authorization : `token ${ token } ` ,
173
265
} ,
266
+ } )
267
+ . reply (
268
+ 200 ,
269
+ {
270
+ public_keys : publicKeys ,
271
+ } ,
272
+ {
273
+ headers : {
274
+ "content-type" : "application/json" ,
275
+ "x-request-id" : "<request-id>" ,
276
+ } ,
277
+ }
174
278
) ;
175
279
const testRequest = defaultRequest . defaults ( {
176
280
request : { fetch : fetchMock } ,
177
281
} ) ;
178
282
179
283
const result = await fetchVerificationKeys ( {
284
+ token,
180
285
request : testRequest ,
181
286
} ) ;
182
287
0 commit comments