From 5d8be1182a2cfec03d10a3c6a3896c32c7f8ae46 Mon Sep 17 00:00:00 2001 From: cd-bitwarden <106776772+cd-bitwarden@users.noreply.github.com> Date: Fri, 4 Aug 2023 16:22:05 -0400 Subject: [PATCH] Disable API call if the expiration date is in the past (#5831) * Disable API call if the expiration date is in the past * Updates suggested by Thomas * fixes * suggested change from thomas * removing unused service --- apps/web/src/locales/en/messages.json | 3 +++ .../access-token-create-dialog.component.ts | 1 + .../dialogs/expiration-options.component.ts | 25 ++++++++++++++++--- 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/apps/web/src/locales/en/messages.json b/apps/web/src/locales/en/messages.json index 9cef561303..c8621e3479 100644 --- a/apps/web/src/locales/en/messages.json +++ b/apps/web/src/locales/en/messages.json @@ -711,6 +711,9 @@ "unexpectedError": { "message": "An unexpected error has occurred." }, + "expirationDateError" : { + "message":"Please select an expiration date that is in the future." + }, "emailAddress": { "message": "Email address" }, diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/service-accounts/access/dialogs/access-token-create-dialog.component.ts b/bitwarden_license/bit-web/src/app/secrets-manager/service-accounts/access/dialogs/access-token-create-dialog.component.ts index 8bee9b5c49..8004166f62 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/service-accounts/access/dialogs/access-token-create-dialog.component.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/service-accounts/access/dialogs/access-token-create-dialog.component.ts @@ -51,6 +51,7 @@ export class AccessTokenCreateDialogComponent implements OnInit { if (this.formGroup.invalid) { return; } + const accessTokenView = new AccessTokenView(); accessTokenView.name = this.formGroup.value.name; accessTokenView.expireAt = this.formGroup.value.expirationDateControl; diff --git a/bitwarden_license/bit-web/src/app/secrets-manager/service-accounts/access/dialogs/expiration-options.component.ts b/bitwarden_license/bit-web/src/app/secrets-manager/service-accounts/access/dialogs/expiration-options.component.ts index 132494ef47..7eb7fd894b 100644 --- a/bitwarden_license/bit-web/src/app/secrets-manager/service-accounts/access/dialogs/expiration-options.component.ts +++ b/bitwarden_license/bit-web/src/app/secrets-manager/service-accounts/access/dialogs/expiration-options.component.ts @@ -9,10 +9,13 @@ import { NG_VALUE_ACCESSOR, ValidationErrors, Validator, + ValidatorFn, Validators, } from "@angular/forms"; import { Subject, takeUntil } from "rxjs"; +import { I18nService } from "@bitwarden/common/platform/abstractions/i18n.service"; + @Component({ selector: "sm-expiration-options", templateUrl: "./expiration-options.component.html", @@ -46,10 +49,10 @@ export class ExpirationOptionsComponent protected form = new FormGroup({ expires: new FormControl("never", [Validators.required]), - expireDateTime: new FormControl("", [Validators.required]), + expireDateTime: new FormControl("", [Validators.required, this.expiresInFutureValidator()]), }); - constructor(private datePipe: DatePipe) {} + constructor(private datePipe: DatePipe, private i18nService: I18nService) {} async ngOnInit() { this.form.valueChanges.pipe(takeUntil(this.destroy$)).subscribe(() => { @@ -74,7 +77,7 @@ export class ExpirationOptionsComponent validate(control: AbstractControl): ValidationErrors { if ( - (this.form.value.expires == "custom" && this.form.value.expireDateTime) || + (this.form.value.expires == "custom" && !this.form.invalid) || this.form.value.expires !== "custom" ) { return null; @@ -111,4 +114,20 @@ export class ExpirationOptionsComponent currentDate.setDate(currentDate.getDate() + Number(this.form.value.expires)); return currentDate; } + + expiresInFutureValidator(): ValidatorFn { + return (control: AbstractControl): ValidationErrors | null => { + const enteredDate = new Date(control.value); + + if (enteredDate > new Date()) { + return null; + } else { + return { + ValidationError: { + message: this.i18nService.t("expirationDateError"), + }, + }; + } + }; + } }