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

chore: verify shared presentation #629

Merged
merged 5 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions .changeset/eleven-eyes-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@blockchain-lab-um/dapp": patch
---

Replaced shared presentation verifying lib.
1 change: 1 addition & 0 deletions packages/dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
},
"dependencies": {
"@blockchain-lab-um/did-provider-key": "1.0.8",
"@blockchain-lab-um/extended-verification": "0.1.2",
"@blockchain-lab-um/masca-connector": "1.3.2",
"@blockchain-lab-um/oidc-types": "0.0.8",
"@headlessui/react": "^1.7.18",
Expand Down
37 changes: 29 additions & 8 deletions packages/dapp/src/app/api/share/presentation/route.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import jwt from 'jsonwebtoken';
import { type NextRequest, NextResponse } from 'next/server';
import {
type VerificationResult,
VerificationService,
} from '@blockchain-lab-um/extended-verification';

import { getAgent } from '../../veramoSetup';
import { supabaseServiceRoleClient } from '@/utils/supabase/supabaseServiceRoleClient';
import type { W3CVerifiablePresentation } from '@veramo/core';
import { isError, type Result } from '@blockchain-lab-um/masca-connector';

const CORS_HEADERS = {
'Access-Control-Allow-Origin': '*',
Expand Down Expand Up @@ -54,20 +59,36 @@ export async function POST(request: NextRequest) {
});
}

const agent = await getAgent();
const { verified } = await agent.verifyPresentation({
presentation,
});
await VerificationService.init();
const verifiedResult: Result<VerificationResult> =
await VerificationService.verify(
presentation as W3CVerifiablePresentation
);

if (!verified) {
return new NextResponse('Presentation not valid', {
status: 400,
if (isError(verifiedResult)) {
return new NextResponse('Failed to verify presentation', {
status: 500,
headers: {
...CORS_HEADERS,
},
});
}

if (!verifiedResult.data.verified) {
return NextResponse.json(
{
message: 'Invalid presentation',
details: verifiedResult.data.details,
},
{
status: 400,
headers: {
...CORS_HEADERS,
},
}
);
}

const supabase = supabaseServiceRoleClient();

const { data, error } = await supabase
Expand Down
15 changes: 12 additions & 3 deletions packages/dapp/src/components/ShareCredentialModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,13 @@ export const ShareCredentialModal = () => {
try {
const result = await shareResponse.json();

if (!result.presentationId)
throw new Error('Failed to share presentation');
if (!result.presentationId) {
throw new Error(
result.message === 'Invalid presentation'
? 'Invalid presentation'
: 'Failed to share presentation'
);
}

setShareLink(
`${window.location.origin}/app/share-presentation/${result.presentationId}`
Expand All @@ -165,7 +170,10 @@ export const ShareCredentialModal = () => {
setTimeout(() => {
useToastStore.setState({
open: true,
title: t('share-presentation-error'),
title:
(e as Error).message === 'Invalid presentation'
? t('share-error-invalid')
: t('share-presentation-error'),
type: 'error',
loading: false,
link: null,
Expand All @@ -183,6 +191,7 @@ export const ShareCredentialModal = () => {
backdrop="blur"
size="4xl"
isOpen={isOpen}
isDismissable={false}
onClose={() => setIsOpen(false)}
hideCloseButton={true}
placement="center"
Expand Down
1 change: 1 addition & 0 deletions packages/dapp/src/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,7 @@
"placeholder": "Enter a title for the presentation. This can't be changed later.",
"please-sign-in": "Please sign in to share your credentials",
"selected": "Selected Credentials",
"share-error-invalid": "One or more credentials in the presentation are invalid.",
"share-presentation-error": "Failed to share presentation",
"share-presentation-success": "Successfully shared presentation",
"share-link-description": "Share your credential:",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.