diff --git a/lib/screens/common_widgets/auth/oauth1_fields.dart b/lib/screens/common_widgets/auth/oauth1_fields.dart index f942c167..20a56a46 100644 --- a/lib/screens/common_widgets/auth/oauth1_fields.dart +++ b/lib/screens/common_widgets/auth/oauth1_fields.dart @@ -1,11 +1,12 @@ -import 'package:apidash/utils/utils.dart'; +import 'package:apidash/providers/settings_providers.dart'; import 'package:apidash/widgets/widgets.dart'; import 'package:apidash_core/apidash_core.dart'; import 'package:apidash_design_system/widgets/widgets.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'consts.dart'; -class OAuth1Fields extends StatefulWidget { +class OAuth1Fields extends ConsumerStatefulWidget { final AuthModel? authData; final bool readOnly; @@ -20,10 +21,10 @@ class OAuth1Fields extends StatefulWidget { }); @override - State createState() => _OAuth1FieldsState(); + ConsumerState createState() => _OAuth1FieldsState(); } -class _OAuth1FieldsState extends State { +class _OAuth1FieldsState extends ConsumerState { late TextEditingController _consumerKeyController; late TextEditingController _consumerSecretController; late TextEditingController _accessTokenController; @@ -61,8 +62,8 @@ class _OAuth1FieldsState extends State { @override Widget build(BuildContext context) { - return Column( - crossAxisAlignment: CrossAxisAlignment.start, + return ListView( + shrinkWrap: true, children: [ // Text( // "Add auth data to", @@ -198,12 +199,11 @@ class _OAuth1FieldsState extends State { } void _updateOAuth1() async { - final String? credentialsFilePath = - await getApplicationSupportDirectoryFilePath( - "oauth1_credentials", "json"); - if (credentialsFilePath == null) { - return; - } + final settingsModel = ref.read(settingsProvider); + final credentialsFilePath = settingsModel.workspaceFolderPath != null + ? "${settingsModel.workspaceFolderPath}/oauth1_credentials.json" + : null; + widget.updateAuth?.call( widget.authData?.copyWith( type: APIAuthType.oauth1, diff --git a/lib/screens/common_widgets/auth/oauth2_field.dart b/lib/screens/common_widgets/auth/oauth2_field.dart index f46f7880..8eece742 100644 --- a/lib/screens/common_widgets/auth/oauth2_field.dart +++ b/lib/screens/common_widgets/auth/oauth2_field.dart @@ -384,9 +384,6 @@ class _OAuth2FieldsState extends ConsumerState { void _updateOAuth2() async { final String? credentialsFilePath = ref.read(settingsProvider).workspaceFolderPath; - if (credentialsFilePath == null) { - return; - } final updatedOAuth2 = AuthOAuth2Model( grantType: _grantType, @@ -394,7 +391,9 @@ class _OAuth2FieldsState extends ConsumerState { clientId: _clientIdController.text.trim(), accessTokenUrl: _accessTokenUrlController.text.trim(), clientSecret: _clientSecretController.text.trim(), - credentialsFilePath: "$credentialsFilePath/oauth2_credentials.json", + credentialsFilePath: credentialsFilePath != null + ? "$credentialsFilePath/oauth2_credentials.json" + : null, codeChallengeMethod: _codeChallengeMethod, redirectUrl: _redirectUrlController.text.trim(), scope: _scopeController.text.trim(), diff --git a/packages/better_networking/lib/models/auth/auth_oauth1_model.dart b/packages/better_networking/lib/models/auth/auth_oauth1_model.dart index 815377f2..981682dc 100644 --- a/packages/better_networking/lib/models/auth/auth_oauth1_model.dart +++ b/packages/better_networking/lib/models/auth/auth_oauth1_model.dart @@ -10,10 +10,11 @@ class AuthOAuth1Model with _$AuthOAuth1Model { const factory AuthOAuth1Model({ required String consumerKey, required String consumerSecret, - required String credentialsFilePath, + String? credentialsFilePath, String? accessToken, String? tokenSecret, - @Default(OAuth1SignatureMethod.hmacSha1) OAuth1SignatureMethod signatureMethod, + @Default(OAuth1SignatureMethod.hmacSha1) + OAuth1SignatureMethod signatureMethod, @Default("header") String parameterLocation, @Default('1.0') String version, String? realm, diff --git a/packages/better_networking/lib/models/auth/auth_oauth1_model.freezed.dart b/packages/better_networking/lib/models/auth/auth_oauth1_model.freezed.dart index 2d3a2007..87735f49 100644 --- a/packages/better_networking/lib/models/auth/auth_oauth1_model.freezed.dart +++ b/packages/better_networking/lib/models/auth/auth_oauth1_model.freezed.dart @@ -23,7 +23,7 @@ AuthOAuth1Model _$AuthOAuth1ModelFromJson(Map json) { mixin _$AuthOAuth1Model { String get consumerKey => throw _privateConstructorUsedError; String get consumerSecret => throw _privateConstructorUsedError; - String get credentialsFilePath => throw _privateConstructorUsedError; + String? get credentialsFilePath => throw _privateConstructorUsedError; String? get accessToken => throw _privateConstructorUsedError; String? get tokenSecret => throw _privateConstructorUsedError; OAuth1SignatureMethod get signatureMethod => @@ -57,7 +57,7 @@ abstract class $AuthOAuth1ModelCopyWith<$Res> { $Res call({ String consumerKey, String consumerSecret, - String credentialsFilePath, + String? credentialsFilePath, String? accessToken, String? tokenSecret, OAuth1SignatureMethod signatureMethod, @@ -89,7 +89,7 @@ class _$AuthOAuth1ModelCopyWithImpl<$Res, $Val extends AuthOAuth1Model> $Res call({ Object? consumerKey = null, Object? consumerSecret = null, - Object? credentialsFilePath = null, + Object? credentialsFilePath = freezed, Object? accessToken = freezed, Object? tokenSecret = freezed, Object? signatureMethod = null, @@ -112,10 +112,10 @@ class _$AuthOAuth1ModelCopyWithImpl<$Res, $Val extends AuthOAuth1Model> ? _value.consumerSecret : consumerSecret // ignore: cast_nullable_to_non_nullable as String, - credentialsFilePath: null == credentialsFilePath + credentialsFilePath: freezed == credentialsFilePath ? _value.credentialsFilePath : credentialsFilePath // ignore: cast_nullable_to_non_nullable - as String, + as String?, accessToken: freezed == accessToken ? _value.accessToken : accessToken // ignore: cast_nullable_to_non_nullable @@ -178,7 +178,7 @@ abstract class _$$AuthOAuth1ModelImplCopyWith<$Res> $Res call({ String consumerKey, String consumerSecret, - String credentialsFilePath, + String? credentialsFilePath, String? accessToken, String? tokenSecret, OAuth1SignatureMethod signatureMethod, @@ -209,7 +209,7 @@ class __$$AuthOAuth1ModelImplCopyWithImpl<$Res> $Res call({ Object? consumerKey = null, Object? consumerSecret = null, - Object? credentialsFilePath = null, + Object? credentialsFilePath = freezed, Object? accessToken = freezed, Object? tokenSecret = freezed, Object? signatureMethod = null, @@ -232,10 +232,10 @@ class __$$AuthOAuth1ModelImplCopyWithImpl<$Res> ? _value.consumerSecret : consumerSecret // ignore: cast_nullable_to_non_nullable as String, - credentialsFilePath: null == credentialsFilePath + credentialsFilePath: freezed == credentialsFilePath ? _value.credentialsFilePath : credentialsFilePath // ignore: cast_nullable_to_non_nullable - as String, + as String?, accessToken: freezed == accessToken ? _value.accessToken : accessToken // ignore: cast_nullable_to_non_nullable @@ -291,7 +291,7 @@ class _$AuthOAuth1ModelImpl implements _AuthOAuth1Model { const _$AuthOAuth1ModelImpl({ required this.consumerKey, required this.consumerSecret, - required this.credentialsFilePath, + this.credentialsFilePath, this.accessToken, this.tokenSecret, this.signatureMethod = OAuth1SignatureMethod.hmacSha1, @@ -313,7 +313,7 @@ class _$AuthOAuth1ModelImpl implements _AuthOAuth1Model { @override final String consumerSecret; @override - final String credentialsFilePath; + final String? credentialsFilePath; @override final String? accessToken; @override @@ -419,7 +419,7 @@ abstract class _AuthOAuth1Model implements AuthOAuth1Model { const factory _AuthOAuth1Model({ required final String consumerKey, required final String consumerSecret, - required final String credentialsFilePath, + final String? credentialsFilePath, final String? accessToken, final String? tokenSecret, final OAuth1SignatureMethod signatureMethod, @@ -441,7 +441,7 @@ abstract class _AuthOAuth1Model implements AuthOAuth1Model { @override String get consumerSecret; @override - String get credentialsFilePath; + String? get credentialsFilePath; @override String? get accessToken; @override diff --git a/packages/better_networking/lib/models/auth/auth_oauth1_model.g.dart b/packages/better_networking/lib/models/auth/auth_oauth1_model.g.dart index 0d0413d4..7a5cc2f7 100644 --- a/packages/better_networking/lib/models/auth/auth_oauth1_model.g.dart +++ b/packages/better_networking/lib/models/auth/auth_oauth1_model.g.dart @@ -11,7 +11,7 @@ _$AuthOAuth1ModelImpl _$$AuthOAuth1ModelImplFromJson( ) => _$AuthOAuth1ModelImpl( consumerKey: json['consumerKey'] as String, consumerSecret: json['consumerSecret'] as String, - credentialsFilePath: json['credentialsFilePath'] as String, + credentialsFilePath: json['credentialsFilePath'] as String?, accessToken: json['accessToken'] as String?, tokenSecret: json['tokenSecret'] as String?, signatureMethod: diff --git a/packages/better_networking/lib/models/auth/auth_oauth2_model.dart b/packages/better_networking/lib/models/auth/auth_oauth2_model.dart index 94e92276..e6dcb8ee 100644 --- a/packages/better_networking/lib/models/auth/auth_oauth2_model.dart +++ b/packages/better_networking/lib/models/auth/auth_oauth2_model.dart @@ -13,7 +13,7 @@ class AuthOAuth2Model with _$AuthOAuth2Model { required String accessTokenUrl, required String clientId, required String clientSecret, - required String credentialsFilePath, + String? credentialsFilePath, String? redirectUrl, String? scope, String? state, diff --git a/packages/better_networking/lib/models/auth/auth_oauth2_model.freezed.dart b/packages/better_networking/lib/models/auth/auth_oauth2_model.freezed.dart index 1264f8b0..145d35cf 100644 --- a/packages/better_networking/lib/models/auth/auth_oauth2_model.freezed.dart +++ b/packages/better_networking/lib/models/auth/auth_oauth2_model.freezed.dart @@ -26,7 +26,7 @@ mixin _$AuthOAuth2Model { String get accessTokenUrl => throw _privateConstructorUsedError; String get clientId => throw _privateConstructorUsedError; String get clientSecret => throw _privateConstructorUsedError; - String get credentialsFilePath => throw _privateConstructorUsedError; + String? get credentialsFilePath => throw _privateConstructorUsedError; String? get redirectUrl => throw _privateConstructorUsedError; String? get scope => throw _privateConstructorUsedError; String? get state => throw _privateConstructorUsedError; @@ -62,7 +62,7 @@ abstract class $AuthOAuth2ModelCopyWith<$Res> { String accessTokenUrl, String clientId, String clientSecret, - String credentialsFilePath, + String? credentialsFilePath, String? redirectUrl, String? scope, String? state, @@ -97,7 +97,7 @@ class _$AuthOAuth2ModelCopyWithImpl<$Res, $Val extends AuthOAuth2Model> Object? accessTokenUrl = null, Object? clientId = null, Object? clientSecret = null, - Object? credentialsFilePath = null, + Object? credentialsFilePath = freezed, Object? redirectUrl = freezed, Object? scope = freezed, Object? state = freezed, @@ -132,10 +132,10 @@ class _$AuthOAuth2ModelCopyWithImpl<$Res, $Val extends AuthOAuth2Model> ? _value.clientSecret : clientSecret // ignore: cast_nullable_to_non_nullable as String, - credentialsFilePath: null == credentialsFilePath + credentialsFilePath: freezed == credentialsFilePath ? _value.credentialsFilePath : credentialsFilePath // ignore: cast_nullable_to_non_nullable - as String, + as String?, redirectUrl: freezed == redirectUrl ? _value.redirectUrl : redirectUrl // ignore: cast_nullable_to_non_nullable @@ -201,7 +201,7 @@ abstract class _$$AuthOAuth2ModelImplCopyWith<$Res> String accessTokenUrl, String clientId, String clientSecret, - String credentialsFilePath, + String? credentialsFilePath, String? redirectUrl, String? scope, String? state, @@ -235,7 +235,7 @@ class __$$AuthOAuth2ModelImplCopyWithImpl<$Res> Object? accessTokenUrl = null, Object? clientId = null, Object? clientSecret = null, - Object? credentialsFilePath = null, + Object? credentialsFilePath = freezed, Object? redirectUrl = freezed, Object? scope = freezed, Object? state = freezed, @@ -270,10 +270,10 @@ class __$$AuthOAuth2ModelImplCopyWithImpl<$Res> ? _value.clientSecret : clientSecret // ignore: cast_nullable_to_non_nullable as String, - credentialsFilePath: null == credentialsFilePath + credentialsFilePath: freezed == credentialsFilePath ? _value.credentialsFilePath : credentialsFilePath // ignore: cast_nullable_to_non_nullable - as String, + as String?, redirectUrl: freezed == redirectUrl ? _value.redirectUrl : redirectUrl // ignore: cast_nullable_to_non_nullable @@ -332,7 +332,7 @@ class _$AuthOAuth2ModelImpl implements _AuthOAuth2Model { required this.accessTokenUrl, required this.clientId, required this.clientSecret, - required this.credentialsFilePath, + this.credentialsFilePath, this.redirectUrl, this.scope, this.state, @@ -361,7 +361,7 @@ class _$AuthOAuth2ModelImpl implements _AuthOAuth2Model { @override final String clientSecret; @override - final String credentialsFilePath; + final String? credentialsFilePath; @override final String? redirectUrl; @override @@ -477,7 +477,7 @@ abstract class _AuthOAuth2Model implements AuthOAuth2Model { required final String accessTokenUrl, required final String clientId, required final String clientSecret, - required final String credentialsFilePath, + final String? credentialsFilePath, final String? redirectUrl, final String? scope, final String? state, @@ -505,7 +505,7 @@ abstract class _AuthOAuth2Model implements AuthOAuth2Model { @override String get clientSecret; @override - String get credentialsFilePath; + String? get credentialsFilePath; @override String? get redirectUrl; @override diff --git a/packages/better_networking/lib/models/auth/auth_oauth2_model.g.dart b/packages/better_networking/lib/models/auth/auth_oauth2_model.g.dart index 7865fc2f..68515dd8 100644 --- a/packages/better_networking/lib/models/auth/auth_oauth2_model.g.dart +++ b/packages/better_networking/lib/models/auth/auth_oauth2_model.g.dart @@ -16,7 +16,7 @@ _$AuthOAuth2ModelImpl _$$AuthOAuth2ModelImplFromJson( accessTokenUrl: json['accessTokenUrl'] as String, clientId: json['clientId'] as String, clientSecret: json['clientSecret'] as String, - credentialsFilePath: json['credentialsFilePath'] as String, + credentialsFilePath: json['credentialsFilePath'] as String?, redirectUrl: json['redirectUrl'] as String?, scope: json['scope'] as String?, state: json['state'] as String?, diff --git a/packages/better_networking/lib/utils/auth/handle_auth.dart b/packages/better_networking/lib/utils/auth/handle_auth.dart index bea9d765..795281ac 100644 --- a/packages/better_networking/lib/utils/auth/handle_auth.dart +++ b/packages/better_networking/lib/utils/auth/handle_auth.dart @@ -188,7 +188,9 @@ Future handleAuth( throw Exception("No Redirect URL found!"); } - final credentialsFile = File(oauth2.credentialsFilePath); + final credentialsFile = oauth2.credentialsFilePath != null + ? File(oauth2.credentialsFilePath!) + : null; switch (oauth2.grantType) { case OAuth2GrantType.authorizationCode: diff --git a/packages/better_networking/lib/utils/auth/oauth2_utils.dart b/packages/better_networking/lib/utils/auth/oauth2_utils.dart index 9ecfb28a..798073b5 100644 --- a/packages/better_networking/lib/utils/auth/oauth2_utils.dart +++ b/packages/better_networking/lib/utils/auth/oauth2_utils.dart @@ -20,12 +20,12 @@ Future<(oauth2.Client, OAuthCallbackServer?)> oAuth2AuthorizationCodeGrant({ required Uri authorizationEndpoint, required Uri tokenEndpoint, required Uri redirectUrl, - required File credentialsFile, + required File? credentialsFile, String? state, String? scope, }) async { - // Check for existing valid credentials first - if (await credentialsFile.exists()) { + // Check for existing credentials first + if (credentialsFile != null && await credentialsFile.exists()) { try { final json = await credentialsFile.readAsString(); final credentials = oauth2.Credentials.fromJson(json); @@ -139,7 +139,9 @@ Future<(oauth2.Client, OAuthCallbackServer?)> oAuth2AuthorizationCodeGrant({ ); log('OAuth2 authorization successful, saving credentials'); - await credentialsFile.writeAsString(client.credentials.toJson()); + if (credentialsFile != null) { + await credentialsFile.writeAsString(client.credentials.toJson()); + } log(client.credentials.toJson()); return (client, callbackServer); @@ -166,10 +168,10 @@ Future<(oauth2.Client, OAuthCallbackServer?)> oAuth2AuthorizationCodeGrant({ Future oAuth2ClientCredentialsGrantHandler({ required AuthOAuth2Model oauth2Model, - required File credentialsFile, + required File? credentialsFile, }) async { // Try to use saved credentials - if (await credentialsFile.exists()) { + if (credentialsFile != null && await credentialsFile.exists()) { try { final json = await credentialsFile.readAsString(); final credentials = oauth2.Credentials.fromJson(json); @@ -211,8 +213,10 @@ Future oAuth2ClientCredentialsGrantHandler({ log('Successfully authenticated via client credentials grant'); try { - await credentialsFile.writeAsString(client.credentials.toJson()); - log('Saved credentials to file'); + if (credentialsFile != null) { + await credentialsFile.writeAsString(client.credentials.toJson()); + log('Saved credentials to file'); + } } catch (e) { log('Failed to save credentials: $e'); } @@ -230,10 +234,10 @@ Future oAuth2ClientCredentialsGrantHandler({ Future oAuth2ResourceOwnerPasswordGrantHandler({ required AuthOAuth2Model oauth2Model, - required File credentialsFile, + required File? credentialsFile, }) async { // Try to use saved credentials - if (await credentialsFile.exists()) { + if (credentialsFile != null && await credentialsFile.exists()) { try { final json = await credentialsFile.readAsString(); final credentials = oauth2.Credentials.fromJson(json); @@ -281,8 +285,10 @@ Future oAuth2ResourceOwnerPasswordGrantHandler({ log('Successfully authenticated via client credentials grant'); try { - await credentialsFile.writeAsString(client.credentials.toJson()); - log('Saved credentials to file'); + if (credentialsFile != null) { + await credentialsFile.writeAsString(client.credentials.toJson()); + log('Saved credentials to file'); + } } catch (e) { log('Failed to save credentials: $e'); }