Skip to content

Commit

Permalink
Merge pull request #235 from yakforward-ou/remove_callable
Browse files Browse the repository at this point in the history
remove callable
  • Loading branch information
iapicca authored Apr 16, 2024
2 parents 1dcf918 + 7b339fd commit 460dce5
Show file tree
Hide file tree
Showing 58 changed files with 207 additions and 1,558 deletions.
88 changes: 0 additions & 88 deletions examples/yak_flutter/.gitignore

This file was deleted.

5 changes: 0 additions & 5 deletions examples/yak_flutter/README.md

This file was deleted.

29 changes: 0 additions & 29 deletions examples/yak_flutter/analysis_options.yaml

This file was deleted.

97 changes: 0 additions & 97 deletions examples/yak_flutter/lib/main.dart

This file was deleted.

18 changes: 0 additions & 18 deletions examples/yak_flutter/pubspec.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions packages/byte_token/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 2.1.0
- remove delegates to avoid implicit_call_tearoffs

### 2.0.3
- example link and readme update

Expand Down
11 changes: 1 addition & 10 deletions packages/byte_token/lib/src/encoder.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
import 'package:byte_token/byte_token.dart';
import 'package:crypto/crypto.dart';

/// encodes [Bytes] using [Hmac]
class HmacEncoder extends BytesEncoder {
const HmacEncoder();

/// takes two parameters
/// first: the `input` to sign
/// second the `secret key`
@override
Bytes call(p0, p1) => Hmac(sha256, p1).convert(p0).bytes;
}
Bytes hmacEncode(Bytes p0, Bytes p1) => Hmac(sha256, p1).convert(p0).bytes;
5 changes: 1 addition & 4 deletions packages/byte_token/lib/src/secret.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'dart:convert';
import 'dart:typed_data';

import 'package:yak_utils/yak_utils.dart';

/// a class the convert a string to `Bytes` when instantiated
class Secret extends NullaryDelegate<Uint8List> {
class Secret {
final Uint8List _secret;
final Base64Decoder decoder;

Expand All @@ -14,6 +12,5 @@ class Secret extends NullaryDelegate<Uint8List> {
this.decoder = const Base64Decoder(),
}) : _secret = decoder.convert(secret);

@override
Uint8List call() => _secret;
}
14 changes: 4 additions & 10 deletions packages/byte_token/lib/src/signature.dart
Original file line number Diff line number Diff line change
@@ -1,29 +1,23 @@
import 'package:yak_utils/yak_utils.dart';

import 'encoder.dart';
import 'extensions.dart';
import 'payload.dart';
import 'secret.dart';
import 'typedefs.dart';

/// a callable class that generates a signature [Bytes]
class ByteSignature extends UnaryDelegate<Bytes, Payload> {
class ByteSignature {
final Secret secret;
final BytesEncoder encoder;
final Encode encode;

/// the constructor takes a [Secret] as argument
/// default encoder is [HmacEncoder]
const ByteSignature(
this.secret, {
this.encoder = const HmacEncoder(),
});
ByteSignature(this.secret, {Encode? encode}) : encode = encode ?? hmacEncode;

@override
Bytes call(Payload p0) {
if (p0.isExpired) {
throw Exception('token expired\n$p0');
}
final payload = p0.toBuffer();
return encoder(payload, secret());
return encode(payload, secret());
}
}
5 changes: 1 addition & 4 deletions packages/byte_token/lib/src/typedefs.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
import 'package:yak_utils/yak_utils.dart';

typedef EncoderDelegate<T> = MultiDelegate2<T, T, T>;
typedef Bytes = List<int>;
typedef BytesEncoder = EncoderDelegate<Bytes>;
typedef Encode = Bytes Function(Bytes, Bytes);
16 changes: 5 additions & 11 deletions packages/byte_token/lib/src/validator.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
import 'package:yak_utils/yak_utils.dart';

import 'encoder.dart';
import 'signature.dart';
import 'token.dart';
import 'typedefs.dart';
import '../byte_token.dart';

/// a callable class that validates a [ByteToken]
class ByteTokenValidator extends UnaryDelegate<ByteToken, ByteToken> {
final BytesEncoder encoder;
class ByteTokenValidator {
final Encode encode;
final ByteSignature signature;

/// it requires a [ByteSignature] and defaults the encoder to [HmacEncoder]
const ByteTokenValidator(
this.signature, {
this.encoder = const HmacEncoder(),
});
ByteTokenValidator(this.signature, {Encode? encode})
: encode = encode ?? hmacEncode;

/// takes a [ByteToken] as argument and returns it if valid
@override
ByteToken call(ByteToken p0) {
if (!p0.signature.equals(signature(p0.payload))) {
throw Exception('signature not valid for $p0');
Expand Down
4 changes: 2 additions & 2 deletions packages/byte_token/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: byte_token
description: A protobuf based, performant and lightweight token authentication package.
version: 2.0.3
homepage: https://github.com/iapicca/yak_packages
version: 2.1.0
homepage: https://github.com/yakforward-ou/yak_packages

environment:
sdk: '>=3.3.3 <4.0.0'
Expand Down
11 changes: 2 additions & 9 deletions packages/byte_token/test/encoder_test.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
import 'package:byte_token/byte_token.dart';
import 'package:crypto/crypto.dart';
import 'package:test/test.dart';

class HmacEncoder extends BytesEncoder {
const HmacEncoder();
@override
List<int> call(p0, p1) => Hmac(sha256, p1).convert(p0).bytes;
}

void main() {
group('HmacEncoder', () {
final list = [for (var i = 0; i < 100; ++i) i];
final encoder = HmacEncoder();

test(
'GIVEN a HmacEncoder '
'WHEN call(Bytes,Bytes) '
'THEN returns Bytes', () {
expect(
encoder(list, list),
hmacEncode(list, list),
isA<Bytes>(),
reason: 'should output Uint8List',
);
Expand Down
3 changes: 3 additions & 0 deletions packages/stub/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 2.1.0
- remove delegates to avoid implicit_call_tearoffs

### 2.0.2
- example link and readme update

Expand Down
Loading

0 comments on commit 460dce5

Please sign in to comment.