@@ -260,3 +260,172 @@ describe('plan/set', function () {
260
260
assert . equal ( result . error ?. message . includes ( 'not authorized' ) , true )
261
261
} )
262
262
} )
263
+
264
+ describe ( 'plan/create-admin-session' , function ( ) {
265
+ const agent = alice
266
+ const account = 'did:mailto:mallory.com:mallory'
267
+ it ( 'can invoke as an account' , async function ( ) {
268
+ const auth = Plan . createAdminSession . invoke ( {
269
+ issuer : agent ,
270
+ audience : service ,
271
+ with : account ,
272
+ nb : {
273
+ returnURL : 'http://example.com/return' ,
274
+ } ,
275
+ proofs : await createAuthorization ( { agent, service, account } ) ,
276
+ } )
277
+ const result = await access ( await auth . delegate ( ) , {
278
+ capability : Plan . createAdminSession ,
279
+ principal : Verifier ,
280
+ authority : service ,
281
+ validateAuthorization,
282
+ } )
283
+ if ( result . error ) {
284
+ assert . fail ( `error in self issue: ${ result . error . message } ` )
285
+ } else {
286
+ assert . deepEqual ( result . ok . audience . did ( ) , service . did ( ) )
287
+ assert . equal ( result . ok . capability . can , 'plan/create-admin-session' )
288
+ assert . deepEqual ( result . ok . capability . with , account )
289
+ }
290
+ } )
291
+
292
+ it ( 'fails without account delegation' , async function ( ) {
293
+ const agent = alice
294
+ const auth = Plan . createAdminSession . invoke ( {
295
+ issuer : agent ,
296
+ audience : service ,
297
+ with : account ,
298
+ nb : {
299
+ returnURL : 'http://example.com/return' ,
300
+ } ,
301
+ } )
302
+
303
+ const result = await access ( await auth . delegate ( ) , {
304
+ capability : Plan . createAdminSession ,
305
+ principal : Verifier ,
306
+ authority : service ,
307
+ validateAuthorization,
308
+ } )
309
+
310
+ assert . equal ( result . error ?. message . includes ( 'not authorized' ) , true )
311
+ } )
312
+
313
+ it ( 'fails when invoked by a different agent' , async function ( ) {
314
+ const auth = Plan . createAdminSession . invoke ( {
315
+ issuer : bob ,
316
+ audience : service ,
317
+ with : account ,
318
+ nb : {
319
+ returnURL : 'http://example.com/return' ,
320
+ } ,
321
+ proofs : await createAuthorization ( { agent, service, account } ) ,
322
+ } )
323
+
324
+ const result = await access ( await auth . delegate ( ) , {
325
+ capability : Plan . createAdminSession ,
326
+ principal : Verifier ,
327
+ authority : service ,
328
+ validateAuthorization,
329
+ } )
330
+ assert . equal ( result . error ?. message . includes ( 'not authorized' ) , true )
331
+ } )
332
+
333
+ it ( 'can delegate plan/create-admin-session' , async function ( ) {
334
+ const invocation = Plan . createAdminSession . invoke ( {
335
+ issuer : bob ,
336
+ audience : service ,
337
+ with : account ,
338
+ nb : {
339
+ returnURL : 'http://example.com/return' ,
340
+ } ,
341
+ proofs : [
342
+ await Plan . createAdminSession . delegate ( {
343
+ issuer : agent ,
344
+ audience : bob ,
345
+ with : account ,
346
+ nb : {
347
+ returnURL : 'http://example.com/return' ,
348
+ } ,
349
+ proofs : await createAuthorization ( { agent, service, account } ) ,
350
+ } ) ,
351
+ ] ,
352
+ } )
353
+ const result = await access ( await invocation . delegate ( ) , {
354
+ capability : Plan . createAdminSession ,
355
+ principal : Verifier ,
356
+ authority : service ,
357
+ validateAuthorization,
358
+ } )
359
+ if ( result . error ) {
360
+ assert . fail ( `error in self issue: ${ result . error . message } ` )
361
+ } else {
362
+ assert . deepEqual ( result . ok . audience . did ( ) , service . did ( ) )
363
+ assert . equal ( result . ok . capability . can , 'plan/create-admin-session' )
364
+ assert . deepEqual ( result . ok . capability . with , account )
365
+ }
366
+ } )
367
+
368
+ it ( 'can invoke plan/create-admin-session with the return URL that its delegation specifies' , async function ( ) {
369
+ const invocation = Plan . createAdminSession . invoke ( {
370
+ issuer : bob ,
371
+ audience : service ,
372
+ with : account ,
373
+ nb : {
374
+ returnURL : 'http://example.com/return' ,
375
+ } ,
376
+ proofs : [
377
+ await Plan . createAdminSession . delegate ( {
378
+ issuer : agent ,
379
+ audience : bob ,
380
+ with : account ,
381
+ nb : {
382
+ returnURL : 'http://example.com/return' ,
383
+ } ,
384
+ proofs : await createAuthorization ( { agent, service, account } ) ,
385
+ } ) ,
386
+ ] ,
387
+ } )
388
+ const result = await access ( await invocation . delegate ( ) , {
389
+ capability : Plan . createAdminSession ,
390
+ principal : Verifier ,
391
+ authority : service ,
392
+ validateAuthorization,
393
+ } )
394
+ if ( result . error ) {
395
+ assert . fail ( `error in self issue: ${ result . error . message } ` )
396
+ } else {
397
+ assert . deepEqual ( result . ok . audience . did ( ) , service . did ( ) )
398
+ assert . equal ( result . ok . capability . can , 'plan/create-admin-session' )
399
+ assert . deepEqual ( result . ok . capability . with , account )
400
+ }
401
+ } )
402
+
403
+ it ( 'cannot invoke plan/create-admin-session with a different product than its delegation specifies' , async function ( ) {
404
+ const invocation = Plan . createAdminSession . invoke ( {
405
+ issuer : bob ,
406
+ audience : service ,
407
+ with : account ,
408
+ nb : {
409
+ returnURL : 'http://example.com/bad-return' ,
410
+ } ,
411
+ proofs : [
412
+ await Plan . createAdminSession . delegate ( {
413
+ issuer : agent ,
414
+ audience : bob ,
415
+ with : account ,
416
+ nb : {
417
+ returnURL : 'http://example.com/return' ,
418
+ } ,
419
+ proofs : await createAuthorization ( { agent, service, account } ) ,
420
+ } ) ,
421
+ ] ,
422
+ } )
423
+ const result = await access ( await invocation . delegate ( ) , {
424
+ capability : Plan . createAdminSession ,
425
+ principal : Verifier ,
426
+ authority : service ,
427
+ validateAuthorization,
428
+ } )
429
+ assert . equal ( result . error ?. message . includes ( 'not authorized' ) , true )
430
+ } )
431
+ } )
0 commit comments