From 1dac4e6b8205515127ee9631a8171e2608c43148 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Mon, 26 Jun 2023 09:49:43 +0200 Subject: [PATCH] dynamite,dynamite_runtime: Correctly search for authentication --- .../dynamite/lib/src/openapi_builder.dart | 5 +++-- .../dynamite_runtime/lib/src/http_client.dart | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/packages/dynamite/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/dynamite/lib/src/openapi_builder.dart index e29d7ef5..fea0140a 100644 --- a/packages/dynamite/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/dynamite/lib/src/openapi_builder.dart @@ -353,9 +353,10 @@ class OpenAPIBuilder implements Builder { if (requirement.keys.isEmpty) { continue; } + final securityScheme = spec.components!.securitySchemes![requirement.keys.single]!; code.write(''' - if (${isRootClient ? '' : 'rootClient.'}authentications.map((final a) => a.id).contains('${requirement.keys.single}')) { - headers.addAll(${isRootClient ? '' : 'rootClient.'}authentications.singleWhere((final a) => a.id == '${requirement.keys.single}').headers); + if (${isRootClient ? '' : 'rootClient.'}authentications.where((final a) => a.type == '${securityScheme.type}' && a.scheme == '${securityScheme.scheme}').isNotEmpty) { + headers.addAll(${isRootClient ? '' : 'rootClient.'}authentications.singleWhere((final a) => a.type == '${securityScheme.type}' && a.scheme == '${securityScheme.scheme}').headers); } '''); if (!isOptionalSecurity || diff --git a/packages/dynamite/dynamite_runtime/lib/src/http_client.dart b/packages/dynamite/dynamite_runtime/lib/src/http_client.dart index c3ba7f8c..b1b5c9b1 100644 --- a/packages/dynamite/dynamite_runtime/lib/src/http_client.dart +++ b/packages/dynamite/dynamite_runtime/lib/src/http_client.dart @@ -59,7 +59,8 @@ class DynamiteApiException implements Exception { } abstract class DynamiteAuthentication { - String get id; + String get type; + String get scheme; Map get headers; } @@ -74,7 +75,11 @@ class DynamiteHttpBasicAuthentication extends DynamiteAuthentication { final String password; @override - String get id => 'basic_auth'; + String type = 'http'; + + @override + String scheme = 'basic'; + @override Map get headers => { 'Authorization': 'Basic ${base64.encode(utf8.encode('$username:$password'))}', @@ -89,7 +94,11 @@ class DynamiteHttpBearerAuthentication extends DynamiteAuthentication { final String token; @override - String get id => 'bearer'; + String type = 'http'; + + @override + String scheme = 'bearer'; + @override Map get headers => { 'Authorization': 'Bearer $token',