Skip to content

Commit

Permalink
Allow dynamic "Encryption Key" value in service
Browse files Browse the repository at this point in the history
  • Loading branch information
Matej-Hlatky committed Apr 27, 2024
1 parent 12136a5 commit 22dc10f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.4.0

* Allow dynamic "Encryption Key" value.

## 0.3.0

* Implement functions for listing and deleting device integrations
Expand Down
20 changes: 15 additions & 5 deletions lib/src/autogram_authenticator.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import 'package:chopper/chopper.dart';

/// Sets the "X-Encryption-Key" and "Accept": "application/json" values.
class AutogramAuthenticator extends HeadersInterceptor {
AutogramAuthenticator({required String encryptionKey})
: super({
"Accept": "application/json",
"X-Encryption-Key": encryptionKey,
});
final String Function() encryptionKeySource;

AutogramAuthenticator(this.encryptionKeySource) : super(const {});

@override
Future<Request> onRequest(Request request) async {
final encryptionKey = encryptionKeySource();
final allHeaders = {
"Accept": "application/json",
"X-Encryption-Key": encryptionKey,
};

return applyHeaders(request, allHeaders);
}
}
14 changes: 8 additions & 6 deletions lib/src/autogram_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ export 'iautogram_service.dart';

/// Implements [IAutogramService] using [Autogram] instance REST API client.
class AutogramService implements IAutogramService {
static final Uri _defaultBaseUrl =
Uri.parse("https://autogram.slovensko.digital/api/v1");

final Autogram _autogram;

/// Constructs new [AutogramService] instance.
AutogramService({
required Uri baseUrl,
required String encryptionKey,
Uri? baseUrl,
required String Function() encryptionKeySource,
}) : _autogram = Autogram.create(
baseUrl: baseUrl,
baseUrl: baseUrl ?? _defaultBaseUrl,
interceptors: [
AutogramAuthenticator(
encryptionKey: encryptionKey,
),
AutogramAuthenticator(encryptionKeySource),
],
);

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: autogram_sign
description: "Autogram service REST API client"
version: 0.3.0
version: 0.4.0
homepage: ""

environment:
Expand Down

0 comments on commit 22dc10f

Please sign in to comment.