Skip to content

Commit

Permalink
feat!: make credentials classes immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jan 5, 2024
1 parent 320421a commit 670d96a
Show file tree
Hide file tree
Showing 12 changed files with 30 additions and 21 deletions.
2 changes: 1 addition & 1 deletion example/complex_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const thingDescriptionJson = {
};

final Map<String, BasicCredentials> basicCredentials = {
"urn:test": BasicCredentials("username", "password"),
"urn:test": const BasicCredentials("username", "password"),
};

Future<BasicCredentials?> basicCredentialsCallback(
Expand Down
2 changes: 1 addition & 1 deletion example/http_basic_authentication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const thingDescriptionJson = {
},
};

final basicCredentials = BasicCredentials("username", "password");
const basicCredentials = BasicCredentials("username", "password");

final Map<String, BasicCredentials> basicCredentialsMap = {
"urn:test": basicCredentials,
Expand Down
2 changes: 1 addition & 1 deletion example/mqtt_example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ const thingDescriptionJson = {
};

final Map<String, BasicCredentials> basicCredentials = {
"urn:test": BasicCredentials("rw", "readwrite"),
"urn:test": const BasicCredentials("rw", "readwrite"),
};

Future<BasicCredentials?> basicCredentialsCallback(
Expand Down
2 changes: 1 addition & 1 deletion lib/src/core/credentials/ace_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import "credentials.dart";
/// [Credentials] used for the [AceSecurityScheme].
final class AceCredentials extends Credentials {
/// Constructor.
AceCredentials(this.accessToken);
const AceCredentials(this.accessToken);

/// The access token associated with these [AceCredentials] in serialized
/// form.
Expand Down
4 changes: 2 additions & 2 deletions lib/src/core/credentials/apikey_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import "credentials.dart";
/// [Credentials] used for the [ApiKeySecurityScheme].
final class ApiKeyCredentials extends Credentials {
/// Constructor.
ApiKeyCredentials(this.apiKey);
const ApiKeyCredentials(this.apiKey);

/// The [apiKey] associated with these [ApiKeyCredentials].
String apiKey;
final String apiKey;
}
6 changes: 3 additions & 3 deletions lib/src/core/credentials/basic_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import "credentials.dart";
/// Provides an unencrypted [username] and [password] combination.
final class BasicCredentials extends Credentials {
/// Constructor.
BasicCredentials(this.username, this.password);
const BasicCredentials(this.username, this.password);

/// The [username] associated with these [BasicCredentials].
String username;
final String username;

/// The [password] associated with these [BasicCredentials].
String password;
final String password;
}
4 changes: 2 additions & 2 deletions lib/src/core/credentials/bearer_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import "credentials.dart";
/// [Credentials] used for the [BearerSecurityScheme].
final class BearerCredentials extends Credentials {
/// Constructor.
BearerCredentials(this.token);
const BearerCredentials(this.token);

/// The [token] associated with these [BearerCredentials].
String token;
final String token;
}
8 changes: 7 additions & 1 deletion lib/src/core/credentials/credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@
//
// SPDX-License-Identifier: BSD-3-Clause

import "package:meta/meta.dart";

/// Base class used for defining credentials for Thing Interactions.
abstract base class Credentials {}
@immutable
abstract base class Credentials {
/// Default constructor for credentials objects.
const Credentials();
}
6 changes: 3 additions & 3 deletions lib/src/core/credentials/digest_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import "credentials.dart";
/// [Credentials] used for the [DigestSecurityScheme].
final class DigestCredentials extends Credentials {
/// Constructor.
DigestCredentials(this.username, this.password);
const DigestCredentials(this.username, this.password);

/// The [username] associated with these [DigestCredentials].
String username;
final String username;

/// The [password] associated with these [DigestCredentials].
String password;
final String password;
}
9 changes: 6 additions & 3 deletions lib/src/core/credentials/oauth2_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import "credentials.dart";
/// [Credentials] used for the [OAuth2SecurityScheme].
final class OAuth2Credentials extends Credentials {
/// Constructor.
OAuth2Credentials([this.secret]);
const OAuth2Credentials({
this.secret,
this.credentialsJson,
});

/// The optional secret for these [OAuth2Credentials].
String? secret;
final String? secret;

/// A JSON string representation of OAuth2 credentials.
///
/// Used to store obtained credentials from an authorization server.
String? credentialsJson;
final String? credentialsJson;
}
2 changes: 1 addition & 1 deletion lib/src/core/credentials/psk_credentials.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import "credentials.dart";
/// [Credentials] used for the [PskSecurityScheme].
final class PskCredentials extends Credentials {
/// Constructor.
PskCredentials({required this.preSharedKey, required this.identity});
const PskCredentials({required this.preSharedKey, required this.identity});

/// The [identity] associated with these [PskCredentials].
///
Expand Down
4 changes: 2 additions & 2 deletions test/binding_http/http_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,11 @@ void main() {
final parsedTd = ThingDescription.fromJson(thingDescriptionJson);

final Map<String, BasicCredentials> basicCredentialsStore = {
"httpbin.org": BasicCredentials(username, password),
"httpbin.org": const BasicCredentials(username, password),
};

final Map<String, BearerCredentials> bearerCredentialsStore = {
"httpbin.org": BearerCredentials(token),
"httpbin.org": const BearerCredentials(token),
};

Future<BasicCredentials?> basicCredentialsCallback(
Expand Down

0 comments on commit 670d96a

Please sign in to comment.