Skip to content

Commit

Permalink
chore: minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr committed Nov 17, 2023
1 parent c69ec7a commit dd94e4c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 42 deletions.
16 changes: 8 additions & 8 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
//
import type { Term, Quad, NamedNode, BlankNode, Literal } from "@rdfjs/types";
import { DataFactory } from "n3";
import { rdf } from "rdf-namespaces";
import { rdf, acl as _acl } from "rdf-namespaces";

const { namedNode } = DataFactory;

Expand All @@ -40,15 +40,15 @@ export const gc = {
hasConsent: namedNode(`${GC}hasConsent`),
isProvidedTo: namedNode(`${GC}isProvidedTo`),
isConsentForDataSubject: namedNode(`${GC}isConsentForDataSubject`),
}


};

export const acl = {
Read: namedNode(_acl.Read),
Write: namedNode(_acl.Write),
Append: namedNode(_acl.Append),
mode: namedNode(_acl.mode),
};

export const MODE = namedNode(`${ACL}mode`);
export const READ = namedNode(`${ACL}Read`);
export const WRITE = namedNode(`${ACL}Write`);
export const APPEND = namedNode(`${ACL}Append`);
export const ISSUANCE_DATE = namedNode(`${CRED}issuanceDate`);
export const EXPIRATION_DATE = namedNode(`${CRED}expirationDate`);
export const ISSUER = namedNode(`${CRED}issuer`);
Expand Down
90 changes: 63 additions & 27 deletions src/common/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,23 @@ import type { AccessGrantGConsent } from "../gConsent/type/AccessGrant";
import type { AccessRequestGConsent } from "../gConsent/type/AccessRequest";
import type { AccessModes } from "../type/AccessModes";
import {
APPEND,
CREDENTIAL_SUBJECT,
EXPIRATION_DATE,
INHERIT,
ISSUANCE_DATE,
ISSUER,
MODE,
READ,
WRITE,
XSD_BOOLEAN,
assertTermType,
getSingleQuad,
gc
gc,
acl,
} from "./constants";
import { GC_CONSENT_STATUS_DENIED, GC_CONSENT_STATUS_EXPLICITLY_GIVEN, GC_FOR_PERSONAL_DATA, GC_HAS_STATUS } from "../gConsent/constants";
import {
GC_CONSENT_STATUS_DENIED,
GC_CONSENT_STATUS_EXPLICITLY_GIVEN,
GC_FOR_PERSONAL_DATA,
GC_HAS_STATUS,
} from "../gConsent/constants";

const { namedNode, defaultGraph, quad, literal } = DataFactory;

Expand All @@ -65,9 +67,16 @@ export function getResources(
): string[] {
const resources: string[] = [];

for (const { object } of vc.match(getConsent(vc), namedNode(GC_FOR_PERSONAL_DATA), null, defaultGraph())) {
if (object.termType !== 'NamedNode') {
throw new Error(`Expected resource to be a Named Node. Instead got [${object.value}] with term type [${object.termType}]`)
for (const { object } of vc.match(
getConsent(vc),

Check warning on line 71 in src/common/getters.ts

View workflow job for this annotation

GitHub Actions / lint / lint

'getConsent' was used before it was defined
namedNode(GC_FOR_PERSONAL_DATA),
null,
defaultGraph(),
)) {
if (object.termType !== "NamedNode") {
throw new Error(
`Expected resource to be a Named Node. Instead got [${object.value}] with term type [${object.termType}]`,
);
}
resources.push(object.value);
}
Expand All @@ -80,14 +89,30 @@ export function isGConsentAccessGrant(
): boolean {
const credentialSubject = getCredentialSubject(vc);

Check warning on line 90 in src/common/getters.ts

View workflow job for this annotation

GitHub Actions / lint / lint

'getCredentialSubject' was used before it was defined
try {
const providedConsent = getSingleObject(vc, credentialSubject, gc.providedConsent, 'BlankNode');
const providedConsent = getSingleObject(

Check warning on line 92 in src/common/getters.ts

View workflow job for this annotation

GitHub Actions / lint / lint

'getSingleObject' was used before it was defined
vc,
credentialSubject,
gc.providedConsent,
"BlankNode",
);
return (
(
vc.has(quad(providedConsent, namedNode(GC_HAS_STATUS), namedNode(GC_CONSENT_STATUS_DENIED)))
|| vc.has(quad(providedConsent, namedNode(GC_HAS_STATUS), namedNode(GC_CONSENT_STATUS_EXPLICITLY_GIVEN)))
// Because of the getSingleObject the try / catch is also needed to wrap this as well as getting the provided consent
) && !!getSingleObject(vc, providedConsent, gc.isProvidedTo)
)
(vc.has(
quad(
providedConsent,
namedNode(GC_HAS_STATUS),
namedNode(GC_CONSENT_STATUS_DENIED),
),
) ||
vc.has(
quad(
providedConsent,
namedNode(GC_HAS_STATUS),
namedNode(GC_CONSENT_STATUS_EXPLICITLY_GIVEN),
),
)) &&
// Because of the getSingleObject the try / catch is also needed to wrap this as well as getting the provided consent
!!getSingleObject(vc, providedConsent, gc.isProvidedTo)
);
} catch (e) {
return false;
}
Expand Down Expand Up @@ -120,9 +145,9 @@ export function getResourceOwner(
return getSingleObject(
vc,
// We should probably allow this to be Blank or Named Node
getSingleObject(vc, credentialSubject, gc.hasConsent, 'BlankNode'),
gc.isConsentForDataSubject
).value
getSingleObject(vc, credentialSubject, gc.hasConsent, "BlankNode"),
gc.isConsentForDataSubject,
).value;
} catch (e) {
return undefined;
}
Expand All @@ -136,16 +161,20 @@ function getConsent(vc: AccessGrantGConsent | AccessRequestGConsent) {
const credentialSubject = getCredentialSubject(vc);
const consents = [
...vc.match(credentialSubject, gc.providedConsent, null, defaultGraph()),
...vc.match(credentialSubject, gc.hasConsent, null, defaultGraph())
...vc.match(credentialSubject, gc.hasConsent, null, defaultGraph()),
];
if (consents.length !== 1) {
throw new Error(`Expected exactly 1 consent value. Found ${consents.length}.`)
throw new Error(
`Expected exactly 1 consent value. Found ${consents.length}.`,
);
}
const [{ object }] = consents;
if (object.termType !== "BlankNode" && object.termType !== "NamedNode") {
throw new Error(`Expected consent to be a Named Node or Blank Node, instead got ${object.termType}`)
throw new Error(
`Expected consent to be a Named Node or Blank Node, instead got ${object.termType}`,
);
}
return object
return object;
}

/**
Expand Down Expand Up @@ -226,9 +255,9 @@ export function getAccessModes(
): AccessModes {
const consent = getConsent(vc);
return {
read: vc.has(quad(consent, MODE, READ, defaultGraph())),
write: vc.has(quad(consent, MODE, WRITE, defaultGraph())),
append: vc.has(quad(consent, MODE, APPEND, defaultGraph())),
read: vc.has(quad(consent, acl.mode, acl.Read, defaultGraph())),
write: vc.has(quad(consent, acl.mode, acl.Write, defaultGraph())),
append: vc.has(quad(consent, acl.mode, acl.Append, defaultGraph())),
};
}

Expand Down Expand Up @@ -364,7 +393,14 @@ export function getIssuer(
export function getInherit(
vc: AccessGrantGConsent | AccessRequestGConsent,
): boolean {
return !vc.has(quad(getConsent(vc), INHERIT, literal("false", XSD_BOOLEAN), defaultGraph()));
return !vc.has(
quad(
getConsent(vc),
INHERIT,
literal("false", XSD_BOOLEAN),
defaultGraph(),
),
);
}

/**
Expand Down
13 changes: 6 additions & 7 deletions src/gConsent/guard/isAccessGrant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

import { ACCESS_GRANT_STATUS } from "../constants";
import type {
AccessGrantBody,
BaseAccessVcBody,
} from "../type/AccessVerifiableCredential";

export const GC_CONSENT_STATUS_DENIED =
"https://w3id.org/GConsent#ConsentStatusDenied";
export const GC_CONSENT_STATUS_EXPLICITLY_GIVEN =
"https://w3id.org/GConsent#ConsentStatusExplicitlyGiven";
export const GC_CONSENT_STATUS_REQUESTED =
"https://w3id.org/GConsent#ConsentStatusRequested";


import { ACCESS_GRANT_STATUS } from "../constants";
import type {
AccessGrantBody,
BaseAccessVcBody,
} from "../type/AccessVerifiableCredential";

export function isAccessGrant(
vc: BaseAccessVcBody,
): vc is BaseAccessVcBody & AccessGrantBody {
Expand Down

0 comments on commit dd94e4c

Please sign in to comment.