From 3ceb9c18aa7552fa3ba45a63edce1f371e2220f1 Mon Sep 17 00:00:00 2001 From: jld3103 Date: Mon, 26 Jun 2023 09:50:56 +0200 Subject: [PATCH] dynamite,dynamite_runtime: Move authentication to dynamite_runtime --- .../dynamite/lib/src/openapi_builder.dart | 131 ------------------ .../dynamite_runtime/lib/src/http_client.dart | 15 ++ 2 files changed, 15 insertions(+), 131 deletions(-) diff --git a/packages/dynamite/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/dynamite/lib/src/openapi_builder.dart index 58d28b2e..e29d7ef5 100644 --- a/packages/dynamite/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/dynamite/lib/src/openapi_builder.dart @@ -173,137 +173,6 @@ class OpenAPIBuilder implements Builder { ).accept(emitter).toString(), ]; - if (spec.components?.securitySchemes != null) { - for (final name in spec.components!.securitySchemes!.keys) { - final securityScheme = spec.components!.securitySchemes![name]!; - switch (securityScheme.type) { - case 'http': - switch (securityScheme.scheme) { - case 'basic': - output.add( - Class( - (final b) { - final fields = ['username', 'password']; - b - ..name = '${prefix}HttpBasicAuthentication' - ..extend = refer('DynamiteAuthentication') - ..constructors.add( - Constructor( - (final b) => b - ..optionalParameters.addAll( - fields.map( - (final name) => Parameter( - (final b) => b - ..name = name - ..toThis = true - ..named = true - ..required = true, - ), - ), - ), - ), - ) - ..fields.addAll( - fields.map( - (final name) => Field( - (final b) => b - ..name = name - ..type = refer('String') - ..modifier = FieldModifier.final$, - ), - ), - ) - ..methods.addAll([ - Method( - (final b) => b - ..name = 'id' - ..annotations.add(refer('override')) - ..type = MethodType.getter - ..lambda = true - ..returns = refer('String') - ..body = Code("'$name'"), - ), - Method( - (final b) => b - ..name = 'headers' - ..annotations.add(refer('override')) - ..type = MethodType.getter - ..returns = refer('Map') - ..lambda = true - ..body = const Code(r''' - { - 'Authorization': 'Basic ${base64.encode(utf8.encode('$username:$password'))}', - } - '''), - ), - ]); - }, - ).accept(emitter).toString(), - ); - continue; - case 'bearer': - output.add( - Class( - (final b) { - b - ..name = '${prefix}HttpBearerAuthentication' - ..extend = refer('DynamiteAuthentication') - ..constructors.add( - Constructor( - (final b) => b - ..optionalParameters.add( - Parameter( - (final b) => b - ..name = 'token' - ..toThis = true - ..named = true - ..required = true, - ), - ), - ), - ) - ..fields.addAll([ - Field( - (final b) => b - ..name = 'token' - ..type = refer('String') - ..modifier = FieldModifier.final$, - ), - ]) - ..methods.addAll([ - Method( - (final b) => b - ..name = 'id' - ..annotations.add(refer('override')) - ..type = MethodType.getter - ..lambda = true - ..returns = refer('String') - ..body = Code("'$name'"), - ), - Method( - (final b) => b - ..name = 'headers' - ..annotations.add(refer('override')) - ..type = MethodType.getter - ..returns = refer('Map') - ..lambda = true - ..body = const Code(r''' - { - 'Authorization': 'Bearer $token', - } - '''), - ), - ]); - }, - ).accept(emitter).toString(), - ); - continue; - } - } - throw Exception('Can not work with security scheme ${securityScheme.toJson()}'); - } - } - for (final tag in tags) { final isRootClient = tag == null; final paths = {}; diff --git a/packages/dynamite/dynamite_runtime/lib/src/http_client.dart b/packages/dynamite/dynamite_runtime/lib/src/http_client.dart index f428be79..c3ba7f8c 100644 --- a/packages/dynamite/dynamite_runtime/lib/src/http_client.dart +++ b/packages/dynamite/dynamite_runtime/lib/src/http_client.dart @@ -81,6 +81,21 @@ class DynamiteHttpBasicAuthentication extends DynamiteAuthentication { }; } +class DynamiteHttpBearerAuthentication extends DynamiteAuthentication { + DynamiteHttpBearerAuthentication({ + required this.token, + }); + + final String token; + + @override + String get id => 'bearer'; + @override + Map get headers => { + 'Authorization': 'Bearer $token', + }; +} + class DynamiteClient { DynamiteClient( this.baseURL, {