Skip to content

Commit

Permalink
Merge pull request #23 from namib-project/invalid-credentials
Browse files Browse the repository at this point in the history
feat!: allow passing invalid credentials to security callbacks
  • Loading branch information
JKRhb authored Aug 12, 2022
2 parents 5826b4a + 7c85a09 commit af7e47d
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
6 changes: 5 additions & 1 deletion example/complex_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ final Map<String, BasicCredentials> basicCredentials = {
'urn:test': BasicCredentials('username', 'password')
};

Future<BasicCredentials?> basicCredentialsCallback(Uri uri, Form? form) async {
Future<BasicCredentials?> basicCredentialsCallback(
Uri uri,
Form? form, [
BasicCredentials? invalidCredentials,
]) async {
final id = form?.interactionAffordance.thingDescription.identifier;

return basicCredentials[id];
Expand Down
6 changes: 5 additions & 1 deletion example/http_basic_authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ final Map<String, BasicCredentials> basicCredentialsMap = {
'urn:test': basicCredentials
};

Future<BasicCredentials?> basicCredentialsCallback(Uri uri, Form? form) async {
Future<BasicCredentials?> basicCredentialsCallback(
Uri uri,
Form? form,
BasicCredentials? invalidCredentials,
) async {
if (form == null) {
return basicCredentials;
}
Expand Down
18 changes: 14 additions & 4 deletions lib/src/binding_http/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -209,12 +209,22 @@ class HttpClient extends ProtocolClient {
return _handleResponse(request, response, form);
}

Future<BasicCredentials?> _getBasicCredentials(Uri uri, Form? form) async {
return _clientSecurityProvider?.basicCredentialsCallback?.call(uri, form);
Future<BasicCredentials?> _getBasicCredentials(
Uri uri,
Form? form, [
BasicCredentials? invalidCredentials,
]) async {
return _clientSecurityProvider?.basicCredentialsCallback
?.call(uri, form, invalidCredentials);
}

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

static Map<String, String> _getHeadersFromForm(Form form) {
Expand Down
6 changes: 5 additions & 1 deletion lib/src/core/security_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,14 @@ typedef ClientPskCallback = PskCredentials? Function(
/// will lead to the throw of an exception in the respective binding). This
/// behavior might change in future versions of `dart_wot`.
///
/// If the authorization/authentication fails with the given credentials, the
/// callback will be invoked again, containing the [invalidCredentials] as a set
/// argument.
///
/// This callback signature is currently only used for [PskCredentials] due to
/// implementation limititations, which do not allow for asynchronous callbacks.
typedef AsyncClientSecurityCallback<T extends Credentials> = Future<T?>
Function(Uri uri, Form? form);
Function(Uri uri, Form? form, T? invalidCredentials);

/// Class for providing callbacks for client [Credentials] at runtime.
///
Expand Down
6 changes: 3 additions & 3 deletions test/binding_http/http_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ void main() {
};

final clientSecurityProvider = ClientSecurityProvider(
basicCredentialsCallback: (uri, form) async {
basicCredentialsCallback: (uri, form, [invalidCredentials]) async {
return basicCredentialsStore[uri.host];
},
digestCredentialsCallback: (uri, form) async =>
digestCredentialsCallback: (uri, form, [invalidCredentials]) async =>
digestCredentialsStore[uri.host],
bearerCredentialsCallback: (uri, form) async =>
bearerCredentialsCallback: (uri, form, [invalidCredentials]) async =>
bearerCredentialsStore[uri.host],
);

Expand Down

0 comments on commit af7e47d

Please sign in to comment.