From dbc45145291f04d942251c3a88b37be806af950f Mon Sep 17 00:00:00 2001 From: jld3103 Date: Thu, 30 Mar 2023 08:23:43 +0200 Subject: [PATCH] dynamite: Support http bearer auth --- .../dynamite/lib/src/openapi_builder.dart | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/packages/dynamite/lib/src/openapi_builder.dart b/packages/dynamite/lib/src/openapi_builder.dart index 548f903b..c598d8e1 100644 --- a/packages/dynamite/lib/src/openapi_builder.dart +++ b/packages/dynamite/lib/src/openapi_builder.dart @@ -326,6 +326,53 @@ class OpenAPIBuilder implements Builder { ).accept(emitter).toString(), ); continue; + case 'bearer': + output.add( + Class( + (final b) { + b + ..name = '${prefix}HttpBearerAuthentication' + ..extend = refer('${prefix}Authentication') + ..constructors.add( + Constructor( + (final b) => b + ..optionalParameters.add( + Parameter( + (final b) => b + ..name = 'token' + ..toThis = true + ..named = true + ..required = true, + ), + ), + ), + ) + ..fields.add( + Field( + (final b) => b + ..name = 'token' + ..type = refer('String') + ..modifier = FieldModifier.final$, + ), + ) + ..methods.add( + Method( + (final b) => b + ..name = 'headers' + ..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()}');