Skip to content
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

fix: empty session and tunnel username are treated as null. #718

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class _ProfileFormState extends ConsumerState<ProfileFormDesktopView> {
void onSubmit(SshnpParams oldConfig) async {
if (_formkey.currentState!.validate()) {
_formkey.currentState!.save();
SshnpParams config = SshnpParams.merge(oldConfig, newConfig);
SshnpParams config = SshnpParams.merge(SshnpParams.empty(), newConfig);

// get the controller for the profile that is about to be saved. Since this profile is not saved a log will be printed stating that the profile does not exist in keystore.
final controller = ref.read(configFamilyController(newConfig.profileName!).notifier);
Expand Down Expand Up @@ -151,29 +151,35 @@ class _ProfileFormState extends ConsumerState<ProfileFormDesktopView> {
newConfig,
SshnpPartialParams(host: value),
),
validator: FormValidator.validateRequiredField,
validator: FormValidator.validateAtsignField,
),
gapH20,
Text(strings.connectionConfiguration, style: Theme.of(context).textTheme.bodyLarge),
gapH20,
ProfileFormCard(
formFields: [
CustomTextFormField(
initialValue: oldConfig.remoteUsername ?? '',
initialValue: oldConfig.remoteUsername,
labelText: strings.remoteUserName,
toolTip: strings.remoteUserNameTooltip,
onSaved: (value) {
if (value == '') {
value = null;
}
newConfig = SshnpPartialParams.merge(
newConfig,
SshnpPartialParams(remoteUsername: value),
);
}),
gapH10,
CustomTextFormField(
initialValue: oldConfig.tunnelUsername ?? '',
initialValue: oldConfig.tunnelUsername,
labelText: strings.tunnelUserName,
toolTip: strings.tunnelUserNameTooltip,
onSaved: (value) {
if (value == '') {
value = null;
}
newConfig = SshnpPartialParams.merge(
newConfig,
SshnpPartialParams(tunnelUsername: value),
Expand All @@ -184,21 +190,22 @@ class _ProfileFormState extends ConsumerState<ProfileFormDesktopView> {
initialValue: oldConfig.remoteSshdPort.toString(),
labelText: strings.remoteSshdPort,
toolTip: strings.remoteSshdPortTooltip,
onChanged: (value) => newConfig = SshnpPartialParams.merge(
onSaved: (value) => newConfig = SshnpPartialParams.merge(
newConfig,
SshnpPartialParams(remoteSshdPort: int.tryParse(value)),
SshnpPartialParams(remoteSshdPort: int.tryParse(value!)),
),
validator: FormValidator.validateRequiredField,
validator: FormValidator.validateRequiredPortField,
),
gapH10,
CustomTextFormField(
initialValue: oldConfig.localPort.toString(),
labelText: strings.localPort,
toolTip: strings.localPortTooltip,
onChanged: (value) => newConfig = SshnpPartialParams.merge(
onSaved: (value) => newConfig = SshnpPartialParams.merge(
newConfig,
SshnpPartialParams(localPort: int.tryParse(value)),
SshnpPartialParams(localPort: int.tryParse(value!)),
),
validator: FormValidator.validateRequiredPortField,
),
gapH10,
gapH12,
Expand Down Expand Up @@ -288,9 +295,9 @@ class _ProfileFormState extends ConsumerState<ProfileFormDesktopView> {
hintText: strings.localSshOptionsHint,
labelText: strings.localSshOptions,
toolTip: strings.localSshOptionsTooltip,
onChanged: (value) => newConfig = SshnpPartialParams.merge(
onSaved: (value) => newConfig = SshnpPartialParams.merge(
newConfig,
SshnpPartialParams(localSshOptions: value.split(',')),
SshnpPartialParams(localSshOptions: value?.split(',')),
),
),
gapH10,
Expand All @@ -302,6 +309,7 @@ class _ProfileFormState extends ConsumerState<ProfileFormDesktopView> {
newConfig,
SshnpPartialParams(rootDomain: value),
),
validator: FormValidator.validateRequiredField,
),
],
),
Expand Down
2 changes: 2 additions & 0 deletions packages/dart/sshnp_flutter/lib/src/utility/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const kEmptyFieldValidationError = 'Field cannot be left blank';
const kAtsignFieldValidationError = 'Field must start with @';
const kProfileNameFieldValidationError = 'Field must only use lower case alphanumeric characters spaces';
const kPrivateKeyFieldValidationError = 'Field must only use lower case alphanumeric characters';
const kIntFieldValidationError = 'Field must only use numbers';
const kPortFieldValidationError = 'Field must use a valid port number';

const kPrivateKeyDropDownOption = 'Create a new private key';

Expand Down
13 changes: 13 additions & 0 deletions packages/dart/sshnp_flutter/lib/src/utility/form_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,25 @@ class FormValidator {
return null;
}

static String? validateRequiredPortField(String? value) {
String valid = r'^[0-9]+$';
if (value?.isEmpty ?? true) {
return kEmptyFieldValidationError;
} else if (!RegExp(valid).hasMatch(value!)) {
return kPortFieldValidationError;
} else if (!(int.parse(value) >= 0 && int.parse(value) <= 65535)) {
return kPortFieldValidationError;
}
return null;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically for ports, we can do one better, and say the range is 0-65535

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

static String? validateAtsignField(String? value) {
if (value?.isEmpty ?? true) {
return kEmptyFieldValidationError;
} else if (!value!.startsWith('@')) {
return kAtsignFieldValidationError;
}
validateRequiredField(value);
return null;
}

Expand Down