Skip to content

Commit

Permalink
code optimisation - fetch data once
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Feb 28, 2024
1 parent 9e20804 commit 445e5e1
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 167 deletions.
168 changes: 119 additions & 49 deletions lib/app/shared/helper_functions/helper_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:io';

import 'package:altme/app/app.dart';
import 'package:altme/dashboard/dashboard.dart';
import 'package:altme/dashboard/home/home.dart';
import 'package:altme/oidc4vc/oidc4vc.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:convert/convert.dart';
Expand Down Expand Up @@ -583,8 +582,17 @@ bool isSIOPV2OROIDC4VPUrl(Uri uri) {
return isOpenIdUrl || isAuthorizeEndPoint || isSiopv2Url;
}

Future<(OIDC4VCType?, OpenIdConfiguration?, OpenIdConfiguration?, dynamic)>
getIssuanceData({
/// OIDC4VCType?, OpenIdConfiguration?, OpenIdConfiguration?,
/// credentialOfferJson, issuer, pre-authorizedCode
Future<
(
OIDC4VCType?,
OpenIdConfiguration?,
OpenIdConfiguration?,
dynamic,
String?,
String?,
)> getIssuanceData({
required String url,
required DioClient client,
required OIDC4VC oidc4vc,
Expand All @@ -597,6 +605,7 @@ Future<(OIDC4VCType?, OpenIdConfiguration?, OpenIdConfiguration?, dynamic)>

dynamic credentialOfferJson;
String? issuer;
String? preAuthorizedCode;

if (keys.contains('credential_offer') ||
keys.contains('credential_offer_uri')) {
Expand All @@ -605,18 +614,32 @@ Future<(OIDC4VCType?, OpenIdConfiguration?, OpenIdConfiguration?, dynamic)>
scannedResponse: uri.toString(),
dioClient: client,
);
if (credentialOfferJson == null) throw Exception();

issuer = credentialOfferJson['credential_issuer'].toString();
if (credentialOfferJson != null) {
final dynamic preAuthorizedCodeGrant = credentialOfferJson['grants']
['urn:ietf:params:oauth:grant-type:pre-authorized_code'];

if (preAuthorizedCodeGrant != null &&
preAuthorizedCodeGrant is Map &&
preAuthorizedCodeGrant.containsKey('pre-authorized_code')) {
preAuthorizedCode =
preAuthorizedCodeGrant['pre-authorized_code'] as String;
}

issuer = credentialOfferJson['credential_issuer'].toString();
}
}

if (keys.contains('issuer')) {
/// issuance case 1
issuer = uri.queryParameters['issuer'].toString();

/// preAuthorizedCode can be null
preAuthorizedCode = uri.queryParameters['pre-authorized_code'];
}

if (issuer == null) {
return (null, null, null, null);
return (null, null, null, null, null, null);
}

final OpenIdConfiguration openIdConfiguration = await oidc4vc.getOpenIdConfig(
Expand Down Expand Up @@ -668,6 +691,8 @@ Future<(OIDC4VCType?, OpenIdConfiguration?, OpenIdConfiguration?, dynamic)>
openIdConfiguration,
authorizationServerConfiguration,
credentialOfferJson,
issuer,
preAuthorizedCode,
);
}

Expand All @@ -678,13 +703,17 @@ Future<(OIDC4VCType?, OpenIdConfiguration?, OpenIdConfiguration?, dynamic)>
openIdConfiguration,
authorizationServerConfiguration,
credentialOfferJson,
issuer,
preAuthorizedCode,
);
} else {
return (
OIDC4VCType.DEFAULT,
openIdConfiguration,
authorizationServerConfiguration,
credentialOfferJson,
issuer,
preAuthorizedCode,
);
}
}
Expand All @@ -693,6 +722,8 @@ Future<(OIDC4VCType?, OpenIdConfiguration?, OpenIdConfiguration?, dynamic)>
openIdConfiguration,
authorizationServerConfiguration,
credentialOfferJson,
issuer,
preAuthorizedCode,
);
}
}
Expand All @@ -702,6 +733,8 @@ Future<(OIDC4VCType?, OpenIdConfiguration?, OpenIdConfiguration?, dynamic)>
openIdConfiguration,
authorizationServerConfiguration,
credentialOfferJson,
issuer,
preAuthorizedCode,
);
}

Expand Down Expand Up @@ -974,47 +1007,6 @@ Future<String> getHost({
}
}

Future<(String?, String?)> getIssuerAndPreAuthorizedCode({
required String scannedResponse,
required DioClient dioClient,
}) async {
String? preAuthorizedCode;
String? issuer;

final Uri uriFromScannedResponse = Uri.parse(scannedResponse);

final keys = <String>[];
uriFromScannedResponse.queryParameters.forEach((key, value) => keys.add(key));

if (keys.contains('issuer')) {
issuer = uriFromScannedResponse.queryParameters['issuer'].toString();
//preAuthorizedCode can be null
preAuthorizedCode =
uriFromScannedResponse.queryParameters['pre-authorized_code'];
} else if (keys.contains('credential_offer') ||
keys.contains('credential_offer_uri')) {
final dynamic credentialOfferJson = await getCredentialOfferJson(
scannedResponse: scannedResponse,
dioClient: dioClient,
);
if (credentialOfferJson == null) throw Exception();

final dynamic preAuthorizedCodeGrant = credentialOfferJson['grants']
['urn:ietf:params:oauth:grant-type:pre-authorized_code'];

if (preAuthorizedCodeGrant != null &&
preAuthorizedCodeGrant is Map &&
preAuthorizedCodeGrant.containsKey('pre-authorized_code')) {
preAuthorizedCode =
preAuthorizedCodeGrant['pre-authorized_code'] as String;
}

issuer = credentialOfferJson['credential_issuer'].toString();
}

return (preAuthorizedCode, issuer);
}

bool isURL(String input) {
final bool uri = Uri.tryParse(input)?.hasAbsolutePath ?? false;
return uri;
Expand Down Expand Up @@ -1144,6 +1136,8 @@ bool hasIDTokenOrVPToken(String responseType) {

String getFormattedStringOIDC4VCI({
required String url,
required String tokenEndpoint,
required String credentialEndpoint,
OpenIdConfiguration? openIdConfiguration,
OpenIdConfiguration? authorizationServerConfiguration,
dynamic credentialOfferJson,
Expand All @@ -1154,8 +1148,8 @@ String getFormattedStringOIDC4VCI({
${credentialOfferJson != null ? const JsonEncoder.withIndent(' ').convert(credentialOfferJson) : 'None'}\n
<b>ENDPOINTS :</b>
authorization server endpoint : ${openIdConfiguration?.authorizationServer ?? 'None'}
token endpoint : ${openIdConfiguration?.tokenEndpoint ?? authorizationServerConfiguration?.tokenEndpoint ?? 'None'}
credential endpoint : ${openIdConfiguration?.credentialEndpoint ?? 'None'}
token endpoint : $tokenEndpoint}
credential endpoint : $credentialEndpoint}
deferred endpoint : ${openIdConfiguration?.deferredCredentialEndpoint ?? 'None'}
batch endpoint : ${openIdConfiguration?.batchEndpoint ?? 'None'}\n
<b>CREDENTIAL SUPPORTED :</b>
Expand All @@ -1181,6 +1175,16 @@ $codeForAuthorisedFlow
''';
}

String showFormatted({
required String title,
required Map<String, dynamic> jsonValue,
}) {
return '''
<b>$title :</b>
${const JsonEncoder.withIndent(' ').convert(jsonValue)}
''';
}

Future<String> getFormattedStringOIDC4VPSIOPV2({
required String url,
required DioClient client,
Expand Down Expand Up @@ -1351,3 +1355,69 @@ bool supportCryptoCredential(ProfileSetting profileSetting) {

return supportAssociatedCredential;
}

Future<(String?, String?, String?)> getClientDetails({
required ProfileCubit profileCubit,
required bool isEBSIV3,
}) async {
try {
String? clientId;
String? clientSecret;
String? authorization;

final customOidc4vcProfile = profileCubit.state.model.profileSetting
.selfSovereignIdentityOptions.customOidc4vcProfile;

switch (customOidc4vcProfile.clientAuthentication) {
case ClientAuthentication.none:
break;
case ClientAuthentication.clientSecretPost:
clientId = customOidc4vcProfile.clientId;
clientSecret = customOidc4vcProfile.clientSecret;
case ClientAuthentication.clientSecretBasic:
clientId = customOidc4vcProfile.clientId;
clientSecret = customOidc4vcProfile.clientSecret;
authorization = base64UrlEncode(utf8.encode('$clientId:$clientSecret'))
.replaceAll('=', '');
case ClientAuthentication.clientId:
final didKeyType = customOidc4vcProfile.defaultDid;

final privateKey = await fetchPrivateKey(
oidc4vc: profileCubit.oidc4vc,
secureStorage: profileCubit.secureStorageProvider,
isEBSIV3: isEBSIV3,
didKeyType: didKeyType,
);

final (did, _) = await fetchDidAndKid(
privateKey: privateKey,
isEBSIV3: isEBSIV3,
didKitProvider: profileCubit.didKitProvider,
secureStorage: profileCubit.secureStorageProvider,
didKeyType: didKeyType,
);

switch (customOidc4vcProfile.clientType) {
case ClientType.jwkThumbprint:
final tokenParameters = TokenParameters(
privateKey: jsonDecode(privateKey) as Map<String, dynamic>,
did: '', // just added as it is required field
mediaType: MediaType.basic, // just added as it is required field
clientType: ClientType
.jwkThumbprint, // just added as it is required field
proofHeaderType: customOidc4vcProfile.proofHeader,
clientId: '',
);
clientId = tokenParameters.thumbprint;
case ClientType.did:
clientId = did;
case ClientType.confidential:
clientId = customOidc4vcProfile.clientId;
}
}

return (clientId, clientSecret, authorization);
} catch (e) {
return (null, null, null);
}
}
Loading

0 comments on commit 445e5e1

Please sign in to comment.