feat: refactor authentication handling to use new ApiAuthModel and update related components

This commit is contained in:
Udhay-Adithya
2025-06-12 18:18:37 +05:30
parent 6a2d1fd534
commit 449e32521e
13 changed files with 214 additions and 213 deletions

View File

@@ -1,4 +1,5 @@
import 'package:apidash_core/apidash_core.dart';
import 'package:apidash_core/models/auth/api_auth_model.dart';
part 'request_model.freezed.dart';
@@ -15,8 +16,7 @@ class RequestModel with _$RequestModel {
@Default(APIType.rest) APIType apiType,
@Default("") String name,
@Default("") String description,
@Default(APIAuthType.none) APIAuthType authType,
APIAuthModel? authData,
@Default(ApiAuthModel(type: APIAuthType.none)) ApiAuthModel? authData,
@JsonKey(includeToJson: false) @Default(0) requestTabIndex,
HttpRequestModel? httpRequestModel,
int? responseStatus,

View File

@@ -24,8 +24,7 @@ mixin _$RequestModel {
APIType get apiType => throw _privateConstructorUsedError;
String get name => throw _privateConstructorUsedError;
String get description => throw _privateConstructorUsedError;
APIAuthType get authType => throw _privateConstructorUsedError;
APIAuthModel? get authData => throw _privateConstructorUsedError;
ApiAuthModel? get authData => throw _privateConstructorUsedError;
@JsonKey(includeToJson: false)
dynamic get requestTabIndex => throw _privateConstructorUsedError;
HttpRequestModel? get httpRequestModel => throw _privateConstructorUsedError;
@@ -61,8 +60,7 @@ abstract class $RequestModelCopyWith<$Res> {
APIType apiType,
String name,
String description,
APIAuthType authType,
APIAuthModel? authData,
ApiAuthModel? authData,
@JsonKey(includeToJson: false) dynamic requestTabIndex,
HttpRequestModel? httpRequestModel,
int? responseStatus,
@@ -73,7 +71,7 @@ abstract class $RequestModelCopyWith<$Res> {
String? preRequestScript,
String? postRequestScript});
$APIAuthModelCopyWith<$Res>? get authData;
$ApiAuthModelCopyWith<$Res>? get authData;
$HttpRequestModelCopyWith<$Res>? get httpRequestModel;
$HttpResponseModelCopyWith<$Res>? get httpResponseModel;
}
@@ -97,7 +95,6 @@ class _$RequestModelCopyWithImpl<$Res, $Val extends RequestModel>
Object? apiType = null,
Object? name = null,
Object? description = null,
Object? authType = null,
Object? authData = freezed,
Object? requestTabIndex = freezed,
Object? httpRequestModel = freezed,
@@ -126,14 +123,10 @@ class _$RequestModelCopyWithImpl<$Res, $Val extends RequestModel>
? _value.description
: description // ignore: cast_nullable_to_non_nullable
as String,
authType: null == authType
? _value.authType
: authType // ignore: cast_nullable_to_non_nullable
as APIAuthType,
authData: freezed == authData
? _value.authData
: authData // ignore: cast_nullable_to_non_nullable
as APIAuthModel?,
as ApiAuthModel?,
requestTabIndex: freezed == requestTabIndex
? _value.requestTabIndex
: requestTabIndex // ignore: cast_nullable_to_non_nullable
@@ -177,12 +170,12 @@ class _$RequestModelCopyWithImpl<$Res, $Val extends RequestModel>
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
$APIAuthModelCopyWith<$Res>? get authData {
$ApiAuthModelCopyWith<$Res>? get authData {
if (_value.authData == null) {
return null;
}
return $APIAuthModelCopyWith<$Res>(_value.authData!, (value) {
return $ApiAuthModelCopyWith<$Res>(_value.authData!, (value) {
return _then(_value.copyWith(authData: value) as $Val);
});
}
@@ -229,8 +222,7 @@ abstract class _$$RequestModelImplCopyWith<$Res>
APIType apiType,
String name,
String description,
APIAuthType authType,
APIAuthModel? authData,
ApiAuthModel? authData,
@JsonKey(includeToJson: false) dynamic requestTabIndex,
HttpRequestModel? httpRequestModel,
int? responseStatus,
@@ -242,7 +234,7 @@ abstract class _$$RequestModelImplCopyWith<$Res>
String? postRequestScript});
@override
$APIAuthModelCopyWith<$Res>? get authData;
$ApiAuthModelCopyWith<$Res>? get authData;
@override
$HttpRequestModelCopyWith<$Res>? get httpRequestModel;
@override
@@ -266,7 +258,6 @@ class __$$RequestModelImplCopyWithImpl<$Res>
Object? apiType = null,
Object? name = null,
Object? description = null,
Object? authType = null,
Object? authData = freezed,
Object? requestTabIndex = freezed,
Object? httpRequestModel = freezed,
@@ -295,14 +286,10 @@ class __$$RequestModelImplCopyWithImpl<$Res>
? _value.description
: description // ignore: cast_nullable_to_non_nullable
as String,
authType: null == authType
? _value.authType
: authType // ignore: cast_nullable_to_non_nullable
as APIAuthType,
authData: freezed == authData
? _value.authData
: authData // ignore: cast_nullable_to_non_nullable
as APIAuthModel?,
as ApiAuthModel?,
requestTabIndex: freezed == requestTabIndex
? _value.requestTabIndex!
: requestTabIndex,
@@ -351,8 +338,7 @@ class _$RequestModelImpl implements _RequestModel {
this.apiType = APIType.rest,
this.name = "",
this.description = "",
this.authType = APIAuthType.none,
this.authData,
this.authData = const ApiAuthModel(type: APIAuthType.none),
@JsonKey(includeToJson: false) this.requestTabIndex = 0,
this.httpRequestModel,
this.responseStatus,
@@ -379,9 +365,7 @@ class _$RequestModelImpl implements _RequestModel {
final String description;
@override
@JsonKey()
final APIAuthType authType;
@override
final APIAuthModel? authData;
final ApiAuthModel? authData;
@override
@JsonKey(includeToJson: false)
final dynamic requestTabIndex;
@@ -406,7 +390,7 @@ class _$RequestModelImpl implements _RequestModel {
@override
String toString() {
return 'RequestModel(id: $id, apiType: $apiType, name: $name, description: $description, authType: $authType, authData: $authData, requestTabIndex: $requestTabIndex, httpRequestModel: $httpRequestModel, responseStatus: $responseStatus, message: $message, httpResponseModel: $httpResponseModel, isWorking: $isWorking, sendingTime: $sendingTime)';
return 'RequestModel(id: $id, apiType: $apiType, name: $name, description: $description, authData: $authData, requestTabIndex: $requestTabIndex, httpRequestModel: $httpRequestModel, responseStatus: $responseStatus, message: $message, httpResponseModel: $httpResponseModel, isWorking: $isWorking, sendingTime: $sendingTime)';
}
@override
@@ -419,8 +403,6 @@ class _$RequestModelImpl implements _RequestModel {
(identical(other.name, name) || other.name == name) &&
(identical(other.description, description) ||
other.description == description) &&
(identical(other.authType, authType) ||
other.authType == authType) &&
(identical(other.authData, authData) ||
other.authData == authData) &&
const DeepCollectionEquality()
@@ -450,7 +432,6 @@ class _$RequestModelImpl implements _RequestModel {
apiType,
name,
description,
authType,
authData,
const DeepCollectionEquality().hash(requestTabIndex),
httpRequestModel,
@@ -484,8 +465,7 @@ abstract class _RequestModel implements RequestModel {
final APIType apiType,
final String name,
final String description,
final APIAuthType authType,
final APIAuthModel? authData,
final ApiAuthModel? authData,
@JsonKey(includeToJson: false) final dynamic requestTabIndex,
final HttpRequestModel? httpRequestModel,
final int? responseStatus,
@@ -507,9 +487,7 @@ abstract class _RequestModel implements RequestModel {
@override
String get description;
@override
APIAuthType get authType;
@override
APIAuthModel? get authData;
ApiAuthModel? get authData;
@override
@JsonKey(includeToJson: false)
dynamic get requestTabIndex;

View File

@@ -12,11 +12,9 @@ _$RequestModelImpl _$$RequestModelImplFromJson(Map json) => _$RequestModelImpl(
APIType.rest,
name: json['name'] as String? ?? "",
description: json['description'] as String? ?? "",
authType: $enumDecodeNullable(_$APIAuthTypeEnumMap, json['authType']) ??
APIAuthType.none,
authData: json['authData'] == null
? null
: APIAuthModel.fromJson(
? const ApiAuthModel(type: APIAuthType.none)
: ApiAuthModel.fromJson(
Map<String, dynamic>.from(json['authData'] as Map)),
requestTabIndex: json['requestTabIndex'] ?? 0,
httpRequestModel: json['httpRequestModel'] == null
@@ -43,7 +41,6 @@ Map<String, dynamic> _$$RequestModelImplToJson(_$RequestModelImpl instance) =>
'apiType': _$APITypeEnumMap[instance.apiType]!,
'name': instance.name,
'description': instance.description,
'authType': _$APIAuthTypeEnumMap[instance.authType]!,
'authData': instance.authData?.toJson(),
'httpRequestModel': instance.httpRequestModel?.toJson(),
'responseStatus': instance.responseStatus,
@@ -57,14 +54,3 @@ const _$APITypeEnumMap = {
APIType.rest: 'rest',
APIType.graphql: 'graphql',
};
const _$APIAuthTypeEnumMap = {
APIAuthType.none: 'none',
APIAuthType.basic: 'basic',
APIAuthType.apiKey: 'apiKey',
APIAuthType.bearerToken: 'bearerToken',
APIAuthType.jwtBearer: 'jwtBearer',
APIAuthType.digest: 'digest',
APIAuthType.oauth1: 'oauth1',
APIAuthType.oauth2: 'oauth2',
};

View File

@@ -207,8 +207,7 @@ class CollectionStateNotifier
String? id,
HTTPVerb? method,
APIType? apiType,
APIAuthType? authType,
APIAuthModel? authData,
ApiAuthModel? authData,
String? url,
String? name,
String? description,
@@ -236,7 +235,6 @@ class CollectionStateNotifier
var currentHttpRequestModel = currentModel.httpRequestModel;
final newModel = currentModel.copyWith(
apiType: apiType ?? currentModel.apiType,
authType: authType ?? currentModel.authType,
authData: authData ?? currentModel.authData,
name: name ?? currentModel.name,
description: description ?? currentModel.description,
@@ -319,7 +317,6 @@ class CollectionStateNotifier
requestId,
apiType,
requestModel.authData,
requestModel.authType,
substitutedHttpRequestModel,
defaultUriScheme: defaultUriScheme,
noSSL: noSSL,

View File

@@ -1,3 +1,7 @@
import 'package:apidash_core/models/auth/auth_api_key_model.dart';
import 'package:apidash_core/models/auth/auth_basic_model.dart';
import 'package:apidash_core/models/auth/auth_bearer_model.dart';
import 'package:apidash_core/models/auth/auth_jwt_model.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash_core/apidash_core.dart';
@@ -14,7 +18,7 @@ class EditAuthType extends ConsumerWidget {
return const SizedBox.shrink();
}
final currentAuthType = selectedRequest.authType;
final currentAuthType = selectedRequest.authData?.type ?? APIAuthType.none;
final currentAuthData = selectedRequest.authData;
return Padding(
@@ -48,14 +52,14 @@ class EditAuthType extends ConsumerWidget {
onChanged: (APIAuthType? newType) {
if (newType != null) {
ref.read(collectionStateNotifierProvider.notifier).update(
authType: newType,
authData: null,
authData: currentAuthData?.copyWith(type: newType) ??
ApiAuthModel(type: newType),
);
}
},
),
const SizedBox(height: 48),
_buildAuthFields(context, ref, currentAuthType, currentAuthData),
_buildAuthFields(context, ref, currentAuthData),
],
),
);
@@ -64,22 +68,21 @@ class EditAuthType extends ConsumerWidget {
Widget _buildAuthFields(
BuildContext context,
WidgetRef ref,
APIAuthType authType,
APIAuthModel? authData,
ApiAuthModel? authData,
) {
void updateAuth(APIAuthModel model) {
void updateAuth(ApiAuthModel model) {
ref.read(collectionStateNotifierProvider.notifier).update(
authData: model,
);
}
switch (authType) {
switch (authData?.type) {
case APIAuthType.basic:
final usernameController = TextEditingController(
text: (authData is BasicAuth) ? authData.username : '',
text: authData?.basic?.username ?? '',
);
final passwordController = TextEditingController(
text: (authData is BasicAuth) ? authData.password : '',
text: authData?.basic?.password ?? '',
);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
@@ -107,7 +110,13 @@ class EditAuthType extends ConsumerWidget {
),
),
onChanged: (value) => updateAuth(
BasicAuth(username: value, password: passwordController.text),
ApiAuthModel(
type: APIAuthType.basic,
basic: AuthBasicAuthModel(
username: usernameController.text,
password: value,
),
),
),
),
SizedBox(
@@ -136,15 +145,22 @@ class EditAuthType extends ConsumerWidget {
),
),
obscureText: true,
onChanged: (value) => updateAuth(BasicAuth(
username: usernameController.text, password: value)),
onChanged: (value) => updateAuth(
ApiAuthModel(
type: APIAuthType.basic,
basic: AuthBasicAuthModel(
username: usernameController.text,
password: value,
),
),
),
),
],
);
case APIAuthType.bearerToken:
case APIAuthType.bearer:
final tokenController = TextEditingController(
text: (authData is BearerTokenAuth) ? authData.token : '',
text: authData?.bearer?.token ?? '',
);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
@@ -171,20 +187,24 @@ class EditAuthType extends ConsumerWidget {
borderRadius: BorderRadius.circular(8),
),
),
onChanged: (value) => updateAuth(BearerTokenAuth(token: value)),
onChanged: (value) => updateAuth(
ApiAuthModel(
type: APIAuthType.bearer,
bearer: AuthBearerModel(token: value),
),
),
),
],
);
case APIAuthType.apiKey:
final keyController = TextEditingController(
text: (authData is APIKeyAuth) ? authData.key : '',
text: authData?.apikey?.key ?? '',
);
final nameController = TextEditingController(
text: (authData is APIKeyAuth) ? authData.name : 'x-api-key',
text: authData?.apikey?.key ?? 'x-api-key',
);
final currentLocation =
(authData is APIKeyAuth) ? authData.location : 'header';
final currentLocation = authData?.apikey?.location ?? 'header';
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
@@ -221,10 +241,13 @@ class EditAuthType extends ConsumerWidget {
],
onChanged: (String? newLocation) {
if (newLocation != null) {
updateAuth(APIKeyAuth(
key: keyController.text,
name: nameController.text,
location: newLocation,
updateAuth(ApiAuthModel(
type: APIAuthType.apiKey,
apikey: AuthApiKeyModel(
key: keyController.text,
name: nameController.text,
location: newLocation,
),
));
}
},
@@ -252,11 +275,16 @@ class EditAuthType extends ConsumerWidget {
borderRadius: BorderRadius.circular(8),
),
),
onChanged: (value) => updateAuth(APIKeyAuth(
key: keyController.text,
name: value,
location: currentLocation,
)),
onChanged: (value) => updateAuth(
ApiAuthModel(
type: APIAuthType.apiKey,
apikey: AuthApiKeyModel(
key: keyController.text,
name: value,
location: currentLocation,
),
),
),
),
const SizedBox(height: 16),
Text(
@@ -281,18 +309,21 @@ class EditAuthType extends ConsumerWidget {
borderRadius: BorderRadius.circular(8),
),
),
onChanged: (value) => updateAuth(APIKeyAuth(
key: value,
name: nameController.text,
location: currentLocation,
onChanged: (value) => updateAuth(ApiAuthModel(
type: APIAuthType.apiKey,
apikey: AuthApiKeyModel(
key: value,
name: nameController.text,
location: currentLocation,
),
)),
),
],
);
case APIAuthType.jwtBearer:
case APIAuthType.jwt:
final jwtController = TextEditingController(
text: (authData is JWTBearerAuth) ? authData.jwt : '',
text: authData?.jwt?.jwt ?? '',
);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
@@ -319,7 +350,10 @@ class EditAuthType extends ConsumerWidget {
borderRadius: BorderRadius.circular(8),
),
),
onChanged: (value) => updateAuth(JWTBearerAuth(jwt: value)),
onChanged: (value) => updateAuth(ApiAuthModel(
type: APIAuthType.jwt,
jwt: AuthJwtModel(jwt: value),
)),
),
],
);

View File

@@ -30,8 +30,8 @@ class EditRestRequestPane extends ConsumerWidget {
.select((value) => value?.httpRequestModel?.hasBody)) ??
false;
final hasAuth = ref.watch(
selectedRequestModelProvider.select((value) => value?.authType != APIAuthType.none));
final hasAuth = ref.watch(selectedRequestModelProvider
.select((value) => value?.authData?.type != APIAuthType.none));
false;
return RequestPane(
@@ -47,12 +47,7 @@ class EditRestRequestPane extends ConsumerWidget {
.read(collectionStateNotifierProvider.notifier)
.update(requestTabIndex: index);
},
showIndicators: [
paramLength > 0,
headerLength > 0,
hasBody,
hasAuth
],
showIndicators: [paramLength > 0, headerLength > 0, hasBody, hasAuth],
tabLabels: const [
kLabelURLParams,
kLabelHeaders,

View File

@@ -13,12 +13,11 @@ enum APIAuthType {
none,
basic,
apiKey,
bearerToken,
jwtBearer,
bearer,
jwt,
digest,
oauth1,
oauth2,
}
enum EnvironmentVariableType { variable, secret }

View File

@@ -8,16 +8,16 @@ import '../../consts.dart';
part 'api_auth_model.g.dart';
part 'api_auth_model.freezed.dart';
@freezed
class Auth with _$Auth {
const factory Auth({
class ApiAuthModel with _$ApiAuthModel {
const factory ApiAuthModel({
required APIAuthType type,
AuthApiKeyModel? apikey,
AuthBearerModel? bearer,
AuthBasicAuthModel? basic,
AuthJwtModel? jwt,
}) = _Auth;
}) = _ApiAuthModel;
factory Auth.fromJson(Map<String, dynamic> json) => _$AuthFromJson(json);
factory ApiAuthModel.fromJson(Map<String, dynamic> json) =>
_$ApiAuthModelFromJson(json);
}

View File

@@ -14,31 +14,33 @@ T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');
Auth _$AuthFromJson(Map<String, dynamic> json) {
return _Auth.fromJson(json);
ApiAuthModel _$ApiAuthModelFromJson(Map<String, dynamic> json) {
return _ApiAuthModel.fromJson(json);
}
/// @nodoc
mixin _$Auth {
mixin _$ApiAuthModel {
APIAuthType get type => throw _privateConstructorUsedError;
AuthApiKeyModel? get apikey => throw _privateConstructorUsedError;
AuthBearerModel? get bearer => throw _privateConstructorUsedError;
AuthBasicAuthModel? get basic => throw _privateConstructorUsedError;
AuthJwtModel? get jwt => throw _privateConstructorUsedError;
/// Serializes this Auth to a JSON map.
/// Serializes this ApiAuthModel to a JSON map.
Map<String, dynamic> toJson() => throw _privateConstructorUsedError;
/// Create a copy of Auth
/// Create a copy of ApiAuthModel
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
$AuthCopyWith<Auth> get copyWith => throw _privateConstructorUsedError;
$ApiAuthModelCopyWith<ApiAuthModel> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $AuthCopyWith<$Res> {
factory $AuthCopyWith(Auth value, $Res Function(Auth) then) =
_$AuthCopyWithImpl<$Res, Auth>;
abstract class $ApiAuthModelCopyWith<$Res> {
factory $ApiAuthModelCopyWith(
ApiAuthModel value, $Res Function(ApiAuthModel) then) =
_$ApiAuthModelCopyWithImpl<$Res, ApiAuthModel>;
@useResult
$Res call(
{APIAuthType type,
@@ -54,16 +56,16 @@ abstract class $AuthCopyWith<$Res> {
}
/// @nodoc
class _$AuthCopyWithImpl<$Res, $Val extends Auth>
implements $AuthCopyWith<$Res> {
_$AuthCopyWithImpl(this._value, this._then);
class _$ApiAuthModelCopyWithImpl<$Res, $Val extends ApiAuthModel>
implements $ApiAuthModelCopyWith<$Res> {
_$ApiAuthModelCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
/// Create a copy of Auth
/// Create a copy of ApiAuthModel
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
@@ -98,7 +100,7 @@ class _$AuthCopyWithImpl<$Res, $Val extends Auth>
) as $Val);
}
/// Create a copy of Auth
/// Create a copy of ApiAuthModel
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
@@ -112,7 +114,7 @@ class _$AuthCopyWithImpl<$Res, $Val extends Auth>
});
}
/// Create a copy of Auth
/// Create a copy of ApiAuthModel
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
@@ -126,7 +128,7 @@ class _$AuthCopyWithImpl<$Res, $Val extends Auth>
});
}
/// Create a copy of Auth
/// Create a copy of ApiAuthModel
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
@@ -140,7 +142,7 @@ class _$AuthCopyWithImpl<$Res, $Val extends Auth>
});
}
/// Create a copy of Auth
/// Create a copy of ApiAuthModel
/// with the given fields replaced by the non-null parameter values.
@override
@pragma('vm:prefer-inline')
@@ -156,10 +158,11 @@ class _$AuthCopyWithImpl<$Res, $Val extends Auth>
}
/// @nodoc
abstract class _$$AuthImplCopyWith<$Res> implements $AuthCopyWith<$Res> {
factory _$$AuthImplCopyWith(
_$AuthImpl value, $Res Function(_$AuthImpl) then) =
__$$AuthImplCopyWithImpl<$Res>;
abstract class _$$ApiAuthModelImplCopyWith<$Res>
implements $ApiAuthModelCopyWith<$Res> {
factory _$$ApiAuthModelImplCopyWith(
_$ApiAuthModelImpl value, $Res Function(_$ApiAuthModelImpl) then) =
__$$ApiAuthModelImplCopyWithImpl<$Res>;
@override
@useResult
$Res call(
@@ -180,13 +183,14 @@ abstract class _$$AuthImplCopyWith<$Res> implements $AuthCopyWith<$Res> {
}
/// @nodoc
class __$$AuthImplCopyWithImpl<$Res>
extends _$AuthCopyWithImpl<$Res, _$AuthImpl>
implements _$$AuthImplCopyWith<$Res> {
__$$AuthImplCopyWithImpl(_$AuthImpl _value, $Res Function(_$AuthImpl) _then)
class __$$ApiAuthModelImplCopyWithImpl<$Res>
extends _$ApiAuthModelCopyWithImpl<$Res, _$ApiAuthModelImpl>
implements _$$ApiAuthModelImplCopyWith<$Res> {
__$$ApiAuthModelImplCopyWithImpl(
_$ApiAuthModelImpl _value, $Res Function(_$ApiAuthModelImpl) _then)
: super(_value, _then);
/// Create a copy of Auth
/// Create a copy of ApiAuthModel
/// with the given fields replaced by the non-null parameter values.
@pragma('vm:prefer-inline')
@override
@@ -197,7 +201,7 @@ class __$$AuthImplCopyWithImpl<$Res>
Object? basic = freezed,
Object? jwt = freezed,
}) {
return _then(_$AuthImpl(
return _then(_$ApiAuthModelImpl(
type: null == type
? _value.type
: type // ignore: cast_nullable_to_non_nullable
@@ -224,12 +228,12 @@ class __$$AuthImplCopyWithImpl<$Res>
/// @nodoc
@JsonSerializable()
class _$AuthImpl implements _Auth {
const _$AuthImpl(
class _$ApiAuthModelImpl implements _ApiAuthModel {
const _$ApiAuthModelImpl(
{required this.type, this.apikey, this.bearer, this.basic, this.jwt});
factory _$AuthImpl.fromJson(Map<String, dynamic> json) =>
_$$AuthImplFromJson(json);
factory _$ApiAuthModelImpl.fromJson(Map<String, dynamic> json) =>
_$$ApiAuthModelImplFromJson(json);
@override
final APIAuthType type;
@@ -244,14 +248,14 @@ class _$AuthImpl implements _Auth {
@override
String toString() {
return 'Auth(type: $type, apikey: $apikey, bearer: $bearer, basic: $basic, jwt: $jwt)';
return 'ApiAuthModel(type: $type, apikey: $apikey, bearer: $bearer, basic: $basic, jwt: $jwt)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$AuthImpl &&
other is _$ApiAuthModelImpl &&
(identical(other.type, type) || other.type == type) &&
(identical(other.apikey, apikey) || other.apikey == apikey) &&
(identical(other.bearer, bearer) || other.bearer == bearer) &&
@@ -264,31 +268,32 @@ class _$AuthImpl implements _Auth {
int get hashCode =>
Object.hash(runtimeType, type, apikey, bearer, basic, jwt);
/// Create a copy of Auth
/// Create a copy of ApiAuthModel
/// with the given fields replaced by the non-null parameter values.
@JsonKey(includeFromJson: false, includeToJson: false)
@override
@pragma('vm:prefer-inline')
_$$AuthImplCopyWith<_$AuthImpl> get copyWith =>
__$$AuthImplCopyWithImpl<_$AuthImpl>(this, _$identity);
_$$ApiAuthModelImplCopyWith<_$ApiAuthModelImpl> get copyWith =>
__$$ApiAuthModelImplCopyWithImpl<_$ApiAuthModelImpl>(this, _$identity);
@override
Map<String, dynamic> toJson() {
return _$$AuthImplToJson(
return _$$ApiAuthModelImplToJson(
this,
);
}
}
abstract class _Auth implements Auth {
const factory _Auth(
abstract class _ApiAuthModel implements ApiAuthModel {
const factory _ApiAuthModel(
{required final APIAuthType type,
final AuthApiKeyModel? apikey,
final AuthBearerModel? bearer,
final AuthBasicAuthModel? basic,
final AuthJwtModel? jwt}) = _$AuthImpl;
final AuthJwtModel? jwt}) = _$ApiAuthModelImpl;
factory _Auth.fromJson(Map<String, dynamic> json) = _$AuthImpl.fromJson;
factory _ApiAuthModel.fromJson(Map<String, dynamic> json) =
_$ApiAuthModelImpl.fromJson;
@override
APIAuthType get type;
@@ -301,10 +306,10 @@ abstract class _Auth implements Auth {
@override
AuthJwtModel? get jwt;
/// Create a copy of Auth
/// Create a copy of ApiAuthModel
/// with the given fields replaced by the non-null parameter values.
@override
@JsonKey(includeFromJson: false, includeToJson: false)
_$$AuthImplCopyWith<_$AuthImpl> get copyWith =>
_$$ApiAuthModelImplCopyWith<_$ApiAuthModelImpl> get copyWith =>
throw _privateConstructorUsedError;
}

View File

@@ -6,7 +6,8 @@ part of 'api_auth_model.dart';
// JsonSerializableGenerator
// **************************************************************************
_$AuthImpl _$$AuthImplFromJson(Map<String, dynamic> json) => _$AuthImpl(
_$ApiAuthModelImpl _$$ApiAuthModelImplFromJson(Map<String, dynamic> json) =>
_$ApiAuthModelImpl(
type: $enumDecode(_$APIAuthTypeEnumMap, json['type']),
apikey: json['apikey'] == null
? null
@@ -22,7 +23,7 @@ _$AuthImpl _$$AuthImplFromJson(Map<String, dynamic> json) => _$AuthImpl(
: AuthJwtModel.fromJson(json['jwt'] as Map<String, dynamic>),
);
Map<String, dynamic> _$$AuthImplToJson(_$AuthImpl instance) =>
Map<String, dynamic> _$$ApiAuthModelImplToJson(_$ApiAuthModelImpl instance) =>
<String, dynamic>{
'type': _$APIAuthTypeEnumMap[instance.type]!,
'apikey': instance.apikey,
@@ -35,8 +36,8 @@ const _$APIAuthTypeEnumMap = {
APIAuthType.none: 'none',
APIAuthType.basic: 'basic',
APIAuthType.apiKey: 'apiKey',
APIAuthType.bearerToken: 'bearerToken',
APIAuthType.jwtBearer: 'jwtBearer',
APIAuthType.bearer: 'bearer',
APIAuthType.jwt: 'jwt',
APIAuthType.digest: 'digest',
APIAuthType.oauth1: 'oauth1',
APIAuthType.oauth2: 'oauth2',

View File

@@ -1,4 +1,4 @@
export 'environment_model.dart';
export 'http_request_model.dart';
export 'http_response_model.dart';
export 'api_auth_model.dart';
export 'auth/api_auth_model.dart';

View File

@@ -1,70 +1,78 @@
import 'dart:convert';
import 'package:apidash_core/consts.dart';
import 'package:apidash_core/models/api_auth_model.dart';
import 'package:apidash_core/models/auth/api_auth_model.dart';
import 'package:apidash_core/models/http_request_model.dart';
import 'package:seed/seed.dart';
HttpRequestModel handleAuth(HttpRequestModel httpRequestModel,
APIAuthType apiAuthType, APIAuthModel? authData) {
if (authData == null || apiAuthType == APIAuthType.none) {
HttpRequestModel handleAuth(HttpRequestModel httpRequestModel, ApiAuthModel? auth) {
if (auth == null || auth.type == APIAuthType.none) {
return httpRequestModel;
}
List<NameValueModel> updatedHeaders = List.from(httpRequestModel.headers ?? []);
List<NameValueModel> updatedHeaders =
List.from(httpRequestModel.headers ?? []);
List<NameValueModel> updatedParams = List.from(httpRequestModel.params ?? []);
List<bool> updatedHeaderEnabledList = List.from(httpRequestModel.isHeaderEnabledList ?? []);
List<bool> updatedParamEnabledList = List.from(httpRequestModel.isParamEnabledList ?? []);
List<bool> updatedHeaderEnabledList =
List.from(httpRequestModel.isHeaderEnabledList ?? []);
List<bool> updatedParamEnabledList =
List.from(httpRequestModel.isParamEnabledList ?? []);
switch (apiAuthType) {
switch (auth.type) {
case APIAuthType.basic:
final auth = authData as BasicAuth;
final encoded =
base64Encode(utf8.encode('${auth.username}:${auth.password}'));
updatedHeaders.add(const NameValueModel(name: 'Authorization', value: ''));
updatedHeaders[updatedHeaders.length - 1] = NameValueModel(
name: 'Authorization',
value: 'Basic $encoded'
);
updatedHeaderEnabledList.add(true);
break;
case APIAuthType.bearerToken:
final auth = authData as BearerTokenAuth;
updatedHeaders.add(NameValueModel(
name: 'Authorization',
value: 'Bearer ${auth.token}'
));
updatedHeaderEnabledList.add(true);
break;
case APIAuthType.jwtBearer:
final auth = authData as JWTBearerAuth;
updatedHeaders.add(NameValueModel(
name: 'Authorization',
value: 'Bearer ${auth.jwt}'
));
updatedHeaderEnabledList.add(true);
break;
case APIAuthType.apiKey:
final auth = authData as APIKeyAuth;
if (auth.location == 'header') {
updatedHeaders.add(NameValueModel(
name: auth.name,
value: auth.key
));
if (auth.basic != null) {
final basicAuth = auth.basic!;
final encoded = base64Encode(
utf8.encode('${basicAuth.username}:${basicAuth.password}'));
updatedHeaders.add(
NameValueModel(name: 'Authorization', value: 'Basic $encoded'));
updatedHeaderEnabledList.add(true);
} else if (auth.location == 'query') {
updatedParams.add(NameValueModel(
name: auth.name,
value: auth.key
));
updatedParamEnabledList.add(true);
}
break;
default:
case APIAuthType.bearer:
if (auth.bearer != null) {
final bearerAuth = auth.bearer!;
updatedHeaders.add(NameValueModel(
name: 'Authorization', value: 'Bearer ${bearerAuth.token}'));
updatedHeaderEnabledList.add(true);
}
break;
case APIAuthType.jwt:
if (auth.jwt != null) {
final jwtAuth = auth.jwt!;
updatedHeaders.add(NameValueModel(
name: 'Authorization', value: 'Bearer ${jwtAuth.jwt}'));
updatedHeaderEnabledList.add(true);
}
break;
case APIAuthType.apiKey:
if (auth.apikey != null) {
final apiKeyAuth = auth.apikey!;
if (apiKeyAuth.location == 'header') {
updatedHeaders.add(
NameValueModel(name: apiKeyAuth.name, value: apiKeyAuth.key));
updatedHeaderEnabledList.add(true);
} else if (apiKeyAuth.location == 'query') {
updatedParams.add(
NameValueModel(name: apiKeyAuth.name, value: apiKeyAuth.key));
updatedParamEnabledList.add(true);
}
}
break;
case APIAuthType.none:
break;
case APIAuthType.digest:
// TODO: Handle this case.
throw UnimplementedError();
case APIAuthType.oauth1:
// TODO: Handle this case.
throw UnimplementedError();
case APIAuthType.oauth2:
// TODO: Handle this case.
throw UnimplementedError();
}
return httpRequestModel.copyWith(

View File

@@ -17,8 +17,7 @@ final httpClientManager = HttpClientManager();
Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
String requestId,
APIType apiType,
APIAuthModel? authData,
APIAuthType apiAuthType,
ApiAuthModel? authData,
HttpRequestModel requestModel, {
SupportedUriSchemes defaultUriScheme = kDefaultUriScheme,
bool noSSL = false,
@@ -29,8 +28,7 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
final client = httpClientManager.createClient(requestId, noSSL: noSSL);
// Handle authentication
final authenticatedRequestModel =
handleAuth(requestModel, apiAuthType, authData);
final authenticatedRequestModel = handleAuth(requestModel, authData);
(Uri?, String?) uriRec = getValidRequestUri(
authenticatedRequestModel.url,