idTokenClaims function

Map<String, Object?> idTokenClaims(
  1. IdTokenResult? result, {
  2. bool fromToken = false,
})

The claims of result: its claims, or (where the platform doesn't give them, as on Windows) its token's payload.

Implementation

Map<String, Object?> idTokenClaims(IdTokenResult? result, {bool fromToken = false}) {
  if (result == null) return const {};
  if (!fromToken) return {...?result.claims};
  final parts = result.token?.split('.');
  if (parts == null || parts.length < 2) return const {};
  try {
    final payload = jsonDecode(utf8.decode(base64Url.decode(base64Url.normalize(parts[1]))));
    return payload is Map ? {for (final e in payload.entries) '${e.key}': e.value} : const {};
  } on FormatException {
    return const {};
  }
}