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

Added option to use signbytes so we can refactor the signed_json_cli #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 0.0.3 (23-03-2022)
- Added signed bytes

## 0.0.2 (23-03-2022)
- Fixed typo

Expand Down
1 change: 1 addition & 0 deletions lib/signed_json.dart
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export 'src/signed_bytes.dart';
export 'src/signed_json.dart';
24 changes: 12 additions & 12 deletions lib/src/base_signed_json.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,52 @@ class SignedJsonUtil {
Future<R> run<P, R>(FutureOr<R> Function(P) function, P param) async =>
compute(function, param);

Future<String> internalVerify(String cert, String encoded) async {
Future<List<int>> internalVerify(String cert, String encoded) async {
if (useNativeSignedJson) return NativeSignedJson.verify(cert, encoded);
final map = ComputerArgs(cert: cert, encoded: encoded).toJson();
return run(_verifyOnBackgroundThread, map);
}

Future<String> internalDecrypt(String cert, String ciphertext) async {
Future<List<int>> internalDecrypt(String cert, String ciphertext) async {
if (useNativeSignedJson) return NativeSignedJson.decrypt(cert, ciphertext);
final map = ComputerArgs(cert: cert, encoded: ciphertext).toJson();
return run(_decryptOnBackgroundThread, map);
}

Future<T> verifyAndDecrypt<T>(
String certVerify, String certDecrypt, String encoded) async {
String result;
List<int> result;
if (useNativeSignedJson) {
result = await NativeSignedJson.verifyAndDecrypt(
certVerify, certDecrypt, encoded);
} else {
result = await internalDecrypt(
certDecrypt, await internalVerify(certVerify, encoded));
final encryptedBytes = await internalVerify(certVerify, encoded);
final encryptedValue = utf8.decode(encryptedBytes);
result = await internalDecrypt(certDecrypt, encryptedValue);
}
return run(parseAndDecode, result);
final stringValue = utf8.decode(result);
return run(parseAndDecode, stringValue);
}
}

Future<String> _verifyOnBackgroundThread(Map<String, dynamic> data) async {
Future<List<int>> _verifyOnBackgroundThread(Map<String, dynamic> data) async {
final arguments = ComputerArgs.fromJson(data);
final jws = JsonWebSignature.fromCompactSerialization(arguments.encoded);
final jwk =
JsonWebKey.fromJson(jsonDecode(arguments.cert) as Map<String, dynamic>);
final keyStore = JsonWebKeyStore()..addKey(jwk);
final payload = await jws.getPayload(keyStore);
final unzipped = ZLibCodec().decoder.convert(payload.data);
return utf8.decode(unzipped);
return ZLibCodec().decoder.convert(payload.data);
}

Future<String> _decryptOnBackgroundThread(Map<String, dynamic> data) async {
Future<List<int>> _decryptOnBackgroundThread(Map<String, dynamic> data) async {
final arguments = ComputerArgs.fromJson(data);
final jwe = JsonWebEncryption.fromCompactSerialization(arguments.encoded);
final jwk =
JsonWebKey.fromJson(jsonDecode(arguments.cert) as Map<String, dynamic>);
final keyStore = JsonWebKeyStore()..addKey(jwk);
final payload = await jwe.getPayload(keyStore);
final unzipped = ZLibCodec().decoder.convert(payload.data);
return utf8.decode(unzipped);
return ZLibCodec().decoder.convert(payload.data);
}

T parseAndDecode<T>(String response) => jsonDecode(response) as T;
16 changes: 6 additions & 10 deletions lib/src/bridge/native_signed_json.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import 'dart:async';
import 'dart:convert';
import 'dart:io';

import 'package:flutter/services.dart';

class NativeSignedJson {
static const MethodChannel _channel = MethodChannel('signed_json');

static Future<String> verify(String cert, String encoded) async {
static Future<List<int>> verify(String cert, String encoded) async {
final zipBytes = await _channel.invokeMethod<List<int>>(
'verify',
{
Expand All @@ -16,11 +15,10 @@ class NativeSignedJson {
},
) ??
[];
final unzipped = ZLibCodec().decoder.convert(zipBytes);
return utf8.decode(unzipped);
return ZLibCodec().decoder.convert(zipBytes);
}

static Future<String> verifyAndDecrypt(
static Future<List<int>> verifyAndDecrypt(
String certVerify, String certDecrypt, String encoded) async {
final zipBytes = await _channel.invokeMethod<List<int>>(
'verifyAndDecrypt',
Expand All @@ -31,11 +29,10 @@ class NativeSignedJson {
},
) ??
[];
final unzipped = ZLibCodec().decoder.convert(zipBytes);
return utf8.decode(unzipped);
return ZLibCodec().decoder.convert(zipBytes);
}

static Future<String> decrypt(String cert, String encoded) async {
static Future<List<int>> decrypt(String cert, String encoded) async {
final zipBytes = await _channel.invokeMethod<List<int>>(
'decrypt',
{
Expand All @@ -44,7 +41,6 @@ class NativeSignedJson {
},
) ??
[];
final unzipped = ZLibCodec().decoder.convert(zipBytes);
return utf8.decode(unzipped);
return ZLibCodec().decoder.convert(zipBytes);
}
}
32 changes: 32 additions & 0 deletions lib/src/signed_bytes.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import 'package:signed_json/src/base_signed_json.dart';

class SignedBytes {
final _signedJsonUtil = SignedJsonUtil();

final String verificationCert;
final String? decryptionCert;

SignedBytes(
this.verificationCert, {
this.decryptionCert,
});

/// Verify an jwt encoded string against a public key. (verificationCert)
/// JsonWebSignatur is used for verification.
/// On Android we use a native implementation on all other platforms it is done in dart
/// Bytes will be returned.
Future<List<int>> verify<T>(String encoded) async =>
_signedJsonUtil.internalVerify(verificationCert, encoded);

/// Decrypt an jwt encoded string against a decryption key. (decryptionCert)
/// JsonWebEncryption is used for decryption.
/// On Android we use a native implementation on all other platforms it is done in dart
/// Bytes will be returned.
Future<List<int>> decrypt<T>(String encoded) async {
final decryptionCert = this.decryptionCert;
if (decryptionCert == null) {
throw ArgumentError('Decryption key can not be null');
}
return _signedJsonUtil.internalDecrypt(decryptionCert, encoded);
}
}
19 changes: 14 additions & 5 deletions lib/src/signed_json.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:signed_json/src/base_signed_json.dart';

class SignedJson {
Expand All @@ -14,20 +16,27 @@ class SignedJson {
/// Verify an jwt encoded string against a public key. (verificationCert)
/// JsonWebSignatur is used for verification.
/// On Android we use a native implementation on all other platforms it is done in dart
Future<T> verify<T>(String encoded) async => _signedJsonUtil.run(
parseAndDecode,
await _signedJsonUtil.internalVerify(verificationCert, encoded));
/// Json string will be returned.
Future<T> verify<T>(String encoded) async {
final result =
await _signedJsonUtil.internalVerify(verificationCert, encoded);
final param = utf8.decode(result);
return _signedJsonUtil.run(parseAndDecode, param);
}

/// Decrypt an jwt encoded string against a decryption key. (decryptionCert)
/// JsonWebEncryption is used for decryption.
/// On Android we use a native implementation on all other platforms it is done in dart
/// Json string will be returned.
Future<T> decrypt<T>(String encoded) async {
final decryptionCert = this.decryptionCert;
if (decryptionCert == null) {
throw ArgumentError('Decryption key can not be null');
}
return _signedJsonUtil.run(parseAndDecode,
await _signedJsonUtil.internalDecrypt(decryptionCert, encoded));
final result =
await _signedJsonUtil.internalDecrypt(decryptionCert, encoded);
final param = utf8.decode(result);
return _signedJsonUtil.run(parseAndDecode, param);
}

/// First a jwt is verified using (verify) after that you get another jwt
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: signed_json
description: An implementation to read signed jsons
repository: https://github.com/icapps/flutter-signed-json
version: 0.0.2
version: 0.0.3
homepage:

environment:
Expand Down