Extract lightdm session logic into useLightdm composable
parent
752dfc964c
commit
ea715d5f1e
@ -0,0 +1,116 @@
|
||||
import { computed, ref } from 'vue'
|
||||
import type { LightdmSession, LightdmUsers } from '@/models/lightdm'
|
||||
|
||||
const username = ref('')
|
||||
const password = ref('')
|
||||
const desktop = ref('')
|
||||
const showPassword = ref(false)
|
||||
const users = ref<LightdmUsers[]>([])
|
||||
const desktops = ref<LightdmSession[]>([])
|
||||
const canShutdown = ref(false)
|
||||
const canRestart = ref(false)
|
||||
const canSuspend = ref(false)
|
||||
const canHibernate = ref(false)
|
||||
|
||||
const currentUser = computed(() => users.value.find((u) => u.username === username.value))
|
||||
const currentDesktop = computed(() => desktops.value.find(({ key }) => key === desktop.value))
|
||||
|
||||
let _errorTimer: ReturnType<typeof setTimeout> | null = null
|
||||
|
||||
function _setInputError() {
|
||||
const el = document.getElementById('password')
|
||||
if (!el) return
|
||||
el.classList.add('password-input--error')
|
||||
if (_errorTimer) clearTimeout(_errorTimer)
|
||||
_errorTimer = setTimeout(() => {
|
||||
el.classList.remove('password-input--error')
|
||||
_errorTimer = null
|
||||
}, 10000)
|
||||
}
|
||||
|
||||
function _setupSignals() {
|
||||
window.lightdm?.show_message?.connect((text: string, type: any) => {
|
||||
console.log({ text, type })
|
||||
})
|
||||
|
||||
window.lightdm?.show_prompt?.connect((_message: string, type: number) => {
|
||||
if (!window.lightdm) return
|
||||
if (type === 0) {
|
||||
window.lightdm.respond(username.value)
|
||||
} else if (type === 1 && (window.lightdm as any).in_authentication) {
|
||||
window.lightdm.respond(password.value)
|
||||
}
|
||||
})
|
||||
|
||||
window.lightdm?.authentication_complete?.connect(() => {
|
||||
if (window.lightdm?.is_authenticated) {
|
||||
window.lightdm.start_session(desktop.value || window.lightdm.default_session)
|
||||
} else {
|
||||
window.lightdm?.cancel_authentication()
|
||||
_setInputError()
|
||||
}
|
||||
})
|
||||
|
||||
window.lightdm_cancel_login = () => window.lightdm?.cancel_authentication()
|
||||
window.lightdm_start = (d: string) => window.lightdm?.start_session(d)
|
||||
|
||||
window.show_prompt = (text: string) => {
|
||||
if (text === 'Password: ') window.lightdm?.respond(password.value)
|
||||
}
|
||||
|
||||
window.authentication_complete = () => {
|
||||
if (window.lightdm?.is_authenticated) {
|
||||
window.lightdm_start(desktop.value)
|
||||
} else if (document.head.dataset.wintype === 'primary') {
|
||||
window.lightdm?.cancel_authentication()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function initLightdm() {
|
||||
users.value = (window.lightdm?.users || []) as LightdmUsers[]
|
||||
desktops.value = (window.lightdm?.sessions || []) as LightdmSession[]
|
||||
username.value = users.value[0]?.username || ''
|
||||
desktop.value = window.lightdm?.default_session || desktops.value[0]?.key || ''
|
||||
canShutdown.value = !!window.lightdm?.can_shutdown
|
||||
canRestart.value = !!window.lightdm?.can_restart
|
||||
canSuspend.value = !!window.lightdm?.can_suspend
|
||||
canHibernate.value = !!window.lightdm?.can_hibernate
|
||||
_setupSignals()
|
||||
}
|
||||
|
||||
function login() {
|
||||
window.lightdm?.authenticate(username.value)
|
||||
}
|
||||
|
||||
function toggleShowPassword() {
|
||||
showPassword.value = !showPassword.value
|
||||
}
|
||||
|
||||
function shutdown() { window.lightdm?.shutdown() }
|
||||
function hibernate() { window.lightdm?.hibernate() }
|
||||
function suspend() { window.lightdm?.suspend() }
|
||||
function restart() { window.lightdm?.restart() }
|
||||
|
||||
export function useLightdm() {
|
||||
return {
|
||||
username,
|
||||
password,
|
||||
desktop,
|
||||
showPassword,
|
||||
users,
|
||||
desktops,
|
||||
currentUser,
|
||||
currentDesktop,
|
||||
canShutdown,
|
||||
canRestart,
|
||||
canSuspend,
|
||||
canHibernate,
|
||||
login,
|
||||
toggleShowPassword,
|
||||
shutdown,
|
||||
hibernate,
|
||||
suspend,
|
||||
restart,
|
||||
}
|
||||
}
|
||||
@ -1,182 +0,0 @@
|
||||
const DEBUG_PASSWORD = 'password'
|
||||
const lightdmDebug = window.lightdm === undefined
|
||||
|
||||
function setIsAuthenticated(value: boolean) {
|
||||
;(window.lightdm as any).is_authenticated = value
|
||||
}
|
||||
|
||||
if (lightdmDebug) {
|
||||
window.lightdm = {
|
||||
is_authenticated: false,
|
||||
authentication_user: undefined,
|
||||
default_session: 'plasma-shell',
|
||||
can_suspend: true,
|
||||
can_restart: true,
|
||||
can_hibernate: true,
|
||||
can_shutdown: true,
|
||||
|
||||
authentication_complete: { connect: (callback: () => void) => { console.log('authentication complete') } },
|
||||
|
||||
sessions: [
|
||||
{ name: 'GNOME', key: 'gnome-shell' },
|
||||
{ name: 'KDE', key: 'plasma-shell' },
|
||||
{ name: 'XFCE', key: 'xfce' },
|
||||
{ name: 'Cinnamon', key: 'cinnamon' },
|
||||
{ name: 'i3', key: 'i3' },
|
||||
{ name: 'Openbox', key: 'openbox' },
|
||||
],
|
||||
users: [
|
||||
{
|
||||
display_name: 'Warinyourself',
|
||||
username: 'Warinyourself',
|
||||
image: 'https://avatars.githubusercontent.com/u/83131232?s=200&u=26fbedfe561a2b37225c78c10b1c5d67d6fe1832&v=4'
|
||||
},
|
||||
{ display_name: 'Bob', username: 'Bob' }
|
||||
],
|
||||
languages: [
|
||||
{ name: 'Русский', code: 'ru_RU.utf8' },
|
||||
{ name: 'American English', code: 'en_US.utf8' }
|
||||
],
|
||||
language: { code: 'en_US', name: 'American English' },
|
||||
|
||||
start_authentication: (username: string) => {
|
||||
const inputNode = document.getElementById('password') as HTMLInputElement
|
||||
window.lightdm?.respond(inputNode?.value || '')
|
||||
},
|
||||
authenticate: (username: string) => {
|
||||
const inputNode = document.getElementById('password') as HTMLInputElement
|
||||
if (window.lightdm) (window.lightdm as any).authentication_user = username
|
||||
window.lightdm?.respond(inputNode?.value || '')
|
||||
},
|
||||
cancel_authentication: () => { console.log('Auth cancelled') },
|
||||
start_session(session: string) { alert(`Start session: ${session}`) },
|
||||
respond: (password: string) => {
|
||||
if (password === DEBUG_PASSWORD) {
|
||||
setIsAuthenticated(true)
|
||||
} else {
|
||||
setIsAuthenticated(false)
|
||||
}
|
||||
},
|
||||
shutdown() { alert('(DEBUG: System is shutting down)') },
|
||||
hibernate() { alert('(DEBUG: System is hibernating)') },
|
||||
suspend: () => { alert('(DEBUG: System is suspending)') },
|
||||
restart: () => { alert('(DEBUG: System is rebooting)') }
|
||||
} as any
|
||||
}
|
||||
|
||||
class LightdmWebkit {
|
||||
protected _inputErrorTimer!: null | ReturnType<typeof setTimeout>
|
||||
|
||||
get defaultSession() {
|
||||
return this.sessions[0]?.key || window.lightdm?.default_session || 'i3'
|
||||
}
|
||||
|
||||
get sessions() { return window.lightdm?.sessions || [] }
|
||||
get hasGuestAccount() { return window.lightdm?.has_guest_account }
|
||||
get users() { return window.lightdm?.users || [] }
|
||||
get username() { return this.users[0]?.username || 'Username' }
|
||||
get languages() { return window.lightdm?.languages }
|
||||
get canRestart() { return window.lightdm?.can_restart }
|
||||
get canShutdown() { return window.lightdm?.can_shutdown }
|
||||
get canSuspend() { return window.lightdm?.can_suspend }
|
||||
get canHibernate() { return window.lightdm?.can_hibernate }
|
||||
|
||||
shutdown() { return window.lightdm?.shutdown() }
|
||||
hibernate() { return window.lightdm?.hibernate() }
|
||||
suspend() { return window.lightdm?.suspend() }
|
||||
restart() { return window.lightdm?.restart() }
|
||||
|
||||
protected _setInputError() {
|
||||
const inputNode = document.getElementById('password')
|
||||
if (!inputNode) return
|
||||
|
||||
inputNode.classList.add('password-input--error')
|
||||
if (this._inputErrorTimer) clearTimeout(this._inputErrorTimer)
|
||||
|
||||
this._inputErrorTimer = setTimeout(() => {
|
||||
inputNode.classList.remove('password-input--error')
|
||||
this._inputErrorTimer = null
|
||||
}, 10000)
|
||||
}
|
||||
}
|
||||
|
||||
class LightdmNode extends LightdmWebkit {
|
||||
public _username!: string
|
||||
public _password!: string
|
||||
public _session!: string
|
||||
|
||||
constructor() {
|
||||
super()
|
||||
this.init()
|
||||
}
|
||||
|
||||
public login(username: string, password: string, session?: string): void {
|
||||
this._username = username
|
||||
this._password = password
|
||||
this._session = session || this.defaultSession
|
||||
window.lightdm?.authenticate(username)
|
||||
}
|
||||
|
||||
public setAuthenticationDone(): void {
|
||||
window.lightdm?.authentication_complete?.connect(() => {
|
||||
if (window.lightdm?.is_authenticated) {
|
||||
window.lightdm?.start_session(this._session || window.lightdm?.default_session)
|
||||
} else {
|
||||
this._authenticationFailed()
|
||||
}
|
||||
})
|
||||
|
||||
window.lightdm_cancel_login = () => {
|
||||
window.lightdm?.cancel_authentication()
|
||||
}
|
||||
}
|
||||
|
||||
private _authenticationFailed(): void {
|
||||
this._setInputError()
|
||||
window.lightdm?.cancel_authentication()
|
||||
}
|
||||
|
||||
public setSignalHandler(): void {
|
||||
window.lightdm?.show_message?.connect(function (text, type) {
|
||||
console.log({ text, type })
|
||||
})
|
||||
|
||||
window.lightdm?.show_prompt?.connect((_message, type) => {
|
||||
if (!window.lightdm) return
|
||||
if (type === 0) {
|
||||
window.lightdm.respond(this._username)
|
||||
} else if (type === 1 && window.lightdm.in_authentication) {
|
||||
window.lightdm.respond(this._password)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
public init(): void {
|
||||
this.setSignalHandler()
|
||||
this.setAuthenticationDone()
|
||||
}
|
||||
}
|
||||
|
||||
export const LightdmHandler = new LightdmNode()
|
||||
|
||||
window.lightdm_cancel_login = () => {
|
||||
window.lightdm?.cancel_authentication()
|
||||
}
|
||||
|
||||
window.lightdm_start = (desktop: string) => {
|
||||
window.lightdm?.start_session(desktop)
|
||||
}
|
||||
|
||||
window.show_prompt = (text, type) => {
|
||||
if (text === 'Password: ' && LightdmHandler._password !== undefined) {
|
||||
window.lightdm?.respond(LightdmHandler._password)
|
||||
}
|
||||
}
|
||||
|
||||
window.authentication_complete = () => {
|
||||
if (window.lightdm?.is_authenticated) {
|
||||
window.lightdm_start(LightdmHandler._session)
|
||||
} else if (document.head.dataset.wintype === 'primary') {
|
||||
window.lightdm?.cancel_authentication()
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,55 @@
|
||||
const DEBUG_PASSWORD = 'password'
|
||||
|
||||
if (window.lightdm === undefined) {
|
||||
window.lightdm = {
|
||||
is_authenticated: false,
|
||||
authentication_user: undefined,
|
||||
default_session: 'gnome-shell',
|
||||
can_suspend: true,
|
||||
can_restart: true,
|
||||
can_hibernate: true,
|
||||
can_shutdown: true,
|
||||
|
||||
authentication_complete: { connect: () => {} },
|
||||
show_message: { connect: () => {} },
|
||||
show_prompt: { connect: () => {} },
|
||||
|
||||
sessions: [
|
||||
{ name: 'GNOME', key: 'gnome-shell' },
|
||||
{ name: 'KDE', key: 'plasma-shell' },
|
||||
{ name: 'XFCE', key: 'xfce' },
|
||||
{ name: 'Cinnamon', key: 'cinnamon' },
|
||||
{ name: 'i3', key: 'i3' },
|
||||
{ name: 'Openbox', key: 'openbox' },
|
||||
],
|
||||
users: [
|
||||
{
|
||||
display_name: 'Warinyourself',
|
||||
username: 'Warinyourself',
|
||||
image: 'https://avatars.githubusercontent.com/u/83131232?s=200&u=26fbedfe561a2b37225c78c10b1c5d67d6fe1832&v=4'
|
||||
},
|
||||
{ display_name: 'Bob', username: 'Bob' }
|
||||
],
|
||||
languages: [
|
||||
{ name: 'Русский', code: 'ru_RU.utf8' },
|
||||
{ name: 'American English', code: 'en_US.utf8' }
|
||||
],
|
||||
language: { code: 'en_US', name: 'American English' },
|
||||
|
||||
authenticate: (username: string) => {
|
||||
if (window.lightdm) (window.lightdm as any).authentication_user = username
|
||||
const inputNode = document.getElementById('password') as HTMLInputElement
|
||||
window.lightdm?.respond(inputNode?.value || '')
|
||||
},
|
||||
cancel_authentication: () => { console.log('Auth cancelled') },
|
||||
start_session: (session: string) => { alert(`Start session: ${session}`) },
|
||||
respond: (password: string) => {
|
||||
;(window.lightdm as any).is_authenticated = password === DEBUG_PASSWORD
|
||||
window.authentication_complete?.()
|
||||
},
|
||||
shutdown: () => { alert('(DEBUG: shutdown)') },
|
||||
hibernate: () => { alert('(DEBUG: hibernate)') },
|
||||
suspend: () => { alert('(DEBUG: suspend)') },
|
||||
restart: () => { alert('(DEBUG: restart)') },
|
||||
} as any
|
||||
}
|
||||
Loading…
Reference in New Issue