@@ -17,9 +17,11 @@ bedrock.events.on('bedrock.init', async () => {
1717 REFRESHED_ZCAP_CACHE = new LruCache ( cfg . caches . refreshedZcap ) ;
1818} ) ;
1919
20- export async function getRefreshedZcap ( { profileId, capability} ) {
20+ export async function getRefreshedZcap ( {
21+ profileId, capability, capabilityChain
22+ } ) {
2123 const key = _createCacheKey ( { profileId, capability} ) ;
22- const fn = ( ) => _getUncached ( { profileId, capability} ) ;
24+ const fn = ( ) => _getUncached ( { profileId, capability, capabilityChain } ) ;
2325 return REFRESHED_ZCAP_CACHE . memoize ( { key, fn} ) ;
2426}
2527
@@ -50,7 +52,7 @@ async function _getZcapPolicy({profileId, delegateId}) {
5052 }
5153}
5254
53- async function _getUncached ( { profileId, capability} ) {
55+ async function _getUncached ( { profileId, capability, capabilityChain } ) {
5456 // get the policy for the profile + controller (delegate)
5557 const { controller : delegateId } = capability ;
5658 const { policy} = await _getZcapPolicy ( { profileId, delegateId} ) ;
@@ -92,10 +94,19 @@ async function _getUncached({profileId, capability}) {
9294
9395 // compute new `expires` from policy, defaulting to max delegation TTL
9496 const now = Date . now ( ) ;
95- const expires = new Date (
97+ let expires = new Date (
9698 now + ( policy . refresh ?. constraints ?. maxDelegationTtl ??
9799 authorizeZcapInvocationOptions . maxDelegationTtl ) ) ;
98100
101+ // ensure policy computed expiry doesn't exceed parent capability
102+ const parentZcap = capabilityChain . at ( - 2 ) ;
103+ if ( parentZcap . expires !== undefined ) {
104+ const parentExpires = new Date ( parentZcap . expires ) ;
105+ if ( expires > parentExpires ) {
106+ expires = parentExpires ;
107+ }
108+ }
109+
99110 return profileAgents . refreshCapability ( {
100111 capability, profileSigner, now, expires
101112 } ) ;
0 commit comments