mirror of
https://github.com/foss42/apidash.git
synced 2025-12-02 18:57:05 +08:00
Add AuthPage
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
export 'api_key_auth_fields.dart';
|
export 'api_key_auth_fields.dart';
|
||||||
|
export 'auth_page.dart';
|
||||||
export 'basic_auth_fields.dart';
|
export 'basic_auth_fields.dart';
|
||||||
export 'bearer_auth_fields.dart';
|
export 'bearer_auth_fields.dart';
|
||||||
export 'digest_auth_fields.dart';
|
export 'digest_auth_fields.dart';
|
||||||
|
|||||||
89
lib/screens/common_widgets/auth/auth_page.dart
Normal file
89
lib/screens/common_widgets/auth/auth_page.dart
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
import 'package:apidash_design_system/apidash_design_system.dart';
|
||||||
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:apidash_core/apidash_core.dart';
|
||||||
|
import 'api_key_auth_fields.dart';
|
||||||
|
import 'basic_auth_fields.dart';
|
||||||
|
import 'bearer_auth_fields.dart';
|
||||||
|
import 'digest_auth_fields.dart';
|
||||||
|
import 'jwt_auth_fields.dart';
|
||||||
|
import 'consts.dart';
|
||||||
|
|
||||||
|
class AuthPage extends StatelessWidget {
|
||||||
|
final AuthModel? authModel;
|
||||||
|
final bool readOnly;
|
||||||
|
final Function(APIAuthType? newType)? onChangedAuthType;
|
||||||
|
final Function(AuthModel? model)? updateAuthData;
|
||||||
|
|
||||||
|
const AuthPage({
|
||||||
|
super.key,
|
||||||
|
this.authModel,
|
||||||
|
this.readOnly = false,
|
||||||
|
this.onChangedAuthType,
|
||||||
|
this.updateAuthData,
|
||||||
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return SingleChildScrollView(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
kLabelSelectAuthType,
|
||||||
|
style: TextStyle(
|
||||||
|
fontWeight: FontWeight.bold,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
SizedBox(
|
||||||
|
height: 8,
|
||||||
|
),
|
||||||
|
ADPopupMenu<APIAuthType>(
|
||||||
|
value: authModel?.type.displayType,
|
||||||
|
values: APIAuthType.values
|
||||||
|
.map((type) => (type, type.displayType))
|
||||||
|
.toList(),
|
||||||
|
tooltip: kTooltipSelectAuth,
|
||||||
|
isOutlined: true,
|
||||||
|
onChanged: readOnly ? null : onChangedAuthType,
|
||||||
|
),
|
||||||
|
const SizedBox(height: 48),
|
||||||
|
switch (authModel?.type) {
|
||||||
|
APIAuthType.basic => BasicAuthFields(
|
||||||
|
readOnly: readOnly,
|
||||||
|
authData: authModel,
|
||||||
|
updateAuth: updateAuthData,
|
||||||
|
),
|
||||||
|
APIAuthType.bearer => BearerAuthFields(
|
||||||
|
readOnly: readOnly,
|
||||||
|
authData: authModel,
|
||||||
|
updateAuth: updateAuthData,
|
||||||
|
),
|
||||||
|
APIAuthType.apiKey => ApiKeyAuthFields(
|
||||||
|
readOnly: readOnly,
|
||||||
|
authData: authModel,
|
||||||
|
updateAuth: updateAuthData,
|
||||||
|
),
|
||||||
|
APIAuthType.jwt => JwtAuthFields(
|
||||||
|
readOnly: readOnly,
|
||||||
|
authData: authModel,
|
||||||
|
updateAuth: updateAuthData,
|
||||||
|
),
|
||||||
|
APIAuthType.digest => DigestAuthFields(
|
||||||
|
readOnly: readOnly,
|
||||||
|
authData: authModel,
|
||||||
|
updateAuth: updateAuthData,
|
||||||
|
),
|
||||||
|
APIAuthType.none =>
|
||||||
|
Text(readOnly ? kMsgNoAuth : kMsgNoAuthSelected),
|
||||||
|
_ => Text(readOnly
|
||||||
|
? "${authModel?.type.name} $kMsgAuthNotSupported"
|
||||||
|
: kMsgNotImplemented),
|
||||||
|
}
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -62,3 +62,12 @@ const kHintJson =
|
|||||||
'{"sub": "1234567890", "name": "John Doe", "iat": 1516239022}';
|
'{"sub": "1234567890", "name": "John Doe", "iat": 1516239022}';
|
||||||
const kHeaderPrefix = 'Bearer';
|
const kHeaderPrefix = 'Bearer';
|
||||||
const kQueryParamKey = 'token';
|
const kQueryParamKey = 'token';
|
||||||
|
|
||||||
|
//AuthPAge
|
||||||
|
const kLabelSelectAuthType = "Authentication Type";
|
||||||
|
const kTooltipSelectAuth = "Select Authentication Type";
|
||||||
|
const kMsgNoAuth = "No authentication was used for this request.";
|
||||||
|
const kMsgNoAuthSelected = "No authentication selected.";
|
||||||
|
const kMsgAuthNotSupported =
|
||||||
|
"authentication details are not yet supported in history view.";
|
||||||
|
const kMsgNotImplemented = "This auth type is not implemented yet.";
|
||||||
|
|||||||
@@ -1,97 +1,44 @@
|
|||||||
import 'package:apidash/screens/common_widgets/auth/api_key_auth_fields.dart';
|
|
||||||
import 'package:apidash/screens/common_widgets/auth/basic_auth_fields.dart';
|
|
||||||
import 'package:apidash/screens/common_widgets/auth/bearer_auth_fields.dart';
|
|
||||||
import 'package:apidash/screens/common_widgets/auth/digest_auth_fields.dart';
|
|
||||||
import 'package:apidash/screens/common_widgets/auth/jwt_auth_fields.dart';
|
|
||||||
import 'package:apidash_design_system/widgets/popup_menu.dart';
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
import 'package:apidash_core/apidash_core.dart';
|
import 'package:apidash_core/apidash_core.dart';
|
||||||
import 'package:apidash/providers/providers.dart';
|
import 'package:apidash/providers/providers.dart';
|
||||||
|
import '../../../../common_widgets/common_widgets.dart';
|
||||||
|
|
||||||
class EditAuthType extends ConsumerWidget {
|
class EditAuthType extends ConsumerWidget {
|
||||||
final AuthModel? authModel;
|
|
||||||
final bool readOnly;
|
final bool readOnly;
|
||||||
|
|
||||||
const EditAuthType({
|
const EditAuthType({
|
||||||
super.key,
|
super.key,
|
||||||
this.authModel,
|
|
||||||
this.readOnly = false,
|
this.readOnly = false,
|
||||||
});
|
});
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context, WidgetRef ref) {
|
Widget build(BuildContext context, WidgetRef ref) {
|
||||||
final AuthModel? currentAuthData;
|
|
||||||
final APIAuthType currentAuthType;
|
|
||||||
|
|
||||||
if (authModel != null) {
|
|
||||||
currentAuthData = authModel;
|
|
||||||
currentAuthType = authModel!.type;
|
|
||||||
} else {
|
|
||||||
final selectedRequest = ref.read(selectedRequestModelProvider);
|
final selectedRequest = ref.read(selectedRequestModelProvider);
|
||||||
if (selectedRequest == null) {
|
if (selectedRequest == null) {
|
||||||
return const SizedBox.shrink();
|
return const SizedBox.shrink();
|
||||||
}
|
}
|
||||||
|
|
||||||
currentAuthType = ref.watch(
|
ref.watch(
|
||||||
selectedRequestModelProvider.select((request) =>
|
selectedRequestModelProvider.select((request) =>
|
||||||
request?.httpRequestModel?.authModel?.type ?? APIAuthType.none),
|
request?.httpRequestModel?.authModel?.type ?? APIAuthType.none),
|
||||||
);
|
);
|
||||||
currentAuthData = selectedRequest.httpRequestModel?.authModel;
|
final currentAuthData = selectedRequest.httpRequestModel?.authModel;
|
||||||
}
|
|
||||||
return SingleChildScrollView(
|
return AuthPage(
|
||||||
child: Padding(
|
authModel: currentAuthData,
|
||||||
padding: const EdgeInsets.all(16.0),
|
readOnly: readOnly,
|
||||||
child: Column(
|
onChangedAuthType: (newType) {
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
final selectedRequest = ref.read(selectedRequestModelProvider);
|
||||||
children: [
|
|
||||||
Text(
|
|
||||||
"Authentication Type",
|
|
||||||
style: TextStyle(
|
|
||||||
fontWeight: FontWeight.bold,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
SizedBox(
|
|
||||||
height: 8,
|
|
||||||
),
|
|
||||||
ADPopupMenu<APIAuthType>(
|
|
||||||
value: currentAuthType.displayType,
|
|
||||||
values: APIAuthType.values
|
|
||||||
.map((type) => (type, type.displayType))
|
|
||||||
.toList(),
|
|
||||||
tooltip: "Select Authentication Type",
|
|
||||||
isOutlined: true,
|
|
||||||
onChanged: readOnly
|
|
||||||
? null
|
|
||||||
: (APIAuthType? newType) {
|
|
||||||
final selectedRequest =
|
|
||||||
ref.read(selectedRequestModelProvider);
|
|
||||||
if (newType != null) {
|
if (newType != null) {
|
||||||
ref
|
ref.read(collectionStateNotifierProvider.notifier).update(
|
||||||
.read(collectionStateNotifierProvider.notifier)
|
authModel: selectedRequest?.httpRequestModel?.authModel
|
||||||
.update(
|
|
||||||
authModel: selectedRequest
|
|
||||||
?.httpRequestModel?.authModel
|
|
||||||
?.copyWith(type: newType) ??
|
?.copyWith(type: newType) ??
|
||||||
AuthModel(type: newType),
|
AuthModel(type: newType),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
updateAuthData: (model) {
|
||||||
const SizedBox(height: 48),
|
|
||||||
_buildAuthFields(context, ref, currentAuthData),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget _buildAuthFields(
|
|
||||||
BuildContext context,
|
|
||||||
WidgetRef ref,
|
|
||||||
AuthModel? authData,
|
|
||||||
) {
|
|
||||||
void updateAuth(AuthModel? model) {
|
|
||||||
if (model == null) {
|
if (model == null) {
|
||||||
ref.read(collectionStateNotifierProvider.notifier).update(
|
ref.read(collectionStateNotifierProvider.notifier).update(
|
||||||
authModel: AuthModel(type: APIAuthType.none),
|
authModel: AuthModel(type: APIAuthType.none),
|
||||||
@@ -100,47 +47,7 @@ class EditAuthType extends ConsumerWidget {
|
|||||||
ref.read(collectionStateNotifierProvider.notifier).update(
|
ref.read(collectionStateNotifierProvider.notifier).update(
|
||||||
authModel: model,
|
authModel: model,
|
||||||
);
|
);
|
||||||
}
|
},
|
||||||
|
|
||||||
switch (authData?.type) {
|
|
||||||
case APIAuthType.basic:
|
|
||||||
return BasicAuthFields(
|
|
||||||
readOnly: readOnly,
|
|
||||||
authData: authData,
|
|
||||||
updateAuth: updateAuth,
|
|
||||||
);
|
);
|
||||||
case APIAuthType.bearer:
|
|
||||||
return BearerAuthFields(
|
|
||||||
readOnly: readOnly,
|
|
||||||
authData: authData,
|
|
||||||
updateAuth: updateAuth,
|
|
||||||
);
|
|
||||||
case APIAuthType.apiKey:
|
|
||||||
return ApiKeyAuthFields(
|
|
||||||
readOnly: readOnly,
|
|
||||||
authData: authData,
|
|
||||||
updateAuth: updateAuth,
|
|
||||||
);
|
|
||||||
case APIAuthType.jwt:
|
|
||||||
return JwtAuthFields(
|
|
||||||
readOnly: readOnly,
|
|
||||||
authData: authData,
|
|
||||||
updateAuth: updateAuth,
|
|
||||||
);
|
|
||||||
case APIAuthType.digest:
|
|
||||||
return DigestAuthFields(
|
|
||||||
readOnly: readOnly,
|
|
||||||
authData: authData,
|
|
||||||
updateAuth: updateAuth,
|
|
||||||
);
|
|
||||||
case APIAuthType.none:
|
|
||||||
return Text(readOnly
|
|
||||||
? "No authentication was used for this request."
|
|
||||||
: "No authentication selected.");
|
|
||||||
default:
|
|
||||||
return Text(readOnly
|
|
||||||
? "Authentication details for ${authData?.type.name} are not yet supported in history view."
|
|
||||||
: "This auth type is not implemented yet.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user