Skip to content

Added Basic Authentication feature #687

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,12 @@ const kLabelImport = "Import";
const kUntitled = "untitled";
// Request Pane
const kLabelRequest = "Request";
const kLabelAuthorizationType = "Authorization Type";
const kLabelHideCode = "Hide Code";
const kLabelViewCode = "View Code";
const kLabelURLParams = "URL Params";
const kLabelHeaders = "Headers";
const kLabelAuthorization = "Authorization";
const kLabelBody = "Body";
const kLabelQuery = "Query";
const kNameCheckbox = "Checkbox";
Expand Down
82 changes: 82 additions & 0 deletions lib/providers/auth_providers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'dart:convert';
import 'package:apidash_core/apidash_core.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

enum AuthType {
none,
basicAuth,
apiKey,
bearerToken,
jwtBearer,
digestAuth,
oauth1,
oauth2,
}

String authTypeToString(AuthType authType) {
switch (authType) {
case AuthType.basicAuth:
return "Basic Auth";
case AuthType.apiKey:
return "API Key";
case AuthType.bearerToken:
return "Bearer Token";
case AuthType.jwtBearer:
return "JWT Bearer";
case AuthType.digestAuth:
return "Digest Auth";
case AuthType.oauth1:
return "OAuth 1.0";
case AuthType.oauth2:
return "OAuth 2.0";
case AuthType.none:
default:
return "None";
}
}

final authTypeProvider = StateProvider<AuthType>((ref) => AuthType.none);
final authCredentialsProvider = StateProvider<Map<String, String>>((ref) => {
'username': '',
'password': '',
'apiKey': '',
'token': '',
});

void updateAuthType(WidgetRef ref, AuthType newAuthType) {
ref.read(authTypeProvider.notifier).state = newAuthType;
}

void updateCredentials(WidgetRef ref, Map<String, String> newCredentials) {
ref.read(authCredentialsProvider.notifier).update((state) => {
...state,
...newCredentials,
});
}

List<NameValueModel> generateAuthHeaders(WidgetRef ref) {
final authType = ref.read(authTypeProvider);
final credentials = ref.read(authCredentialsProvider);

switch (authType) {
case AuthType.basicAuth:
if (credentials['username']?.isNotEmpty == true &&
credentials['password']?.isNotEmpty == true) {
String basicAuth =
'Basic ${base64Encode(utf8.encode('${credentials['username']}:${credentials['password']}'))}';
return [NameValueModel(name: "Authorization", value: basicAuth)];
}
break;
case AuthType.bearerToken:
case AuthType.jwtBearer:
break;
case AuthType.apiKey:
break;
case AuthType.oauth1:
case AuthType.oauth2:
case AuthType.digestAuth:
case AuthType.none:
break;
}
return [];
}
1 change: 1 addition & 0 deletions lib/providers/providers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ export 'environment_providers.dart';
export 'history_providers.dart';
export 'settings_providers.dart';
export 'ui_providers.dart';
export 'auth_providers.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import 'dart:convert';

import 'package:apidash/consts.dart';
import 'package:apidash/providers/collection_providers.dart';
import 'package:apidash_core/apidash_core.dart';
import 'package:apidash_design_system/tokens/tokens.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../../../../providers/auth_providers.dart';

class EditRequestAuthorization extends ConsumerStatefulWidget {
const EditRequestAuthorization({super.key});

@override
ConsumerState<EditRequestAuthorization> createState() =>
_EditRequestAuthorizationState();
}

class _EditRequestAuthorizationState
extends ConsumerState<EditRequestAuthorization> {
late TextEditingController _usernameController;
late TextEditingController _passwordController;

@override
void initState() {
super.initState();
_usernameController = TextEditingController();
_passwordController = TextEditingController();

_usernameController.addListener(() {
ref.read(authCredentialsProvider.notifier).update((state) => {
...state,
'username': _usernameController.text,
});
});

_passwordController.addListener(() {
ref.read(authCredentialsProvider.notifier).update((state) => {
...state,
'password': _passwordController.text,
});
});
}

@override
void dispose() {
_usernameController.dispose();
_passwordController.dispose();
super.dispose();
}

void _updateHeaders(AuthType? authType, String? username, String? password) {
final headers = _generateAuthHeaders(authType, username, password);
ref.read(collectionStateNotifierProvider.notifier).update(headers: headers);
}

List<NameValueModel> _generateAuthHeaders(
AuthType? authType, String? username, String? password) {
if (authType == AuthType.basicAuth &&
username != null &&
password != null &&
username.isNotEmpty &&
password.isNotEmpty) {
String basicAuth =
'Basic ${base64Encode(utf8.encode('$username:$password'))}';
return [NameValueModel(name: "Authorization", value: basicAuth)];
}
return [];
}

@override
Widget build(BuildContext context) {
final authType = ref.watch(authTypeProvider);
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 10),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
kVSpacer16,
DropdownButtonFormField<AuthType>(
value: authType,
decoration: InputDecoration(
labelText: kLabelAuthorizationType,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
contentPadding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
),
items: AuthType.values.map((AuthType type) {
return DropdownMenuItem(
value: type, child: Text(type.toString().split('.').last));
}).toList(),
onChanged: (value) {
if (value != null) {
ref.read(authTypeProvider.notifier).update((state) => value);
_updateHeaders(
value, _usernameController.text, _passwordController.text);
}
},
),
const SizedBox(height: 16),
if (authType == AuthType.basicAuth) ...[
TextFormField(
controller: _usernameController,
decoration: InputDecoration(
labelText: "Username",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
contentPadding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
),
onChanged: (value) {
ref.read(authCredentialsProvider.notifier).update((state) => {
...state,
'username': value,
});
_updateHeaders(authType, value, _passwordController.text);
},
),
const SizedBox(height: 12),
TextFormField(
controller: _passwordController,
decoration: InputDecoration(
labelText: "Password",
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
contentPadding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
),
obscureText: true,
onChanged: (value) {
ref.read(authCredentialsProvider.notifier).update((state) => {
...state,
'password': value,
});
_updateHeaders(authType, _usernameController.text, value);
},
),
],
],
),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:apidash/consts.dart';
import 'package:apidash/screens/home_page/editor_pane/details_card/request_pane/request_authorization.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash/providers/providers.dart';
Expand All @@ -21,6 +22,9 @@ class EditGraphQLRequestPane extends ConsumerWidget {
final hasQuery = ref.watch(selectedRequestModelProvider
.select((value) => value?.httpRequestModel?.hasQuery)) ??
false;

final authEnabled = ref.watch(authTypeProvider) != AuthType.none;

if (tabIndex >= 2) {
tabIndex = 0;
}
Expand All @@ -38,14 +42,17 @@ class EditGraphQLRequestPane extends ConsumerWidget {
.update(requestTabIndex: index);
},
showIndicators: [
headerLength > 0,
authEnabled,
headerLength > 0 && !authEnabled,
hasQuery,
],
tabLabels: const [
kLabelAuthorization,
kLabelHeaders,
kLabelQuery,
],
children: const [
EditRequestAuthorization(),
EditRequestHeaders(),
EditRequestBody(),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'package:apidash/consts.dart';
import 'package:apidash/screens/home_page/editor_pane/details_card/request_pane/request_authorization.dart';
import 'package:apidash/screens/home_page/editor_pane/details_card/request_pane/request_headers.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:apidash/providers/providers.dart';
import 'package:apidash/widgets/widgets.dart';
import 'request_headers.dart';
import 'request_params.dart';
import 'request_body.dart';

Expand All @@ -26,7 +27,7 @@ class EditRestRequestPane extends ConsumerWidget {
final hasBody = ref.watch(selectedRequestModelProvider
.select((value) => value?.httpRequestModel?.hasBody)) ??
false;

final authEnabled = ref.watch(authTypeProvider) != AuthType.none;
return RequestPane(
selectedId: selectedId,
codePaneVisible: codePaneVisible,
Expand All @@ -42,16 +43,19 @@ class EditRestRequestPane extends ConsumerWidget {
},
showIndicators: [
paramLength > 0,
headerLength > 0,
authEnabled,
headerLength > 0 && !authEnabled,
hasBody,
],
tabLabels: const [
kLabelURLParams,
kLabelAuthorization,
kLabelHeaders,
kLabelBody,
],
children: const [
EditRequestURLParams(),
EditRequestAuthorization(),
EditRequestHeaders(),
EditRequestBody(),
],
Expand Down
2 changes: 1 addition & 1 deletion lib/widgets/request_pane.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class _RequestPaneState extends State<RequestPane>
@override
Widget build(BuildContext context) {
final TabController controller = useTabController(
initialLength: widget.children.length,
initialLength: widget.tabLabels.length,
vsync: this,
);
if (widget.tabIndex != null) {
Expand Down