Skip to content

Commit

Permalink
chore: refactor constants
Browse files Browse the repository at this point in the history
  • Loading branch information
jeswr committed Nov 23, 2023
1 parent 7bb1c43 commit 738a135
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 57 deletions.
15 changes: 10 additions & 5 deletions src/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ import { rdf, acl as _acl } from "rdf-namespaces";

const { namedNode } = DataFactory;

export const CRED = "https://www.w3.org/2018/credentials#";
export const GC = "https://w3id.org/GConsent#";
export const ACL = "http://www.w3.org/ns/auth/acl#";
export const VC = "http://www.w3.org/ns/solid/vc#";
export const XSD = "http://www.w3.org/2001/XMLSchema#";
const CRED = "https://www.w3.org/2018/credentials#";
const GC = "https://w3id.org/GConsent#";
const VC = "http://www.w3.org/ns/solid/vc#";
const XSD = "http://www.w3.org/2001/XMLSchema#";

export const XSD_BOOLEAN = namedNode(`${XSD}boolean`);

Expand All @@ -37,9 +36,15 @@ export const TYPE = namedNode(rdf.type);
export const gc = {
providedConsent: namedNode(`${GC}providedConsent`),
hasConsent: namedNode(`${GC}hasConsent`),
hasStatus: namedNode(`${GC}hasStatus`),
isProvidedTo: namedNode(`${GC}isProvidedTo`),
isConsentForDataSubject: namedNode(`${GC}isConsentForDataSubject`),
forPurpose: namedNode(`${GC}forPurpose`),
forPersonalData: namedNode(`${GC}forPersonalData`),

ConsentStatusDenied: namedNode(`${GC}ConsentStatusDenied`),
ConsentStatusExplicitlyGiven: namedNode(`${GC}ConsentStatusExplicitlyGiven`),
ConsentStatusRequested: namedNode(`${GC}ConsentStatusRequested`),
};

export const acl = {
Expand Down
3 changes: 1 addition & 2 deletions src/common/getters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {
getTypes,
} from "./getters";
import type { AccessGrant, AccessRequest } from "../gConsent";
import { GC_FOR_PERSONAL_DATA } from "../gConsent/constants";
import { TYPE, cred, gc } from "./constants";

const { quad, namedNode, literal, blankNode } = DataFactory;
Expand Down Expand Up @@ -77,7 +76,7 @@ describe("getters", () => {
...mockedGConsentGrant,
quad(
getConsent(mockedGConsentGrant),
namedNode(GC_FOR_PERSONAL_DATA),
gc.forPersonalData,
literal("hello world"),
),
]) as unknown as AccessGrant,
Expand Down
17 changes: 6 additions & 11 deletions src/common/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@ import type { AccessGrantGConsent } from "../gConsent/type/AccessGrant";
import type { AccessRequestGConsent } from "../gConsent/type/AccessRequest";
import type { AccessModes } from "../type/AccessModes";
import { INHERIT, XSD_BOOLEAN, gc, acl, cred, TYPE } from "./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 Down Expand Up @@ -188,7 +182,7 @@ export function getResources(

for (const { object } of vc.match(
getConsent(vc),
namedNode(GC_FOR_PERSONAL_DATA),
gc.forPersonalData,
null,
defaultGraph(),
)) {
Expand Down Expand Up @@ -255,16 +249,17 @@ export function isGConsentAccessGrant(
const gcStatus = getSingleObject(
vc,
providedConsent,
namedNode(GC_HAS_STATUS),
gc.hasStatus,
undefined,
false,
);

return (
gcStatus !== undefined &&
[GC_CONSENT_STATUS_DENIED, GC_CONSENT_STATUS_EXPLICITLY_GIVEN].includes(
gcStatus?.value,
) &&
(vc.has(quad(providedConsent, gc.hasStatus, gc.ConsentStatusDenied)) ||
vc.has(
quad(providedConsent, gc.hasStatus, gc.ConsentStatusExplicitlyGiven),
)) &&
getSingleObject(vc, providedConsent, gc.isProvidedTo, undefined, false)
?.termType === "NamedNode"
);
Expand Down
22 changes: 7 additions & 15 deletions src/gConsent/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,7 @@
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

export const GC_CONSENT = "https://w3id.org/GConsent#Consent";
export const GC_FOR_PERSONAL_DATA = "https://w3id.org/GConsent#forPersonalData";
export const GC_FOR_PURPOSE = "https://w3id.org/GConsent#forPurpose";
export const GC_HAS_EXPIRY = "https://w3id.org/GConsent#hasExpiry";
export const GC_HAS_STATUS = "https://w3id.org/GConsent#hasStatus";
export const GC_IS_PROVIDED_TO = "https://w3id.org/GConsent#isProvidedTo";
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 { gc } from "../common/constants";

export const GC_CONSENT_STATUS_DENIED_ABBREV = "ConsentStatusDenied";
export const GC_CONSENT_STATUS_EXPLICITLY_GIVEN_ABBREV =
Expand Down Expand Up @@ -101,8 +90,8 @@ export const ACCESS_CREDENTIAL_TYPE = new Set([

export const ACCESS_GRANT_STATUS = Object.freeze(
new Set([
GC_CONSENT_STATUS_DENIED,
GC_CONSENT_STATUS_EXPLICITLY_GIVEN,
gc.ConsentStatusDenied.value,
gc.ConsentStatusExplicitlyGiven.value,
GC_CONSENT_STATUS_DENIED_ABBREV,
"gc:ConsentStatusDenied",
"Consent:StatusDenied",
Expand All @@ -111,7 +100,10 @@ export const ACCESS_GRANT_STATUS = Object.freeze(
);

export const ACCESS_REQUEST_STATUS = Object.freeze(
new Set([GC_CONSENT_STATUS_REQUESTED, GC_CONSENT_STATUS_REQUESTED_ABBREV]),
new Set([
gc.ConsentStatusRequested.value,
GC_CONSENT_STATUS_REQUESTED_ABBREV,
]),
);

export const ACCESS_STATUS = Object.freeze(
Expand Down
4 changes: 2 additions & 2 deletions src/gConsent/manage/approveAccessRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import type { AccessModes } from "../../type/AccessModes";
import type { AccessGrant } from "../type/AccessGrant";
import { getGrantBody, issueAccessVc } from "../util/issueAccessVc";
import { isAccessRequest } from "../guard/isAccessRequest";
import { GC_CONSENT_STATUS_EXPLICITLY_GIVEN } from "../constants";
import {
ACL_RESOURCE_ACCESS_MODE_APPEND,
ACL_RESOURCE_ACCESS_MODE_READ,
Expand All @@ -41,6 +40,7 @@ import { initializeGrantParameters } from "../util/initializeGrantParameters";
import { getSessionFetch } from "../../common/util/getSessionFetch";
import { isAccessGrant } from "../guard/isAccessGrant";
import { isBaseAccessGrantVerifiableCredential } from "../guard/isBaseAccessGrantVerifiableCredential";
import { gc } from "../../common/constants";

export type ApproveAccessRequestOverrides = Omit<
Omit<AccessGrantParameters, "status">,
Expand Down Expand Up @@ -185,7 +185,7 @@ async function internal_approveAccessRequest(
purpose: internalGrantOptions.purpose,
issuanceDate: internalGrantOptions.issuanceDate,
expirationDate: internalGrantOptions.expirationDate ?? undefined,
status: GC_CONSENT_STATUS_EXPLICITLY_GIVEN,
status: gc.ConsentStatusExplicitlyGiven.value,
inherit: internalGrantOptions.inherit,
});

Expand Down
11 changes: 4 additions & 7 deletions src/gConsent/manage/denyAccessRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@
import type { VerifiableCredential } from "@inrupt/solid-client-vc";
import { isVerifiableCredential } from "@inrupt/solid-client-vc";
import type { UrlString, WebId } from "@inrupt/solid-client";
import {
CREDENTIAL_TYPE_ACCESS_DENIAL,
GC_CONSENT_STATUS_DENIED,
GC_CONSENT_STATUS_EXPLICITLY_GIVEN,
} from "../constants";
import { CREDENTIAL_TYPE_ACCESS_DENIAL } from "../constants";
import type { AccessBaseOptions } from "../type/AccessBaseOptions";
import { getGrantBody, issueAccessVc } from "../util/issueAccessVc";
import { getBaseAccessRequestVerifiableCredential } from "../util/getBaseAccessVerifiableCredential";
import { initializeGrantParameters } from "../util/initializeGrantParameters";
import { normalizeAccessGrant } from "./approveAccessRequest";
import type { AccessGrant } from "../type/AccessGrant";
import { gc } from "../../common/constants";

// Merge back in denyAccessRequest after the deprecated signature has been removed.
// eslint-disable-next-line camelcase
Expand All @@ -51,15 +48,15 @@ async function internal_denyAccessRequest(
requestor: internalOptions.requestor,
resources: internalOptions.resources,
requestorInboxUrl: internalOptions.requestorInboxUrl,
status: GC_CONSENT_STATUS_EXPLICITLY_GIVEN,
status: gc.ConsentStatusExplicitlyGiven.value,
purpose: internalOptions.purpose,
// denyAccessRequest doesn't take an override, so the expiration date
// cannot be null.
expirationDate: internalOptions.expirationDate as Date | undefined,
});
denialBody.type = [CREDENTIAL_TYPE_ACCESS_DENIAL];
denialBody.credentialSubject.providedConsent.hasStatus =
GC_CONSENT_STATUS_DENIED;
gc.ConsentStatusDenied.value;

return issueAccessVc(denialBody, options);
}
Expand Down
7 changes: 3 additions & 4 deletions src/gConsent/manage/getAccessGrantAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ import {
CREDENTIAL_TYPE_ACCESS_DENIAL,
CREDENTIAL_TYPE_ACCESS_GRANT,
CREDENTIAL_TYPE_BASE,
GC_CONSENT_STATUS_DENIED,
GC_CONSENT_STATUS_EXPLICITLY_GIVEN,
} from "../constants";
import { getAccessApiEndpoint } from "../discover/getAccessApiEndpoint";
import type { AccessBaseOptions } from "../type/AccessBaseOptions";
Expand All @@ -42,6 +40,7 @@ import { isAccessGrant } from "../guard/isAccessGrant";
import { isBaseAccessGrantVerifiableCredential } from "../guard/isBaseAccessGrantVerifiableCredential";
import { getInherit, getResources } from "../../common/getters";
import { normalizeAccessGrant } from "./approveAccessRequest";
import { gc } from "../../common/constants";

export type AccessParameters = Partial<
Pick<IssueAccessRequestParameters, "access" | "purpose"> & {
Expand Down Expand Up @@ -124,8 +123,8 @@ async function internal_getAccessGrantAll(
credentialSubject: {
providedConsent: {
hasStatus: {
granted: GC_CONSENT_STATUS_EXPLICITLY_GIVEN,
denied: GC_CONSENT_STATUS_DENIED,
granted: gc.ConsentStatusExplicitlyGiven.value,
denied: gc.ConsentStatusDenied.value,
all: undefined,
}[statusShorthand],
forPersonalData: url ? [url.href] : undefined,
Expand Down
4 changes: 2 additions & 2 deletions src/gConsent/request/issueAccessRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@

import type { VerifiableCredential } from "@inrupt/solid-client-vc";
import { getRequestBody, issueAccessVc } from "../util/issueAccessVc";
import { GC_CONSENT_STATUS_REQUESTED } from "../constants";
import type { AccessBaseOptions } from "../type/AccessBaseOptions";
import type {
DeprecatedAccessRequestParameters,
IssueAccessRequestParameters,
} from "../type/IssueAccessRequestParameters";
import type { AccessRequest } from "../type/AccessRequest";
import { isAccessRequest } from "../guard/isAccessRequest";
import { gc } from "../../common/constants";

/**
* Internal function. This is a stopgap until we have proper JSON-LD parsing.
Expand Down Expand Up @@ -91,7 +91,7 @@ async function issueAccessRequest(
): Promise<AccessRequest> {
const requestBody = getRequestBody({
...params,
status: GC_CONSENT_STATUS_REQUESTED,
status: gc.ConsentStatusRequested.value,
});
const accessRequest = normalizeAccessRequest(
await issueAccessVc(requestBody, options),
Expand Down
10 changes: 3 additions & 7 deletions src/gConsent/type/Parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@
*/

import type { UrlString } from "@inrupt/solid-client";

import type {
GC_CONSENT_STATUS_REQUESTED,
GC_CONSENT_STATUS_EXPLICITLY_GIVEN,
} from "../constants";
import type { AccessModes } from "../../type/AccessModes";
import type { gc } from "../../common/constants";

export interface BaseRequestParameters {
access: AccessModes;
Expand All @@ -45,13 +41,13 @@ export interface InputAccessRequestParameters extends BaseRequestParameters {
resourceOwner: UrlString;
}
export interface AccessRequestParameters extends InputAccessRequestParameters {
status: typeof GC_CONSENT_STATUS_REQUESTED;
status: typeof gc.ConsentStatusRequested.value;
}

export interface InputAccessGrantParameters extends BaseRequestParameters {
requestor: UrlString;
}

export interface AccessGrantParameters extends InputAccessGrantParameters {
status: typeof GC_CONSENT_STATUS_EXPLICITLY_GIVEN;
status: typeof gc.ConsentStatusExplicitlyGiven.value;
}
4 changes: 2 additions & 2 deletions src/gConsent/util/access.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import type { ResourceAccessMode } from "../../type/ResourceAccessMode";
import {
CREDENTIAL_TYPE_ACCESS_GRANT,
CREDENTIAL_TYPE_ACCESS_REQUEST,
GC_CONSENT_STATUS_REQUESTED,
MOCK_CONTEXT,
} from "../constants";
import { normalizeAccessGrant } from "../manage/approveAccessRequest";
Expand All @@ -40,6 +39,7 @@ import type {
BaseGrantBody,
BaseRequestBody,
} from "../type/AccessVerifiableCredential";
import { gc } from "../../common/constants";

type RequestVcOptions = Partial<{
resources: UrlString[];
Expand All @@ -52,7 +52,7 @@ type RequestVcOptions = Partial<{
export const mockAccessRequestVcObject = (options?: RequestVcOptions) => {
const hasConsent = {
forPersonalData: options?.resources ?? ["https://some.resource"],
hasStatus: GC_CONSENT_STATUS_REQUESTED,
hasStatus: gc.ConsentStatusRequested.value,
mode: options?.modes ?? ["http://www.w3.org/ns/auth/acl#Read"],
isConsentForDataSubject: "https://some.pod/profile#you" as
| string
Expand Down

0 comments on commit 738a135

Please sign in to comment.