Skip to content

Commit 137aa70

Browse files
committed
feat(keto-client-wrapper): enhance OryAuthorizationGuard to handle partial public access
1 parent c44729a commit 137aa70

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

packages/keto-client-wrapper/src/lib/ory-authorization.guard.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
Type,
1212
} from '@nestjs/common';
1313
import { Reflector } from '@nestjs/core';
14+
import { PermissionApiExpandPermissionsRequest } from '@ory/client';
1415
import type { Observable } from 'rxjs';
1516

1617
import {
@@ -91,8 +92,26 @@ export const OryAuthorizationGuard = (
9192
}
9293

9394
try {
94-
const { data } = await this.oryService.checkPermission(result.value);
95-
return { allowed: data.allowed, relationTuple };
95+
if (result.value.subjectId || result.value.subjectSetNamespace) {
96+
const { data } = await this.oryService.checkPermission(
97+
result.value
98+
);
99+
return { allowed: data.allowed, relationTuple };
100+
}
101+
/**
102+
* !experimental and counter-inituitive: to use with care
103+
* We check that this resolves to no children, meaning that the object has no relations with any subject => it is public
104+
*/
105+
const { data } = await this.oryService.expandPermissions(
106+
result.value as PermissionApiExpandPermissionsRequest
107+
);
108+
/**
109+
* This Keto API endpoint has a quirk,it returns {code: 404, ... } when relation is not found
110+
* ? maybe the check should be more complex based on data.type or data.children[n].type
111+
**/
112+
//
113+
const allowed = !data.children || data.children.length === 0;
114+
return { allowed, relationTuple };
96115
} catch (error) {
97116
throw unauthorizedFactory.bind(this)(context, error);
98117
}

0 commit comments

Comments
 (0)