Skip to content

Commit

Permalink
feat: Added category for SIOPV2/OIDC4VP #1999
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Oct 20, 2023
1 parent 424df46 commit 599221f
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 104 deletions.
157 changes: 153 additions & 4 deletions lib/app/shared/helper_functions/helper_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,40 @@ Future<Map<String, dynamic>?> getPresentationDefinition({
}
}

Future<Map<String, dynamic>?> getClientMetada({
required Uri uri,
required DioClient client,
}) async {
try {
final keys = <String>[];
uri.queryParameters.forEach((key, value) => keys.add(key));

if (keys.contains('client_metadata')) {
final String clientMetaDataValue =
uri.queryParameters['client_metadata'] ?? '';

final json = jsonDecode(clientMetaDataValue.replaceAll("'", '"'))
as Map<String, dynamic>;

return json;
} else if (keys.contains('client_metadata_uri')) {
final clientMetaDataUri =
uri.queryParameters['client_metadata_uri'].toString();
final dynamic response = await client.get(clientMetaDataUri);

final Map<String, dynamic> data = response == String
? jsonDecode(response.toString()) as Map<String, dynamic>
: response as Map<String, dynamic>;

return data;
} else {
return null;
}
} catch (e) {
return null;
}
}

Future<bool?> isEBSIV3ForVerifiers({
required Uri uri,
required DioClient client,
Expand Down Expand Up @@ -1016,16 +1050,45 @@ ${openidConfigurationResponse != null ? const JsonEncoder.withIndent(' ').conve
''';
}

String getFormattedStringOIDC4VPSIOPV2({
Future<String> getFormattedStringOIDC4VPSIOPV2({
required String url,
required Map<String, dynamic>? presentationDefinition,
}) {
return '''
required DioClient client,
required Map<String, dynamic>? response,
}) async {
final Map<String, dynamic>? presentationDefinition =
await getPresentationDefinition(
client: client,
uri: Uri.parse(url),
);

final Map<String, dynamic>? clientMetaData = await getClientMetada(
client: client,
uri: Uri.parse(url),
);

final registration = Uri.parse(url).queryParameters['registration'];

final registrationMap = registration != null
? jsonDecode(registration) as Map<String, dynamic>
: null;

final data = '''
SCHEME : ${getSchemeFromUrl(url)}
\n
AUTHORIZATION REQUEST :
${response != null ? const JsonEncoder.withIndent(' ').convert(response) : 'None'}
\n
CLIENT METADATA :
${clientMetaData != null ? const JsonEncoder.withIndent(' ').convert(clientMetaData) : 'None'}
\n
PRESENTATION DEFINITION :
${presentationDefinition != null ? const JsonEncoder.withIndent(' ').convert(presentationDefinition) : 'None'}
\n
REGISTRATION :
${registrationMap != null ? const JsonEncoder.withIndent(' ').convert(registrationMap) : 'None'}
''';

return data;
}

String getSchemeFromUrl(String url) {
Expand All @@ -1052,3 +1115,89 @@ Future<dynamic> fetchRequestUriPayload({
}
return data;
}

String getUpdatedUrlForSIOPV2OIC4VP({
required String url,
required Map<String, dynamic> response,
}) {
final responseType = response['response_type'];
final redirectUri = response['redirect_uri'];
final scope = response['scope'];
final responseUri = response['response_uri'];
final responseMode = response['response_mode'];
final nonce = response['nonce'];
final clientId = response['client_id'];
final claims = response['claims'];
final stateValue = response['state'];
final presentationDefinition = response['presentation_definition'];
final presentationDefinitionUri = response['presentation_definition_uri'];
final registration = response['registration'];
final clientMetadata = response['client_metadata'];
final clientMetadataUri = response['client_metadata_uri'];

final queryJson = <String, dynamic>{};

if (scope != null) {
queryJson['scope'] = scope;
}

if (clientId != null) {
queryJson['client_id'] = clientId;
}

if (redirectUri != null) {
queryJson['redirect_uri'] = redirectUri;
}

if (responseUri != null) {
queryJson['response_uri'] = responseUri;
}

if (responseMode != null) {
queryJson['response_mode'] = responseMode;
}

if (nonce != null) {
queryJson['nonce'] = nonce;
}

if (stateValue != null) {
queryJson['state'] = stateValue;
}

if (responseType != null) {
queryJson['response_type'] = responseType;
}

if (claims != null) {
queryJson['claims'] = jsonEncode(claims).replaceAll('"', "'");
}

if (presentationDefinition != null) {
queryJson['presentation_definition'] =
jsonEncode(presentationDefinition).replaceAll('"', "'");
}

if (presentationDefinitionUri != null) {
queryJson['presentation_definition_uri'] = presentationDefinitionUri;
}

if (registration != null) {
queryJson['registration'] =
registration is Map ? jsonEncode(registration) : registration;
}

if (clientMetadata != null) {
queryJson['client_metadata'] =
jsonEncode(clientMetadata).replaceAll('"', "'");
}

if (clientMetadataUri != null) {
queryJson['client_metadata_uri'] = clientMetadataUri;
}

final String queryString = Uri(queryParameters: queryJson).query;

final String newUrl = '$url&$queryString';
return newUrl;
}
82 changes: 11 additions & 71 deletions lib/dashboard/qr_code/qr_code_scan/cubit/qr_code_scan_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -463,81 +463,21 @@ class QRCodeScanCubit extends Cubit<QRCodeScanState> {
Future<void> startSIOPV2OIDC4VPProcess(Uri uri) async {
final String? requestUri = uri.queryParameters['request_uri'];
final String? request = uri.queryParameters['request'];
dynamic responseType;

/// check if request uri is provided or not
if (requestUri != null || request != null) {
/// verifier side (oidc4vp) or (siopv2 oidc4vc) with request_uri
/// afer verification process
final Map<String, dynamic> response =
decodePayload(jwtDecode: jwtDecode, token: encodedData as String);
encodedData = null;

responseType = response['response_type'];
final redirectUri = response['redirect_uri'];
final scope = response['scope'];
final responseUri = response['response_uri'];
final responseMode = response['response_mode'];
final nonce = response['nonce'];
final clientId = response['client_id'];
final claims = response['claims'];
final stateValue = response['state'];
final presentationDefinition = response['presentation_definition'];
final presentationDefinitionUri = response['presentation_definition_uri'];
final registration = response['registration'];

final queryJson = <String, dynamic>{};

if (scope != null) {
queryJson['scope'] = scope;
}

if (clientId != null) {
queryJson['client_id'] = clientId;
}

if (redirectUri != null) {
queryJson['redirect_uri'] = redirectUri;
}

if (responseUri != null) {
queryJson['response_uri'] = responseUri;
}

if (responseMode != null) {
queryJson['response_mode'] = responseMode;
}

if (nonce != null) {
queryJson['nonce'] = nonce;
}

if (stateValue != null) {
queryJson['state'] = stateValue;
}
if (responseType != null) {
queryJson['response_type'] = responseType;
}
if (claims != null) {
queryJson['claims'] = jsonEncode(claims).replaceAll('"', "'");
}
if (presentationDefinition != null) {
queryJson['presentation_definition'] =
jsonEncode(presentationDefinition).replaceAll('"', "'");
}

if (presentationDefinitionUri != null) {
queryJson['presentation_definition_uri'] = presentationDefinitionUri;
}

if (registration != null) {
queryJson['registration'] =
registration is Map ? jsonEncode(registration) : registration;
}

final String queryString = Uri(queryParameters: queryJson).query;
final Map<String, dynamic> response = decodePayload(
jwtDecode: jwtDecode,
token: encodedData as String,
);

final String newUrl = '$uri&$queryString';
final String newUrl = getUpdatedUrlForSIOPV2OIC4VP(
url: uri.toString(),
response: response,
);
encodedData = null;

emit(
state.copyWith(
Expand All @@ -546,10 +486,10 @@ class QRCodeScanCubit extends Cubit<QRCodeScanState> {
),
);
log.i('uri - $newUrl');
} else {
responseType = uri.queryParameters['response_type'] ?? '';
}

final responseType = uri.queryParameters['response_type'] ?? '';

/// check required keys available or not
final keys = <String>[];
state.uri?.queryParameters.forEach((key, value) => keys.add(key));
Expand Down
40 changes: 11 additions & 29 deletions lib/splash/bloclisteners/blocklisteners.dart
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ final qrCodeBlocListener = BlocListener<QRCodeScanCubit, QRCodeScanState>(
state.uri!.queryParameters['request_uri'];
final String? request = state.uri!.queryParameters['request'];

Map<String, dynamic>? response;

if (requestUri != null || request != null) {
late dynamic encodedData;
if (requestUri != null) {
Expand All @@ -285,42 +287,22 @@ final qrCodeBlocListener = BlocListener<QRCodeScanCubit, QRCodeScanState>(
} else {
encodedData = request;
}
final Map<String, dynamic> response = decodePayload(

response = decodePayload(
jwtDecode: JWTDecode(),
token: encodedData as String,
);

final presentationDefinition =
response['presentation_definition'];
final presentationDefinitionUri =
response['presentation_definition_uri'];

final queryJson = <String, dynamic>{};

if (presentationDefinition != null) {
queryJson['presentation_definition'] =
jsonEncode(presentationDefinition).replaceAll('"', "'");
}

if (presentationDefinitionUri != null) {
queryJson['presentation_definition_uri'] =
presentationDefinitionUri;
}

final String queryString =
Uri(queryParameters: queryJson).query;

url = '${state.uri}}&$queryString';
url = getUpdatedUrlForSIOPV2OIC4VP(
url: state.uri.toString(),
response: response,
);
}

final Map<String, dynamic>? presentationDefinitionData =
await getPresentationDefinition(
formattedData = await getFormattedStringOIDC4VPSIOPV2(
url: url,
client: client,
uri: Uri.parse(url),
);
formattedData = getFormattedStringOIDC4VPSIOPV2(
url: state.uri.toString(),
presentationDefinition: presentationDefinitionData,
response: response,
);
}

Expand Down

0 comments on commit 599221f

Please sign in to comment.