core: fix logic for token expiration (#9426)
* core: fix logic for token expiration Signed-off-by: Jens Langhammer <jens@goauthentik.io> * bump default token expiration Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix frontend Signed-off-by: Jens Langhammer <jens@goauthentik.io> * fix Signed-off-by: Jens Langhammer <jens@goauthentik.io> --------- Signed-off-by: Jens Langhammer <jens@goauthentik.io>
This commit is contained in:
@ -111,6 +111,21 @@ export function dateTimeLocal(date: Date): string {
|
||||
return `${parts[0]}:${parts[1]}`;
|
||||
}
|
||||
|
||||
export function dateToUTC(date: Date): Date {
|
||||
// Sigh...so our API is UTC/can take TZ info in the ISO format as it should.
|
||||
// datetime-local fields (which is almost the only date-time input we use)
|
||||
// can return its value as a UTC timestamp...however the generated API client
|
||||
// _requires_ a Date object, only to then convert it to an ISO string anyways
|
||||
// JS Dates don't include timezone info in the ISO string, so that just sends
|
||||
// the local time as UTC...which is wrong
|
||||
// Instead we have to do this, convert the given date to a UTC timestamp,
|
||||
// then subtract the timezone offset to create an "invalid" date (correct time&date)
|
||||
// but it still "thinks" it's in local TZ
|
||||
const timestamp = date.getTime();
|
||||
const offset = -1 * (new Date().getTimezoneOffset() * 60000);
|
||||
return new Date(timestamp - offset);
|
||||
}
|
||||
|
||||
// Lit is extremely well-typed with regard to CSS, and Storybook's `build` does not currently have a
|
||||
// coherent way of importing CSS-as-text into CSSStyleSheet. It works well when Storybook is running
|
||||
// in `dev,` but in `build` it fails. Storied components will have to map their textual CSS imports
|
||||
|
@ -1,6 +1,6 @@
|
||||
import { EVENT_REFRESH } from "@goauthentik/common/constants";
|
||||
import { MessageLevel } from "@goauthentik/common/messages";
|
||||
import { camelToSnake, convertToSlug } from "@goauthentik/common/utils";
|
||||
import { camelToSnake, convertToSlug, dateToUTC } from "@goauthentik/common/utils";
|
||||
import { AKElement } from "@goauthentik/elements/Base";
|
||||
import { HorizontalFormElement } from "@goauthentik/elements/forms/HorizontalFormElement";
|
||||
import { SearchSelect } from "@goauthentik/elements/forms/SearchSelect";
|
||||
@ -104,7 +104,7 @@ export function serializeForm<T extends KeyUnknown>(
|
||||
inputElement.tagName.toLowerCase() === "input" &&
|
||||
inputElement.type === "datetime-local"
|
||||
) {
|
||||
assignValue(inputElement, new Date(inputElement.valueAsNumber), json);
|
||||
assignValue(inputElement, dateToUTC(new Date(inputElement.valueAsNumber)), json);
|
||||
} else if (
|
||||
inputElement.tagName.toLowerCase() === "input" &&
|
||||
"type" in inputElement.dataset &&
|
||||
@ -112,7 +112,7 @@ export function serializeForm<T extends KeyUnknown>(
|
||||
) {
|
||||
// Workaround for Firefox <93, since 92 and older don't support
|
||||
// datetime-local fields
|
||||
assignValue(inputElement, new Date(inputElement.value), json);
|
||||
assignValue(inputElement, dateToUTC(new Date(inputElement.value)), json);
|
||||
} else if (
|
||||
inputElement.tagName.toLowerCase() === "input" &&
|
||||
inputElement.type === "checkbox"
|
||||
|
@ -1,3 +1,4 @@
|
||||
import { dateTimeLocal } from "@goauthentik/authentik/common/utils";
|
||||
import { DEFAULT_CONFIG } from "@goauthentik/common/api/config";
|
||||
import "@goauthentik/elements/forms/HorizontalFormElement";
|
||||
import { ModelForm } from "@goauthentik/elements/forms/ModelForm";
|
||||
@ -44,11 +45,8 @@ export class UserTokenForm extends ModelForm<Token, string> {
|
||||
renderForm(): TemplateResult {
|
||||
const now = new Date();
|
||||
const expiringDate = this.instance?.expires
|
||||
? new Date(
|
||||
this.instance.expires.getTime() -
|
||||
this.instance.expires.getTimezoneOffset() * 60000,
|
||||
)
|
||||
: new Date(now.getTime() + 30 * 60000 - now.getTimezoneOffset() * 60000);
|
||||
? new Date(this.instance.expires.getTime())
|
||||
: new Date(now.getTime() + 30 * 60000);
|
||||
|
||||
return html` <ak-form-element-horizontal
|
||||
label=${msg("Identifier")}
|
||||
@ -73,8 +71,8 @@ export class UserTokenForm extends ModelForm<Token, string> {
|
||||
? html`<ak-form-element-horizontal label=${msg("Expiring")} name="expires">
|
||||
<input
|
||||
type="datetime-local"
|
||||
value="${expiringDate.toISOString().slice(0, -8)}"
|
||||
min="${now.toISOString().slice(0, -8)}"
|
||||
value="${dateTimeLocal(expiringDate)}"
|
||||
min="${dateTimeLocal(now)}"
|
||||
class="pf-c-form-control"
|
||||
/>
|
||||
</ak-form-element-horizontal>`
|
||||
|
Reference in New Issue
Block a user