|
1 | 1 | import { Authenticator, AuthenticatorEvents } from "../../auth"; |
2 | | -import { LightDMUser } from "nody-greeter-types"; |
| 2 | +import { LightDMUser, ThemeUtils } from "nody-greeter-types"; |
3 | 3 | import { UIScreen, UILockScreenElements } from "../screen"; |
4 | 4 | import { UI } from "../../ui"; |
5 | 5 |
|
| 6 | +const PATH_LOCK_TIMESTAMP_PREFIX = '/tmp/codam_web_greeter_lock_timestamp'; |
| 7 | + |
6 | 8 | export class LockScreenUI extends UIScreen { |
7 | 9 | public readonly _form: UILockScreenElements; |
8 | 10 | private readonly _activeSession: LightDMUser; |
9 | 11 | private _isExamMode: boolean = false; |
10 | | - private _lockedTime: Date = new Date(); |
| 12 | + private _lockedTime: Date | null = null; |
11 | 13 | protected _events: AuthenticatorEvents = { |
12 | 14 | authenticationStart: () => { |
13 | 15 | this._disableForm(); |
@@ -43,6 +45,10 @@ export class LockScreenUI extends UIScreen { |
43 | 45 | } as UILockScreenElements; |
44 | 46 |
|
45 | 47 | this._initForm(); |
| 48 | + |
| 49 | + // Check when the screen was locked every minute (delete the lock_timestamp file in /tmp to prevent the automated logout) |
| 50 | + setInterval(this._getAndSetLockedTimestamp.bind(this), 60000); |
| 51 | + this._getAndSetLockedTimestamp(); |
46 | 52 | } |
47 | 53 |
|
48 | 54 | protected _initForm(): void { |
@@ -122,16 +128,53 @@ export class LockScreenUI extends UIScreen { |
122 | 128 | return (this._form as UILockScreenElements).passwordInput; |
123 | 129 | } |
124 | 130 |
|
| 131 | + public get lockedTime(): Date | null { |
| 132 | + return this._lockedTime; |
| 133 | + } |
| 134 | + |
| 135 | + private _getScreenLockedTimestamp(login: string): Promise<Date> { |
| 136 | + return new Promise((resolve, reject) => { |
| 137 | + fetch(`${PATH_LOCK_TIMESTAMP_PREFIX}_${login}`) |
| 138 | + .then(response => response.text()) |
| 139 | + .then(text => { |
| 140 | + // Get the first word from the text file |
| 141 | + const timestamp = text.split(' ')[0]; |
| 142 | + resolve(new Date(parseInt(timestamp) * 1000)); |
| 143 | + }) |
| 144 | + .catch(() => { |
| 145 | + reject(); |
| 146 | + }); |
| 147 | + }); |
| 148 | + } |
| 149 | + |
| 150 | + private _getAndSetLockedTimestamp(): void { |
| 151 | + this._getScreenLockedTimestamp(this._activeSession.username) |
| 152 | + .then((timestamp: Date) => { |
| 153 | + this._lockedTime = timestamp; |
| 154 | + this._lockedTimer(); // run once immediately, after this the interval will take care of updating the timer |
| 155 | + }) |
| 156 | + .catch(() => { |
| 157 | + // Unable to get the screen locked timestamp, prevent automated logout by setting the locked time to null |
| 158 | + this._lockedTime = null; |
| 159 | + }); |
| 160 | + } |
| 161 | + |
125 | 162 | private _lockedTimer(): void { |
| 163 | + if (!this._lockedTime) { |
| 164 | + // Unsure when the screen was locked, no automated logout possible |
| 165 | + return; |
| 166 | + } |
| 167 | + |
126 | 168 | const logoutAfter = 42; // minutes |
127 | 169 | const lockedMinutesAgo = (Date.now() - this._lockedTime.getTime()) / 1000 / 60; |
128 | 170 | const timeRemaining = logoutAfter - lockedMinutesAgo; |
129 | 171 | if (timeRemaining <= 0.25) { |
130 | 172 | this._disableForm(); |
131 | 173 | this._form.lockedTimeAgo.innerText = "Automated logout in progress..."; |
132 | | - if (timeRemaining < -5) { |
| 174 | + if (timeRemaining < -5) { // Give it a 5 minute grace period |
133 | 175 | // Add debug text indicating the systemd service might have failed or was not installed |
134 | 176 | window.ui.setDebugInfo("Automated logout appears to take a while. Is the systemd idling service from codam-web-greeter installed and enabled?"); |
| 177 | + this._enableForm(); // Allow the user to just unlock the screen again |
135 | 178 | } |
136 | 179 | } |
137 | 180 | else { |
|
0 commit comments