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

Localize expiration timestamps for device assurance grace period #3703

Merged
merged 7 commits into from
Sep 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ module.exports = {
'^LoginRouter$': `${ROOT}/src/LoginRouter`,
},
setupFiles: [
'<rootDir>/test/unit/jest/jest-setup-global.js'
'<rootDir>/test/unit/jest/jest-setup.js'
],
globalSetup: '<rootDir>/test/unit/jest/jest-global-setup.js',
testMatch: [
'**/test/unit/spec/**/*.{js,ts}'
],
Expand Down
4 changes: 1 addition & 3 deletions packages/@okta/i18n/src/properties/login.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1656,9 +1656,7 @@ idx.error.code.access_denied.device_assurance.remediation.android.zero.trust.and

# Device Assurance Grace Period
idx.device_assurance.grace_period.title = Device assurance reminder
# {0} is the number of days until the grace period expires
idx.device_assurance.grace_period.warning.title.due_by_days = Your device doesn't meet the security requirements. Fix the issue within {0} days to prevent lockout.
Comment on lines -1659 to -1660
Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are consolidating to a single string for the expiration message. This can get deleted in the next translation run

# {0} is the date that the grace period expires
# {0} is the date that the grace period expires, in Okta short-with-timezone format (e.g. 11/29/2021, 4:15 PM EDT)
idx.device_assurance.grace_period.warning.title.due_by_date = Your device doesn't meet the security requirements. Fix the issue by {0} to prevent lockout.
idx.device_assurance.grace_period.continue_to_app = Continue to app

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,6 @@ idx.error.code.access_denied.device_assurance.remediation.android.zero.trust.uns
idx.error.code.access_denied.device_assurance.remediation.android.zero.trust.android_device_policy_app_required = 》Îñšţåļļ ţĥé Åñðŕöîð Ðéṽîçé Þöļîçý åþþ öñ ţĥîš ðéṽîçé €홝한Ӝฐโ⾼ئ䀕ヸ€홝한Ӝฐโ《
idx.error.code.access_denied.device_assurance.remediation.android.zero.trust.android_device_policy_app_required_manual_install = 》Ĝö ţö <$1>Šéţţîñĝš îñ Öķţå Ṽéŕîƒý</$1> åñð ƒöļļöŵ ţĥé îñšţŕûçţîöñš ţö îñšţåļļ ţĥé Åñðŕöîð Ðéṽîçé Þöļîçý åþþ โ⾼ئ䀕ヸ€홝한Ӝฐโ⾼ئ䀕ヸ€홝한Ӝฐโ《
idx.device_assurance.grace_period.title = 》Ðéṽîçé åššûŕåñçé ŕéɱîñðéŕ 홝한Ӝฐโ⾼ئ䀕ヸ€홝한Ӝฐโ《
idx.device_assurance.grace_period.warning.title.due_by_days = 》Ýöûŕ ðéṽîçé ðöéšñ´ţ ɱééţ ţĥé šéçûŕîţý ŕéǫûîŕéɱéñţš· Ƒîẋ ţĥé îššûé ŵîţĥîñ 한Ӝฐโ⾼ئ䀕ヸ€홝한Ӝฐโ {0} ðåýš ţö þŕéṽéñţ ļöçķöûţ· 홝한Ӝฐโ《
idx.device_assurance.grace_period.warning.title.due_by_date = 》Ýöûŕ ðéṽîçé ðöéšñ´ţ ɱééţ ţĥé šéçûŕîţý ŕéǫûîŕéɱéñţš· Ƒîẋ ţĥé îššûé ƀý 한Ӝฐโ⾼ئ䀕ヸ€홝한Ӝฐโ {0} ţö þŕéṽéñţ ļöçķöûţ· 한Ӝฐโ《
idx.device_assurance.grace_period.continue_to_app = 》Çöñţîñûé ţö åþþ ฐโ⾼ئ䀕ヸ€홝한Ӝฐโ《
api.policy.okta.account.management.insufficient.authenticators.unable.to.sign.in = 》Ûñåƀļé ţö šîĝñ îñ· Çöñţåçţ šûþþöŕţ ƒöŕ åššîšţåñçé· ⾼ئ䀕ヸ€홝한Ӝฐโ⾼ئ䀕ヸ€홝한Ӝฐโ《
Original file line number Diff line number Diff line change
Expand Up @@ -1248,7 +1248,6 @@ idx.error.code.access_denied.device_assurance.remediation.android.zero.trust.uns
idx.error.code.access_denied.device_assurance.remediation.android.zero.trust.android_device_policy_app_required = [[okta-signin-widget:login: idx.error.code.access_denied.device_assurance.remediation.android.zero.trust.android_device_policy_app_required]]
idx.error.code.access_denied.device_assurance.remediation.android.zero.trust.android_device_policy_app_required_manual_install = [[okta-signin-widget:login: idx.error.code.access_denied.device_assurance.remediation.android.zero.trust.android_device_policy_app_required_manual_install]]
idx.device_assurance.grace_period.title = [[okta-signin-widget:login: idx.device_assurance.grace_period.title]]
idx.device_assurance.grace_period.warning.title.due_by_days = [[okta-signin-widget:login: idx.device_assurance.grace_period.warning.title.due_by_days]]
idx.device_assurance.grace_period.warning.title.due_by_date = [[okta-signin-widget:login: idx.device_assurance.grace_period.warning.title.due_by_date]]
idx.device_assurance.grace_period.continue_to_app = [[okta-signin-widget:login: idx.device_assurance.grace_period.continue_to_app]]
api.policy.okta.account.management.insufficient.authenticators.unable.to.sign.in = [[okta-signin-widget:login: api.policy.okta.account.management.insufficient.authenticators.unable.to.sign.in]]

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"type": "array",
"value": [
{
"message": "Your device doesn't meet the security requirements. Fix the issue by 08/01/2024 to prevent lockout.",
"message": "Your device doesn't meet the security requirements. Fix the issue by 09/05/2024, 12:00 AM UTC to prevent lockout.",
"i18n": {
"key": "idx.device_assurance.grace_period.warning.title.due_by_date",
"params": [
"08/01/2024"
"2024-09-05T00:00:00.000Z"
]
},
"class": "WARNING"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"type": "array",
"value": [
{
"message": "Your device doesn't meet the security requirements. Fix the issue within 7 days to prevent lockout.",
"message": "Your device doesn't meet the security requirements. Fix the issue by 09/05/2024, 12:00 AM UTC to prevent lockout.",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update: we will send a localized string that specifies UTC as the timezone for this message field to still provide a semantically correct expiry string from the server. This will cover cases where we might have a L10N error and client-side loc() fails.

E.g. For this specific mock, the server will send an ISO8601 format string of "2024-09-05T00:00:00.000Z" as the param and the message field will be that same string localized to UTC rather than the client's timezone -> "09/05/2024, 12:00 AM UTC"

Copy link
Contributor

Choose a reason for hiding this comment

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

This is a good thought to have the fallback, although does the code currently handle if a call to loc returns L10N_ERROR well?

Copy link
Contributor Author

@trevoring-okta trevoring-okta Sep 4, 2024

Choose a reason for hiding this comment

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

You're right, I just checked and it will still render L10N_ERROR as the string. The main case I'm looking to add coverage for is something going wrong in the TimeUtil function when creating the localized expiry param. I'll add a check to use the fallback message if the param comes back falsy because of an invalid Date object.

Digging into this more, I don't think this fallback message with the server-side localized string will ever actually get rendered to the SIW because the generic transformer that localizes all IDX transaction messages will always run before my grace period code is hit, so the actual fallback string users might see is "Your device doesn't meet the security requirements. Fix the issue by 2024-09-05T00:00:00.000Z to prevent lockout." with the ISO8601 string as the parameter before it's processed into the ideal format via TimeUtil. Regardless, I think having this be the server-side message in the IDX payload is better than what I originally had, which was a Unix timestamp

"i18n": {
"key": "idx.device_assurance.grace_period.warning.title.due_by_days",
"key": "idx.device_assurance.grace_period.warning.title.due_by_date",
"params": [
7
"2024-09-05T00:00:00.000Z"
]
},
"class": "WARNING"
Expand Down
33 changes: 33 additions & 0 deletions src/util/TimeUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,37 @@ export default {
unit: convertMomentUnits(highestUnit),
};
},

/**
* @method formatDateToDeviceAssuranceGracePeriodExpiryLocaleString
* Conversion from a Date object to a locale string that mimics Okta's `short-with-timezone` format
* but rounded down to the nearest hour
* e.g. new Date(2024-09-05T00:00:00.000Z) -> 09/05/2024, 8:00 PM EDT
*
* @param {Date} date The Date object for the grace period expiry
* @param {LanguageCode} languageCode The user's language code / locale
* @return {string} The formatted `short-with-timezone` local string
*/
formatDateToDeviceAssuranceGracePeriodExpiryLocaleString: (date, languageCode) => {
try {
// Invalid Date objects will return NaN for valueOf()
if (date && !isNaN(date.valueOf()) && languageCode !== null) {
// Round down the date to the nearest hour
date.setMinutes(0, 0, 0);
return date.toLocaleString(languageCode, {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
timeZoneName: 'short',
});
} else {
return null;
}
} catch (e) {
// If `languageCode` isn't in a valid format `toLocaleString()` will throw a `RangeError`
return null;
}
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ const DeviceAssuranceGracePeriodView = BaseForm.extend(

showMessages() {
const messages = this.options.appState.get('messages').value;
const languageCode = this.settings.get('languageCode');
if (messages) {
this.add(createCallout({
content: new EndUserRemediationMessages({ messages }),
content: new EndUserRemediationMessages({ messages, languageCode }),
type: 'warning',
}));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { View } from '@okta/courage';
import hbs from '@okta/handlebars-inline-precompile';
import { getMessage } from '../../../ion/i18nTransformer';
import TimeUtil from 'util/TimeUtil';
import { loc } from 'util/loc';

const I18N_ACCESS_DENIED_KEY_PREFIX = 'idx.error.code.access_denied.device_assurance.remediation';
const I18N_GRACE_PERIOD_KEY_PREFIX = 'idx.device_assurance.grace_period.warning';
Expand Down Expand Up @@ -72,18 +74,31 @@ export default View.extend({
let explanation = null;
let useCustomHelpText = false;

messages.forEach((message) => {
if (message.i18n.key === ACCESS_DENIED_TITLE_KEY || message.i18n.key.startsWith(GRACE_PERIOD_TITLE_KEY)) {
title = getMessage(message);
} else if (message.i18n.key.startsWith(ACCESS_DENIED_EXPLANATION_KEY_PREFIX)) {
explanation = getMessage(message);
} else if (message.i18n.key.startsWith(HELP_AND_CONTACT_KEY_PREFIX)) {
useCustomHelpText = message.i18n.key === CUSTOM_URL_ADDITIONAL_HELP_KEY;
if (message.links && message.links[0] && message.links[0].url) {
this.additionalHelpUrl = message.links[0].url;
// eslint-disable-next-line complexity
messages.forEach((msg) => {
const { i18n: { key, params = [] }, links, message } = msg;

if (key === ACCESS_DENIED_TITLE_KEY) {
title = getMessage(msg);
} else if (key.startsWith(GRACE_PERIOD_TITLE_KEY)) {
if (params.length > 0) {
const expiry = params[0];
const expiryDate = new Date(expiry);
const localizedExpiry = TimeUtil.formatDateToDeviceAssuranceGracePeriodExpiryLocaleString(
expiryDate,
this.options.languageCode
);
title = localizedExpiry ? loc(key, 'login', [localizedExpiry]) : message;
}
} else if (key.startsWith(ACCESS_DENIED_EXPLANATION_KEY_PREFIX)) {
explanation = getMessage(msg);
} else if (key.startsWith(HELP_AND_CONTACT_KEY_PREFIX)) {
useCustomHelpText = key === CUSTOM_URL_ADDITIONAL_HELP_KEY;
if (links && links[0] && links[0].url) {
this.additionalHelpUrl = links[0].url;
}
} else {
remediationOptions.push(buildRemediationOptionBlockMessage(message));
remediationOptions.push(buildRemediationOptionBlockMessage(msg));
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Object {
},
],
"listStyleType": "disc",
"title": "idx.device_assurance.grace_period.warning.title.due_by_days",
"title": "idx.device_assurance.grace_period.warning.title.due_by_date",
},
Object {
"listStyleType": "disc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ describe('transformDeviceAssuranceGracePeriod Tests', () => {
beforeEach(() => {
transaction.messages = [
{
message: 'Your device doesn\'t meet the security requirements. Fix the issue within 7 days to prevent lockout.',
message: 'Your device doesn\'t meet the security requirements. Fix the issue by 09/05/2024, 12:00 AM UTC to prevent lockout.',
i18n: {
key: 'idx.device_assurance.grace_period.warning.title.due_by_days',
key: 'idx.device_assurance.grace_period.warning.title.due_by_date',
params: [
7,
'2024-09-05T00:00:00.000Z',
],
},
class: 'ERROR',
Expand Down Expand Up @@ -70,7 +70,7 @@ describe('transformDeviceAssuranceGracePeriod Tests', () => {
formBag,
widgetProps,
});
const remediationMessages = buildEndUserRemediationMessages(transaction.messages!);
const remediationMessages = buildEndUserRemediationMessages(transaction.messages!, 'en');

expect(updatedFormBag.uischema.elements.length).toBe(3);
expect(updatedFormBag).toMatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@ import {
InfoboxElement,
TitleElement,
} from '../../../types';
import { buildEndUserRemediationMessages, loc } from '../../../util';
import { buildEndUserRemediationMessages, getLanguageCode, loc } from '../../../util';

export const transformDeviceAssuranceGracePeriod: IdxStepTransformer = ({
formBag,
transaction,
widgetProps,
}) => {
const { uischema } = formBag;
const { messages = [] } = transaction;
const languageCode = getLanguageCode(widgetProps);

// Normally, the transactionMessageTransformer runs after this transformer, but buildEndUserRemediationMessages()
// expects localized transaction messages so we have to call this transformer here.
transactionMessageTransformer(transaction);

const remediationMessages = buildEndUserRemediationMessages(messages);
const remediationMessages = buildEndUserRemediationMessages(messages, languageCode);

const titleElement: TitleElement = {
type: 'Title',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ export const transformOktaVerifyEnrollPoll: IdxStepTransformer = ({

const stepper: StepperLayout = {
type: UISchemaLayoutType.STEPPER,
key: 'stepper_' + channelType,
key: `stepper_${channelType}`,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was pulled in as an automatic lint fix. I'll leave it here but it's unrelated to my actual code changes

elements: [
// QR code
{
Expand Down
Loading