Skip to content

Commit

Permalink
feat: use PresentationDefinition id to initiate KCC application (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tbd authored Aug 21, 2024
1 parent c66bba7 commit 520bf5f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 27 deletions.
13 changes: 11 additions & 2 deletions lib/features/kcc/kcc_consent_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:web5/web5.dart';

class KccConsentPage extends HookConsumerWidget {
final Pfi pfi;
final PresentationDefinition presentationDefinition;

const KccConsentPage({required this.pfi, super.key});
const KccConsentPage({
required this.pfi,
required this.presentationDefinition,
super.key,
});

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand Down Expand Up @@ -44,7 +50,10 @@ class KccConsentPage extends HookConsumerWidget {
? null
: () => Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => KccWebviewPage(pfi: pfi),
builder: (_) => KccWebviewPage(
pfi: pfi,
presentationDefinition: presentationDefinition,
),
),
),
),
Expand Down
17 changes: 14 additions & 3 deletions lib/features/kcc/kcc_issuance_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,12 @@ class KccIssuanceService {

Future<IdvRequest> getIdvRequest(
Pfi pfi,
PresentationDefinition presentationDefinition,
BearerDid bearerDid,
) async {
final idvServiceEndpoint = await getIdvServiceEndpoint(pfi);
final idvServiceEndpoint =
await getIdvServiceEndpoint(pfi, presentationDefinition);

var response = await httpClient.get(idvServiceEndpoint);

final authRequest = await SiopV2AuthRequest.fromUrlParams(response.body);
Expand Down Expand Up @@ -373,7 +376,8 @@ class KccIssuanceService {
return getVerifiableCredential(pfi, idvRequest, tokenResponse, bearerDid);
}

Future<Uri> getIdvServiceEndpoint(Pfi pfi) async {
Future<Uri> getIdvServiceEndpoint(
Pfi pfi, PresentationDefinition presentationDefinition) async {
try {
final res = await DidResolver.resolve(pfi.did);
if (res.hasError()) {
Expand All @@ -386,7 +390,14 @@ class KccIssuanceService {
throw Exception('Malformed resolution result: missing DID Document');
}

return PfisService.getServiceEndpoint(res.didDocument!, 'IDV');
final idvServiceEndpoint =
PfisService.getServiceEndpoint(res.didDocument!, 'IDV');

return idvServiceEndpoint.replace(
queryParameters: {
'presentation_definition_id': presentationDefinition.id,
},
);
} on Exception catch (e) {
throw CredentialRequestException(
message: 'failed to resolve PFI DID',
Expand Down
9 changes: 6 additions & 3 deletions lib/features/kcc/kcc_webview_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:web5/web5.dart';

class KccWebviewPage extends HookConsumerWidget {
final Pfi pfi;
final PresentationDefinition presentationDefinition;

const KccWebviewPage({
required this.pfi,
required this.presentationDefinition,
super.key,
});

Expand Down Expand Up @@ -77,9 +80,9 @@ class KccWebviewPage extends HookConsumerWidget {
),
),
);
} else {
controller.loadUrl(urlRequest: URLRequest(url: WebUri(fullPath)));
}

controller.loadUrl(urlRequest: URLRequest(url: WebUri(fullPath)));
},
onLoadStop: (controller, url) async {
if (url == null) {
Expand Down Expand Up @@ -131,7 +134,7 @@ class KccWebviewPage extends HookConsumerWidget {
try {
final idvRequest = await ref
.read(kccIssuanceProvider)
.getIdvRequest(pfi, ref.read(didProvider));
.getIdvRequest(pfi, presentationDefinition, ref.read(didProvider));

if (context.mounted) {
state.value = AsyncData(idvRequest);
Expand Down
34 changes: 15 additions & 19 deletions lib/features/payment/payment_details_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,23 @@ class PaymentDetailsPage extends HookConsumerWidget {
);

if (credentials == null) {
final issuedCredential = await _startKccFlow(context);
final issuedCredential = await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ModalFlow(
initialWidget: KccConsentPage(
pfi: Pfi(
did: paymentState.paymentAmountState?.pfiDid ?? '',
),
presentationDefinition: presentationDefinition,
),
),
fullscreenDialog: true,
),
);

if (issuedCredential == null) return;

credentials = issuedCredential;
credentials = [issuedCredential as String];
}

state.value = state.value.copyWith(credentialsJwt: credentials);
Expand Down Expand Up @@ -262,23 +275,6 @@ class PaymentDetailsPage extends HookConsumerWidget {
);
}

Future<List<String>?> _startKccFlow(BuildContext context) async {
final credential = await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ModalFlow(
initialWidget: KccConsentPage(
pfi: Pfi(
did: paymentState.paymentAmountState?.pfiDid ?? '',
),
),
),
fullscreenDialog: true,
),
);

return credential == null ? null : [credential as String];
}

Future<void> _sendRfq(
BuildContext context,
WidgetRef ref,
Expand Down

0 comments on commit 520bf5f

Please sign in to comment.