Browse Source

dynamite,dynamite_runtime: Move authentication to dynamite_runtime

pull/418/head
jld3103 2 years ago
parent
commit
3ceb9c18aa
No known key found for this signature in database
GPG Key ID: 9062417B9E8EB7B3
  1. 131
      packages/dynamite/dynamite/lib/src/openapi_builder.dart
  2. 15
      packages/dynamite/dynamite_runtime/lib/src/http_client.dart

131
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<String, String>')
..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<String, String>')
..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 = <String, PathItem>{};

15
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<String, String> get headers => {
'Authorization': 'Bearer $token',
};
}
class DynamiteClient {
DynamiteClient(
this.baseURL, {

Loading…
Cancel
Save