-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathWebIdAuthorizer.ts
More file actions
37 lines (31 loc) · 1.18 KB
/
WebIdAuthorizer.ts
File metadata and controls
37 lines (31 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import { ANY_RESOURCE, ANY_SCOPE, Authorizer } from './Authorizer';
import { Permission } from '../../views/Permission';
import { ClaimSet } from '../../credentials/ClaimSet';
import { WEBID } from '../../credentials/Claims';
import { getLoggerFor } from 'global-logger-factory';
/**
* An Authorizer granting access for WebID's to resources in given namespaces.
*/
export class WebIdAuthorizer implements Authorizer {
protected readonly logger = getLoggerFor(this);
/**
* Creates a PublicNamespaceAuthorizer with the given public namespaces.
*
* @param webids - The WebIDs that can be used.
*/
constructor(
protected webids: string[],
) {}
/** @inheritdoc */
public async permissions(claims: ClaimSet, query?: Partial<Permission>[]): Promise<Permission[]> {
this.logger.info(`Calculating permissions. ${JSON.stringify({ claims, query })}`);
const webid = claims[WEBID];
if (!(typeof webid === 'string' && this.webids.includes(webid))) return [];
return (query ?? []).map(
(permission): Permission => ({
resource_id: permission.resource_id ?? ANY_RESOURCE,
resource_scopes: permission.resource_scopes ?? [ ANY_SCOPE ]
})
);
}
}