@tomei/sso 0.65.0 → 0.65.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.gitlab-ci.yml +40 -0
- package/.husky/commit-msg +0 -0
- package/.husky/pre-commit +0 -0
- package/__tests__/unit/components/login-user/password-expiry.spec.ts +87 -0
- package/dist/__tests__/unit/components/login-user/password-expiry.spec.d.ts +1 -0
- package/dist/__tests__/unit/components/login-user/password-expiry.spec.js +86 -0
- package/dist/__tests__/unit/components/login-user/password-expiry.spec.js.map +1 -0
- package/dist/src/components/login-user/user.d.ts +5 -0
- package/dist/src/components/login-user/user.js +44 -0
- package/dist/src/components/login-user/user.js.map +1 -1
- package/dist/src/components/user-password-history/user-password-history.d.ts +1 -0
- package/dist/src/components/user-password-history/user-password-history.js +41 -2
- package/dist/src/components/user-password-history/user-password-history.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/components/login-user/user.ts +78 -0
- package/src/components/user-password-history/user-password-history.ts +57 -6
package/package.json
CHANGED
|
@@ -2988,6 +2988,7 @@ export class User extends UserBase {
|
|
|
2988
2988
|
|
|
2989
2989
|
this.Password = passwordHash;
|
|
2990
2990
|
this.LastPasswordChangedAt = new Date();
|
|
2991
|
+
this.NeedToChangePasswordYN = YN.No;
|
|
2991
2992
|
|
|
2992
2993
|
const entityValueAfter: IUserAttr = {
|
|
2993
2994
|
UserId: this.UserId,
|
|
@@ -3022,6 +3023,7 @@ export class User extends UserBase {
|
|
|
3022
3023
|
{
|
|
3023
3024
|
Password: this.Password,
|
|
3024
3025
|
LastPasswordChangedAt: this.LastPasswordChangedAt,
|
|
3026
|
+
NeedToChangePasswordYN: this.NeedToChangePasswordYN,
|
|
3025
3027
|
},
|
|
3026
3028
|
{
|
|
3027
3029
|
where: {
|
|
@@ -3053,6 +3055,82 @@ export class User extends UserBase {
|
|
|
3053
3055
|
}
|
|
3054
3056
|
}
|
|
3055
3057
|
|
|
3058
|
+
// PASSWORD_EXPIRY_MONTHS=0 disables the policy entirely (AC#1) - checked before NeedToChangePasswordYN/history.
|
|
3059
|
+
async getPasswordExpiryStatus(dbTransaction?: any): Promise<{
|
|
3060
|
+
expired: boolean;
|
|
3061
|
+
expiringSoon: boolean;
|
|
3062
|
+
expiresInDays: number | null;
|
|
3063
|
+
}> {
|
|
3064
|
+
try {
|
|
3065
|
+
const expiryMonthsRaw = Number(process.env.PASSWORD_EXPIRY_MONTHS);
|
|
3066
|
+
const expiryMonths = Number.isFinite(expiryMonthsRaw)
|
|
3067
|
+
? Math.max(0, Math.floor(expiryMonthsRaw))
|
|
3068
|
+
: 0;
|
|
3069
|
+
|
|
3070
|
+
if (expiryMonths === 0) {
|
|
3071
|
+
return { expired: false, expiringSoon: false, expiresInDays: null };
|
|
3072
|
+
}
|
|
3073
|
+
|
|
3074
|
+
// Accounts authenticated without a local password cannot rotate one here.
|
|
3075
|
+
if (!this.Password) {
|
|
3076
|
+
return { expired: false, expiringSoon: false, expiresInDays: null };
|
|
3077
|
+
}
|
|
3078
|
+
|
|
3079
|
+
// Forces first-login change for temp-password/admin-created accounts (AC#4), regardless of password age.
|
|
3080
|
+
if (this.NeedToChangePasswordYN === YN.Yes) {
|
|
3081
|
+
return { expired: true, expiringSoon: false, expiresInDays: null };
|
|
3082
|
+
}
|
|
3083
|
+
|
|
3084
|
+
const warningDaysRaw = Number(process.env.PASSWORD_EXPIRY_WARNING_DAYS);
|
|
3085
|
+
const warningDays = Number.isFinite(warningDaysRaw)
|
|
3086
|
+
? Math.max(0, Math.floor(warningDaysRaw))
|
|
3087
|
+
: 14;
|
|
3088
|
+
|
|
3089
|
+
const latestHistory = await UserPasswordHistory.getLatestByUserId(
|
|
3090
|
+
dbTransaction,
|
|
3091
|
+
this.UserId,
|
|
3092
|
+
);
|
|
3093
|
+
|
|
3094
|
+
if (!latestHistory) {
|
|
3095
|
+
// Legacy/pre-existing accounts without a history row.
|
|
3096
|
+
return { expired: true, expiringSoon: false, expiresInDays: null };
|
|
3097
|
+
}
|
|
3098
|
+
|
|
3099
|
+
const createdAt = latestHistory.CreatedAt;
|
|
3100
|
+
const expiryMonth = createdAt.getUTCMonth() + expiryMonths;
|
|
3101
|
+
const lastDayOfExpiryMonth = new Date(
|
|
3102
|
+
Date.UTC(createdAt.getUTCFullYear(), expiryMonth + 1, 0),
|
|
3103
|
+
).getUTCDate();
|
|
3104
|
+
const expiryDate = new Date(
|
|
3105
|
+
Date.UTC(
|
|
3106
|
+
createdAt.getUTCFullYear(),
|
|
3107
|
+
expiryMonth,
|
|
3108
|
+
Math.min(createdAt.getUTCDate(), lastDayOfExpiryMonth),
|
|
3109
|
+
createdAt.getUTCHours(),
|
|
3110
|
+
createdAt.getUTCMinutes(),
|
|
3111
|
+
createdAt.getUTCSeconds(),
|
|
3112
|
+
createdAt.getUTCMilliseconds(),
|
|
3113
|
+
),
|
|
3114
|
+
);
|
|
3115
|
+
|
|
3116
|
+
const now = new Date();
|
|
3117
|
+
if (now.getTime() >= expiryDate.getTime()) {
|
|
3118
|
+
return { expired: true, expiringSoon: false, expiresInDays: null };
|
|
3119
|
+
}
|
|
3120
|
+
|
|
3121
|
+
const msPerDay = 24 * 60 * 60 * 1000;
|
|
3122
|
+
const expiringSoon =
|
|
3123
|
+
now.getTime() >= expiryDate.getTime() - warningDays * msPerDay;
|
|
3124
|
+
const expiresInDays = Math.ceil(
|
|
3125
|
+
(expiryDate.getTime() - now.getTime()) / msPerDay,
|
|
3126
|
+
);
|
|
3127
|
+
|
|
3128
|
+
return { expired: false, expiringSoon, expiresInDays };
|
|
3129
|
+
} catch (error) {
|
|
3130
|
+
throw error;
|
|
3131
|
+
}
|
|
3132
|
+
}
|
|
3133
|
+
|
|
3056
3134
|
async verifyPasscode(passcode: string) {
|
|
3057
3135
|
try {
|
|
3058
3136
|
// Return error if this._Userid is empty "User is not created yet."
|
|
@@ -76,16 +76,30 @@ export class UserPasswordHistory
|
|
|
76
76
|
): Promise<void> {
|
|
77
77
|
// This method used to check if password entered is valid by checking previous password history
|
|
78
78
|
try {
|
|
79
|
-
// Part 1-2: Retrieve password history policy
|
|
79
|
+
// Part 1-2: Retrieve password history policy: env var takes precedence over component config, call ComponentConfig. by passing:
|
|
80
80
|
// - ComponentName: "@tomei/sso"
|
|
81
81
|
// - ConfigKey: "passwordHistory"
|
|
82
82
|
// If no password history set, use default value 3
|
|
83
83
|
|
|
84
|
-
const
|
|
84
|
+
const envPasswordHistoryPolicy = Number(
|
|
85
|
+
process.env.PASSWORD_HISTORY_LIMIT,
|
|
86
|
+
);
|
|
87
|
+
const componentPasswordHistoryPolicy = Number(
|
|
85
88
|
ComponentConfig.getComponentConfigValue(
|
|
86
89
|
'@tomei/sso',
|
|
87
90
|
'passwordHistory',
|
|
88
|
-
)
|
|
91
|
+
),
|
|
92
|
+
);
|
|
93
|
+
const passwordHistoryPolicy =
|
|
94
|
+
(Number.isFinite(envPasswordHistoryPolicy) &&
|
|
95
|
+
envPasswordHistoryPolicy > 0
|
|
96
|
+
? Math.floor(envPasswordHistoryPolicy)
|
|
97
|
+
: 0) ||
|
|
98
|
+
(Number.isFinite(componentPasswordHistoryPolicy) &&
|
|
99
|
+
componentPasswordHistoryPolicy > 0
|
|
100
|
+
? Math.floor(componentPasswordHistoryPolicy)
|
|
101
|
+
: 0) ||
|
|
102
|
+
3;
|
|
89
103
|
|
|
90
104
|
// Part 3-4: Retrieve records from the table by using class._repo findAll() by passing:
|
|
91
105
|
// where: { UserId: params.UserId }
|
|
@@ -137,16 +151,30 @@ export class UserPasswordHistory
|
|
|
137
151
|
): Promise<void> {
|
|
138
152
|
// This method used to check if password entered is valid by checking previous password history
|
|
139
153
|
try {
|
|
140
|
-
// Part 1-2: Retrieve password history policy
|
|
154
|
+
// Part 1-2: Retrieve password history policy: env var takes precedence over component config, call ComponentConfig. by passing:
|
|
141
155
|
// - ComponentName: "@tomei/sso"
|
|
142
156
|
// - ConfigKey: "passwordHistory"
|
|
143
157
|
// If no password history set, use default value 3
|
|
144
158
|
|
|
145
|
-
const
|
|
159
|
+
const envPasswordHistoryPolicy = Number(
|
|
160
|
+
process.env.PASSWORD_HISTORY_LIMIT,
|
|
161
|
+
);
|
|
162
|
+
const componentPasswordHistoryPolicy = Number(
|
|
146
163
|
ComponentConfig.getComponentConfigValue(
|
|
147
164
|
'@tomei/sso',
|
|
148
165
|
'passwordHistory',
|
|
149
|
-
)
|
|
166
|
+
),
|
|
167
|
+
);
|
|
168
|
+
const passwordHistoryPolicy =
|
|
169
|
+
(Number.isFinite(envPasswordHistoryPolicy) &&
|
|
170
|
+
envPasswordHistoryPolicy > 0
|
|
171
|
+
? Math.floor(envPasswordHistoryPolicy)
|
|
172
|
+
: 0) ||
|
|
173
|
+
(Number.isFinite(componentPasswordHistoryPolicy) &&
|
|
174
|
+
componentPasswordHistoryPolicy > 0
|
|
175
|
+
? Math.floor(componentPasswordHistoryPolicy)
|
|
176
|
+
: 0) ||
|
|
177
|
+
3;
|
|
150
178
|
|
|
151
179
|
// Part 3: Insert new password history by calling class _repo create() method.
|
|
152
180
|
const userPasswordHistory = new UserPasswordHistory();
|
|
@@ -184,4 +212,27 @@ export class UserPasswordHistory
|
|
|
184
212
|
throw error;
|
|
185
213
|
}
|
|
186
214
|
}
|
|
215
|
+
|
|
216
|
+
// Latest history row's CreatedAt is the source of truth for password age (used by User.getPasswordExpiryStatus()).
|
|
217
|
+
public static async getLatestByUserId(
|
|
218
|
+
dbTransaction: any,
|
|
219
|
+
UserId: number,
|
|
220
|
+
): Promise<UserPasswordHistory | null> {
|
|
221
|
+
try {
|
|
222
|
+
const passwordHistory = await UserPasswordHistory._Repo.findAll({
|
|
223
|
+
where: { UserId: UserId },
|
|
224
|
+
order: [['CreatedAt', 'DESC']],
|
|
225
|
+
limit: 1,
|
|
226
|
+
transaction: dbTransaction,
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
if (!passwordHistory || passwordHistory.length < 1) {
|
|
230
|
+
return null;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return new UserPasswordHistory(passwordHistory[0].get({ plain: true }));
|
|
234
|
+
} catch (error) {
|
|
235
|
+
throw error;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
187
238
|
}
|