Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VC support for alternative origins #2211

Merged
merged 3 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions demos/test-app/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ let latestOpts:
| undefined
| {
issuerOrigin: string;
derivationOrigin?: string;
credTy: CredType;
flowId: number;
win: Window;
Expand Down Expand Up @@ -408,6 +409,7 @@ function handleFlowReady(evnt: MessageEvent) {
},
credentialSpec: credentialSpecs[opts.credTy],
credentialSubject: principal,
derivationOrigin: opts.derivationOrigin,
},
};

Expand Down Expand Up @@ -458,6 +460,9 @@ const App = () => {
"http://issuer.localhost:5173"
);

// Alternative origin for the RP, if any
const [derivationOrigin, setDerivationOrigin] = useState<string>("");

// Continuously incrementing flow IDs used in the JSON RPC messages
const [nextFlowId, setNextFlowId] = useState(0);

Expand Down Expand Up @@ -486,6 +491,7 @@ const App = () => {
flowId,
credTy,
issuerOrigin: issuerUrl,
derivationOrigin: derivationOrigin !== "" ? derivationOrigin : undefined,
win: iiWindow,
};

Expand All @@ -504,6 +510,15 @@ const App = () => {
onChange={(evt) => setIssuerUrl(evt.target.value)}
/>
</label>
<label>
Alternative Derivation Origin:
<input
data-role="derivation-origin-rp"
type="text"
placeholder="(use default)"
onChange={(evt) => setDerivationOrigin(evt.target.value)}
/>
</label>

<button
data-action="verify-employee"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"aborted_bad_principal_rp": "The principal provided by the relying party does not match the data Internet Identity has.",
"aborted_no_canister_id": "Internet Identity could not find the canister for the issuer.",
"aborted_bad_canister_id": "Internet Identity could not confirm the canister ID given for the issuer.",
"aborted_bad_derivation_origin_rp": "Internet Identity could not confirm the derivation origin for the relying party.",
"notice": "We will now let the relying party know there was an issue.",
"you_may_close": "You may now close this page."
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export type AbortReason =
| "issuer_api_error"
| "bad_principal_rp"
| "no_canister_id"
| "bad_canister_id";
| "bad_canister_id"
| "bad_derivation_origin_rp";

/* A screen telling the user the flow was aborted and giving information
* on why it was aborted and what they can do about it. */
Expand All @@ -42,7 +43,8 @@ const abortedCredentialsTemplate = ({

const slot = html`
<hgroup
data-page="vc-allow"
data-page="vc-aborted"
data-abort-reason=${reason}
${scrollToTop ? mount(() => window.scrollTo(0, 0)) : undefined}
>
<h1 class="t-title t-title--main">${copy.title}</h1>
Expand Down
19 changes: 16 additions & 3 deletions src/frontend/src/flows/verifiableCredentials/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { withLoader } from "$src/components/loader";
import { showMessage } from "$src/components/message";
import { showSpinner } from "$src/components/spinner";
import { fetchDelegation } from "$src/flows/authorize/fetchDelegation";
import { validateDerivationOrigin } from "$src/flows/authorize/validateDerivationOrigin";

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a later PR: Given it is now used in multiple flows, this could be moved to some flow independent infrastructure place? Also the documentation in this file does not convey that it is used in multiple flows.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep definitely! I also want to change the interface:

  • the args are unnamed making it easy to swap the origins by mistake
  • the validation should not be allowed to be called with an undefined derivationOrigin; handling that case should be the caller's responsibility

it's somewhere on my TODO list, but since we already pull some code from the authorize flow elsewhere I thought this wouldn't hurt

import { getAnchorByPrincipal } from "$src/storage";
import { AuthenticatedConnection, Connection } from "$src/utils/iiConnection";
import {
Expand Down Expand Up @@ -69,8 +70,9 @@ const verifyCredentials = async ({
credentialSubject: givenP_RP,
issuer: { origin: issuerOrigin, canisterId: expectedIssuerCanisterId_ },
credentialSpec,
derivationOrigin: rpDerivationOrigin,
},
rpOrigin,
rpOrigin: rpOrigin_,
}: { connection: Connection } & VerifyCredentialsArgs) => {
// Look up the canister ID from the origin
const lookedUp = await withLoader(() =>
Expand All @@ -89,6 +91,15 @@ const verifyCredentials = async ({
}
}

const validationResult = await withLoader(() =>
validateDerivationOrigin(rpOrigin_, rpDerivationOrigin)
);
if (validationResult.result === "invalid") {
return abortedCredentials({ reason: "bad_derivation_origin_rp" });
}
validationResult.result satisfies "valid";
const rpOrigin = rpDerivationOrigin ?? rpOrigin_;

const vcIssuer = new VcIssuer(issuerCanisterId);
// XXX: We don't check that the language matches the user's language. We need
// to figure what to do UX-wise first.
Expand All @@ -99,13 +110,15 @@ const verifyCredentials = async ({
}

const userNumber_ = await getAnchorByPrincipal({
origin: rpOrigin,
origin:
rpOrigin_ /* NOTE: the storage uses the request origin, not the derivation origin */,
principal: givenP_RP,
});

// Ask user to confirm the verification of credentials
const allowed = await allowCredentials({
relyingOrigin: rpOrigin,
relyingOrigin:
rpOrigin_ /* NOTE: the design does not show the derivation origin (yet) */,
providerOrigin: issuerOrigin,
consentMessage: consentInfo.consent_message,
userNumber: userNumber_,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { runInBrowser } from "$src/test-e2e/util";
import { DemoAppView } from "$src/test-e2e/views";

import {
II_URL,
ISSUER_APP_URL,
TEST_APP_CANONICAL_URL,
TEST_APP_NICE_URL,
} from "$src/test-e2e/constants";

import {
authenticateToRelyingParty,
getVCPresentation,
getVCPresentation_,
register,
registerWithIssuer,
} from "./utils";

test("Can issue credential with alternative RP derivation origin", async () => {
await runInBrowser(async (browser: WebdriverIO.Browser) => {
await browser.url(II_URL);

const authConfig = await register["webauthn"](browser);

// 1. Add employee

const { msg: _msg, principal: _principal } = await registerWithIssuer({
browser,
issuer: ISSUER_APP_URL,
authConfig,
});

// 2. Do the flow without alt origins

const vcTestApp = await authenticateToRelyingParty({
browser,
issuer: ISSUER_APP_URL,
authConfig,
relyingParty: TEST_APP_CANONICAL_URL,
});
const principalRP = await vcTestApp.getPrincipal();
const { alias: alias_ } = await getVCPresentation({
vcTestApp,
browser,
authConfig,
});
const alias = JSON.parse(alias_);

// 3. Do the flow WITH alt origins
const vcTestAppAlt = await authenticateToRelyingParty({
browser,
issuer: ISSUER_APP_URL,
authConfig,
relyingParty: TEST_APP_NICE_URL,
derivationOrigin: TEST_APP_CANONICAL_URL,
});
const principalRPAlt = await vcTestAppAlt.getPrincipal();
expect(principalRPAlt).toBe(principalRP);

const { alias: aliasAlt_ } = await getVCPresentation({
vcTestApp: vcTestAppAlt,
browser,
authConfig,
});

const aliasAlt = JSON.parse(aliasAlt_);
expect(aliasAlt.sub).toBe(alias.sub);
});
}, 300_000);

test("Cannot issue credential with bad alternative RP derivation origin", async () => {
await runInBrowser(async (browser: WebdriverIO.Browser) => {
await browser.url(II_URL);

const authConfig = await register["webauthn"](browser);

// 1. Add employee

const { msg: _msg, principal: _principal } = await registerWithIssuer({
browser,
issuer: ISSUER_APP_URL,
authConfig,
});

// 2. Do the flow WITH alt origins RESET

const vcTestAppAlt = await authenticateToRelyingParty({
browser,
issuer: ISSUER_APP_URL,
authConfig,
relyingParty: TEST_APP_NICE_URL,
derivationOrigin: TEST_APP_CANONICAL_URL,
});

await new DemoAppView(browser).resetAlternativeOrigins();

const result = await getVCPresentation_({
vcTestApp: vcTestAppAlt,
browser,
authConfig,
});

expect(result.result).toBe("aborted");
if (!("reason" in result)) {
throw new Error("brwa");
nmattia marked this conversation as resolved.
Show resolved Hide resolved
}
expect(result.reason).toBe("bad_derivation_origin_rp");
});
}, 300_000);
111 changes: 111 additions & 0 deletions src/frontend/src/test-e2e/verifiableCredentials/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import { runInBrowser } from "$src/test-e2e/util";

import {
APPLE_USER_AGENT,
II_URL,
ISSUER_APP_URL,
ISSUER_APP_URL_LEGACY,
TEST_APP_CANONICAL_URL,
TEST_APP_CANONICAL_URL_LEGACY,
} from "$src/test-e2e/constants";

import {
authenticateToRelyingParty,
getVCPresentation,
register,
registerWithIssuer,
} from "./utils";

test("Can add employee on issuer app", async () => {
await runInBrowser(async (browser: WebdriverIO.Browser) => {
const authConfig = await register["webauthn"](browser);

const { msg, principal } = await registerWithIssuer({
browser,
authConfig,
issuer: ISSUER_APP_URL,
});

expect(msg).toContain("Added");
expect(msg).toContain(principal);
});
}, 300_000);

const getDomain = (url: string) => url.split(".").slice(1).join(".");

// The different test configs (different URLs, differnet auth methods)
const testConfigs: Array<{
relyingParty: string;
issuer: string;
authType: "pin" | "webauthn";
}> = [
{
relyingParty: TEST_APP_CANONICAL_URL_LEGACY,
issuer: ISSUER_APP_URL,
authType: "webauthn",
},
{
relyingParty: TEST_APP_CANONICAL_URL,
issuer: ISSUER_APP_URL_LEGACY,
authType: "webauthn",
},
{
relyingParty: TEST_APP_CANONICAL_URL,
issuer: ISSUER_APP_URL,
authType: "pin",
},
];

testConfigs.forEach(({ relyingParty, issuer, authType }) => {
const testSuffix = `RP: ${getDomain(relyingParty)}, ISS: ${getDomain(
issuer
)}, auth: ${authType}`;

test(
"Can issue credentials " + testSuffix,
async () => {
await runInBrowser(
async (browser: WebdriverIO.Browser) => {
await browser.url(II_URL);

const authConfig = await register[authType](browser);

// 1. Add employee

const { msg: _msg, principal: _principal } = await registerWithIssuer(
{
browser,
issuer,
authConfig,
}
);

// 2. Auth to RP

const vcTestApp = await authenticateToRelyingParty({
browser,
issuer,
authConfig,
relyingParty,
});

const principalRP = await vcTestApp.getPrincipal();

// 3. Get VC presentation

const { alias } = await getVCPresentation({
vcTestApp,
browser,
authConfig,
});

// Perform a basic check on the alias
const aliasObj = JSON.parse(alias);
expect(aliasObj.sub).toBe(`did:icp:${principalRP}`);
},
authType === "pin" ? APPLE_USER_AGENT : undefined
);
},
300_000
);
});
Loading
Loading