Skip to content

Commit

Permalink
fixup! feat!: improve deserialization logic
Browse files Browse the repository at this point in the history
  • Loading branch information
JKRhb committed Jun 2, 2024
1 parent 42426d9 commit 6a1ba1a
Showing 1 changed file with 16 additions and 18 deletions.
34 changes: 16 additions & 18 deletions lib/src/core/implementation/servient.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,50 +262,48 @@ class InternalServient implements Servient {
);
}

bool _supportsSecurityScheme(String? securitySchemeType) {
List<String> _determineUnsupportedSecuritySchemes(
scripting_api.ExposedThingInit exposedThingInit,
) {
// TODO: Implement
return true;
return [];
}

// TODO: Implement missing steps of algorithm,
// see https://w3c.github.io/wot-scripting-api/#expand-an-exposedthinginit
Map<String, dynamic> _expandExposedThingInit(
scripting_api.ExposedThingInit exposedThingInit,
) {
final thingDescription = Map.of(exposedThingInit);

final unsupportedSecuritySchemes = <String>{};
final unsupportedSecuritySchemes =
_determineUnsupportedSecuritySchemes(thingDescription);

final securitySchemes =
thingDescription["securitySchemes"] ?? <String, Map<String, dynamic>>{};
final securityDefinitions = thingDescription["securityDefinitions"] ??
<String, Map<String, dynamic>>{};

if (securitySchemes is Map<String, Map<String, dynamic>>) {
for (final securityScheme in securitySchemes.entries) {
final scheme = securityScheme.value["scheme"];
if (scheme is String && !_supportsSecurityScheme(scheme)) {
unsupportedSecuritySchemes.add(securityScheme.key);
}
}
} else {
if (securityDefinitions is! Map<String, Map<String, dynamic>>) {
throw FormatException(
"Incorrect type for securitySchemes field, "
"expected Map<Map<String, dynamic>>, got "
"${securitySchemes.runtimeType}",
"${securityDefinitions.runtimeType}",
);
}

unsupportedSecuritySchemes.forEach(securitySchemes.remove);
unsupportedSecuritySchemes.forEach(securityDefinitions.remove);

final security = thingDescription["security"] ?? <String>[];

if (security is! List<String>) {
throw FormatException(
"Incorrect type for security field, "
"expected List<String>, got "
"${securitySchemes.runtimeType}",
"${security.runtimeType}",
);
}

thingDescription["security"] =
security.where(securitySchemes.containsKey).toList();
security.where(securityDefinitions.containsKey).toList();

// TODO: Should an ID be assigned here?
if (!thingDescription.containsKey("id")) {
Expand All @@ -331,7 +329,7 @@ class InternalServient implements Servient {
);
}

thingDescription["security"] = securitySchemes.keys.first;
thingDescription["security"] = securityDefinitions.keys.first;
}

return thingDescription;
Expand Down

0 comments on commit 6a1ba1a

Please sign in to comment.