Skip to content

Commit

Permalink
feat!: simplify credentials callback usage
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Dec 11, 2023
1 parent f3e39bf commit f15e450
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 153 deletions.
8 changes: 3 additions & 5 deletions example/coaps_readproperty.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ PskCredentials? _pskCredentialsCallback(

Future<void> main(List<String> args) async {
final CoapClientFactory coapClientFactory = CoapClientFactory(
CoapConfig(
coapConfig: const CoapConfig(
dtlsCiphers: 'PSK-AES128-CCM8',
),
pskCredentialsCallback: _pskCredentialsCallback,
);
final securityProvider =
ClientSecurityProvider(pskCredentialsCallback: _pskCredentialsCallback);
final servient = Servient(clientSecurityProvider: securityProvider)
..addClientFactory(coapClientFactory);
final servient = Servient()..addClientFactory(coapClientFactory);

final wot = await servient.start();

Expand Down
12 changes: 6 additions & 6 deletions example/complex_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ Future<BasicCredentials?> basicCredentialsCallback(
}

Future<void> main() async {
final coapConfig = CoapConfig(blocksize: 64);
final CoapClientFactory coapClientFactory = CoapClientFactory(coapConfig);
final HttpClientFactory httpClientFactory = HttpClientFactory();
final securityProvider = ClientSecurityProvider(
basicCredentialsCallback: basicCredentialsCallback,
const coapConfig = CoapConfig(blocksize: 64);
final CoapClientFactory coapClientFactory = CoapClientFactory(
coapConfig: coapConfig,
);
final servient = Servient(clientSecurityProvider: securityProvider)
final HttpClientFactory httpClientFactory =
HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback);
final servient = Servient()
..addClientFactory(coapClientFactory)
..addClientFactory(httpClientFactory);
final wot = await servient.start();
Expand Down
9 changes: 3 additions & 6 deletions example/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ Future<BasicCredentials?> basicCredentialsCallback(

Future<void> main(List<String> args) async {
final CoapClientFactory coapClientFactory = CoapClientFactory();
final HttpClientFactory httpClientFactory = HttpClientFactory();
final HttpClientFactory httpClientFactory =
HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback);
final MqttClientFactory mqttClientFactory = MqttClientFactory();
final servient = Servient(
clientSecurityProvider: ClientSecurityProvider(
basicCredentialsCallback: basicCredentialsCallback,
),
)
final servient = Servient()
..addClientFactory(coapClientFactory)
..addClientFactory(httpClientFactory)
..addClientFactory(mqttClientFactory);
Expand Down
9 changes: 3 additions & 6 deletions example/http_basic_authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@ Future<BasicCredentials?> basicCredentialsCallback(
/// Illustrates the usage of both the basic and the automatic security scheme,
/// with a server supporting basic authentication.
Future<void> main(List<String> args) async {
final HttpClientFactory httpClientFactory = HttpClientFactory();
final securityProvider = ClientSecurityProvider(
basicCredentialsCallback: basicCredentialsCallback,
);
final servient = Servient(clientSecurityProvider: securityProvider)
..addClientFactory(httpClientFactory);
final HttpClientFactory httpClientFactory =
HttpClientFactory(basicCredentialsCallback: basicCredentialsCallback);
final servient = Servient()..addClientFactory(httpClientFactory);
final wot = await servient.start();

final thingDescription = ThingDescription(thingDescriptionJson);
Expand Down
35 changes: 22 additions & 13 deletions lib/src/binding_coap/coap_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ class _InternalCoapConfig extends CoapConfigDefault {

coap.PskCredentialsCallback? _createPskCallback(
Uri uri,
Form? form,
ClientSecurityProvider? clientSecurityProvider,
) {
Form? form, {
ClientPskCallback? pskCredentialsCallback,
}) {
final usesPskScheme = form?.usesPskScheme ?? false;
final pskCredentialsCallback = clientSecurityProvider?.pskCredentialsCallback;

if (!usesPskScheme || pskCredentialsCallback == null) {
return null;
Expand All @@ -82,11 +81,19 @@ coap.PskCredentialsCallback? _createPskCallback(
/// A [ProtocolClient] for the Constrained Application Protocol (CoAP).
final class CoapClient implements ProtocolClient {
/// Creates a new [CoapClient] based on an optional [CoapConfig].
CoapClient([this._coapConfig, this._clientSecurityProvider]);
CoapClient({
CoapConfig? coapConfig,
ClientPskCallback? pskCredentialsCallback,
AceSecurityCallback? aceSecurityCallback,
}) : _pskCredentialsCallback = pskCredentialsCallback,
_aceSecurityCallback = aceSecurityCallback,
_coapConfig = coapConfig;

final CoapConfig? _coapConfig;

final ClientSecurityProvider? _clientSecurityProvider;
final ClientPskCallback? _pskCredentialsCallback;

final AceSecurityCallback? _aceSecurityCallback;

Future<coap.CoapRequest> _createRequest(
coap.RequestMethod requestMethod,
Expand Down Expand Up @@ -153,9 +160,12 @@ final class CoapClient implements ProtocolClient {
}) async {
final coapClient = coap.CoapClient(
uri,
config: _InternalCoapConfig(_coapConfig ?? CoapConfig()),
pskCredentialsCallback:
_createPskCallback(uri, form, _clientSecurityProvider),
config: _InternalCoapConfig(_coapConfig ?? const CoapConfig()),
pskCredentialsCallback: _createPskCallback(
uri,
form,
pskCredentialsCallback: _pskCredentialsCallback,
),
);

final request = await _createRequest(
Expand All @@ -169,8 +179,7 @@ final class CoapClient implements ProtocolClient {
);

final creationHint = await _obtainAceCreationHintFromForm(form);
final aceCredentialsCallback =
_clientSecurityProvider?.aceCredentialsCallback;
final aceCredentialsCallback = _aceSecurityCallback;

final coap.CoapResponse response;

Expand Down Expand Up @@ -236,7 +245,7 @@ final class CoapClient implements ProtocolClient {

final coapClient = coap.CoapClient(
creationHintUri,
config: _InternalCoapConfig(_coapConfig ?? CoapConfig()),
config: _InternalCoapConfig(_coapConfig ?? const CoapConfig()),
);

final response = await coapClient.send(request);
Expand Down Expand Up @@ -421,7 +430,7 @@ final class CoapClient implements ProtocolClient {

final coapClient = coap.CoapClient(
form.resolvedHref,
config: _InternalCoapConfig(_coapConfig ?? CoapConfig()),
config: _InternalCoapConfig(_coapConfig ?? const CoapConfig()),
);

if (subprotocol == CoapSubprotocol.observe) {
Expand Down
26 changes: 18 additions & 8 deletions lib/src/binding_coap/coap_client_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,34 @@ import 'coap_config.dart';
/// A [ProtocolClientFactory] that produces CoAP clients.
final class CoapClientFactory implements ProtocolClientFactory {
/// Creates a new [CoapClientFactory] based on an optional [CoapConfig].
CoapClientFactory([this.coapConfig]);

@override
Set<String> get schemes => {'coap', 'coaps'};
CoapClientFactory({
this.coapConfig,
ClientPskCallback? pskCredentialsCallback,
AceSecurityCallback? aceSecurityCallback,
}) : _pskCredentialsCallback = pskCredentialsCallback,
_aceSecurityCallback = aceSecurityCallback;

/// The [CoapConfig] used to configure new clients.
final CoapConfig? coapConfig;

final ClientPskCallback? _pskCredentialsCallback;

final AceSecurityCallback? _aceSecurityCallback;

@override
Set<String> get schemes => {'coap', 'coaps'};

@override
bool destroy() {
return true;
}

@override
ProtocolClient createClient([
ClientSecurityProvider? clientSecurityProvider,
]) =>
CoapClient(coapConfig, clientSecurityProvider);
ProtocolClient createClient() => CoapClient(
coapConfig: coapConfig,
pskCredentialsCallback: _pskCredentialsCallback,
aceSecurityCallback: _aceSecurityCallback,
);

@override
bool init() {
Expand Down
5 changes: 4 additions & 1 deletion lib/src/binding_coap/coap_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@

import 'dart:typed_data';

import 'package:meta/meta.dart';

/// Allows for configuring the behavior of CoAP clients and servers.
@immutable
class CoapConfig {
/// Creates a new [CoapConfig] object.
CoapConfig({
const CoapConfig({
this.port = 5683,
this.securePort = 5684,
this.blocksize,
Expand Down
18 changes: 12 additions & 6 deletions lib/src/binding_http/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,19 @@ const _authorizationHeader = 'Authorization';
/// [`ComboSecurityScheme`]: https://w3c.github.io/wot-thing-description/#combosecurityscheme
final class HttpClient implements ProtocolClient {
/// Creates a new [HttpClient].
HttpClient(this._clientSecurityProvider);
HttpClient({
AsyncClientSecurityCallback<BasicCredentials>? basicCredentialsCallback,
AsyncClientSecurityCallback<BearerCredentials>? bearerCredentialsCallback,
}) : _basicCredentialsCallback = basicCredentialsCallback,
_bearerCredentialsCallback = bearerCredentialsCallback;

final _client = Client();

final ClientSecurityProvider? _clientSecurityProvider;
final AsyncClientSecurityCallback<BasicCredentials>?
_basicCredentialsCallback;

final AsyncClientSecurityCallback<BearerCredentials>?
_bearerCredentialsCallback;

Future<void> _applyCredentialsFromForm(Request request, Form form) async {
// TODO(JKRhb): Add DigestSecurity back in
Expand Down Expand Up @@ -210,17 +218,15 @@ final class HttpClient implements ProtocolClient {
Form? form, [
BasicCredentials? invalidCredentials,
]) async {
return _clientSecurityProvider?.basicCredentialsCallback
?.call(uri, form, invalidCredentials);
return _basicCredentialsCallback?.call(uri, form, invalidCredentials);
}

Future<BearerCredentials?> _getBearerCredentials(
Uri uri,
Form? form, [
BearerCredentials? invalidCredentials,
]) async {
return _clientSecurityProvider?.bearerCredentialsCallback
?.call(uri, form, invalidCredentials);
return _bearerCredentialsCallback?.call(uri, form, invalidCredentials);
}

static Map<String, String> _getHeadersFromForm(Form form) {
Expand Down
25 changes: 17 additions & 8 deletions lib/src/binding_http/http_client_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import '../core/credentials/basic_credentials.dart';
import '../core/credentials/bearer_credentials.dart';
import '../core/protocol_interfaces/protocol_client.dart';
import '../core/protocol_interfaces/protocol_client_factory.dart';
import '../core/security_provider.dart';
Expand All @@ -13,24 +15,31 @@ import 'http_config.dart';
/// A [ProtocolClientFactory] that produces HTTP and HTTPS clients.
final class HttpClientFactory implements ProtocolClientFactory {
/// Creates a new [HttpClientFactory] based on an optional [HttpConfig].
HttpClientFactory([this.httpConfig]);
HttpClientFactory({
AsyncClientSecurityCallback<BasicCredentials>? basicCredentialsCallback,
AsyncClientSecurityCallback<BearerCredentials>? bearerCredentialsCallback,
}) : _basicCredentialsCallback = basicCredentialsCallback,
_bearerCredentialsCallback = bearerCredentialsCallback;

final AsyncClientSecurityCallback<BasicCredentials>?
_basicCredentialsCallback;

final AsyncClientSecurityCallback<BearerCredentials>?
_bearerCredentialsCallback;

@override
Set<String> get schemes => {'http', 'https'};

/// The [HttpConfig] used to configure new clients.
final HttpConfig? httpConfig;

@override
bool destroy() {
return true;
}

@override
ProtocolClient createClient([
ClientSecurityProvider? clientSecurityProvider,
]) =>
HttpClient(clientSecurityProvider);
ProtocolClient createClient() => HttpClient(
basicCredentialsCallback: _basicCredentialsCallback,
bearerCredentialsCallback: _bearerCredentialsCallback,
);

@override
bool init() {
Expand Down
16 changes: 9 additions & 7 deletions lib/src/binding_mqtt/mqtt_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ import 'mqtt_subscription.dart';
/// Currently, only MQTT version 3.1.1 is supported.
final class MqttClient implements ProtocolClient {
/// Constructor.
MqttClient(
this._clientSecurityProvider,
MqttClient({
MqttConfig? mqttConfig,
) : _mqttConfig = mqttConfig ?? MqttConfig();
AsyncClientSecurityCallback<BasicCredentials>? basicCredentialsCallback,
}) : _mqttConfig = mqttConfig ?? MqttConfig(),
_basicCredentialsCallback = basicCredentialsCallback;

final MqttConfig _mqttConfig;
final AsyncClientSecurityCallback<BasicCredentials>?
_basicCredentialsCallback;

final ClientSecurityProvider? _clientSecurityProvider;
final MqttConfig _mqttConfig;

Future<BasicCredentials?> _obtainCredentials(
Uri uri,
Expand All @@ -50,8 +52,8 @@ final class MqttClient implements ProtocolClient {
return null;
}

final basicCredentials = _clientSecurityProvider?.basicCredentialsCallback
?.call(uri, form, invalidCredentials);
final basicCredentials =
_basicCredentialsCallback?.call(uri, form, invalidCredentials);

if (basicCredentials != null) {
return basicCredentials;
Expand Down
23 changes: 17 additions & 6 deletions lib/src/binding_mqtt/mqtt_client_factory.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,33 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import '../core/credentials/basic_credentials.dart';
import '../core/protocol_interfaces/protocol_client.dart';
import '../core/protocol_interfaces/protocol_client_factory.dart';

import '../core/security_provider.dart';
import 'constants.dart';
import 'mqtt_client.dart';
import 'mqtt_config.dart';

/// [ProtocolClientFactory] for creating [MqttClient]s.
final class MqttClientFactory implements ProtocolClientFactory {
@override
ProtocolClient createClient([
ClientSecurityProvider? clientSecurityProvider,
/// Instatiates a new [MqttClientFactory].
MqttClientFactory({
MqttConfig? mqttConfig,
]) =>
MqttClient(clientSecurityProvider, mqttConfig);
AsyncClientSecurityCallback<BasicCredentials>? basicCredentialsCallback,
}) : _mqttConfig = mqttConfig,
_basicCredentialsCallback = basicCredentialsCallback;

final MqttConfig? _mqttConfig;

final AsyncClientSecurityCallback<BasicCredentials>?
_basicCredentialsCallback;

@override
ProtocolClient createClient() => MqttClient(
mqttConfig: _mqttConfig,
basicCredentialsCallback: _basicCredentialsCallback,
);

@override
bool destroy() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import '../security_provider.dart';
import 'protocol_client.dart';

/// Base class for a factory that produces [ProtocolClient]s.
Expand All @@ -24,5 +23,5 @@ abstract interface class ProtocolClientFactory {

/// Creates a new [ProtocolClient] with that supports one or more of the given
/// [schemes].
ProtocolClient createClient([ClientSecurityProvider? clientSecurityProvider]);
ProtocolClient createClient();
}
Loading

0 comments on commit f15e450

Please sign in to comment.