aboutsummaryrefslogtreecommitdiffstats
path: root/core/src/components/login/PasswordLessLoginForm.vue
blob: bc4d25bf70fffbc17327229588ca465c226771fd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<!--
 - SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
 - SPDX-License-Identifier: AGPL-3.0-or-later
-->
<template>
	<form v-if="(isHttps || isLocalhost) && supportsWebauthn"
		ref="loginForm"
		aria-labelledby="password-less-login-form-title"
		class="password-less-login-form"
		method="post"
		name="login"
		@submit.prevent="submit">
		<h2 id="password-less-login-form-title">
			{{ t('core', 'Log in with a device') }}
		</h2>

		<NcTextField required
			:value="user"
			:autocomplete="autoCompleteAllowed ? 'on' : 'off'"
			:error="!validCredentials"
			:label="t('core', 'Login or email')"
			:placeholder="t('core', 'Login or email')"
			:helper-text="!validCredentials ? t('core', 'Your account is not setup for passwordless login.') : ''"
			@update:value="changeUsername" />

		<LoginButton v-if="validCredentials"
			:loading="loading"
			@click="authenticate" />
	</form>

	<NcEmptyContent v-else-if="!isHttps && !isLocalhost"
		:name="t('core', 'Your connection is not secure')"
		:description="t('core', 'Passwordless authentication is only available over a secure connection.')">
		<template #icon>
			<LockOpenIcon />
		</template>
	</NcEmptyContent>

	<NcEmptyContent v-else
		:name="t('core', 'Browser not supported')"
		:description="t('core', 'Passwordless authentication is not supported in your browser.')">
		<template #icon>
			<InformationIcon />
		</template>
	</NcEmptyContent>
</template>

<script type="ts">
import { browserSupportsWebAuthn } from '@simplewebauthn/browser'
import { defineComponent } from 'vue'
import {
	NoValidCredentials,
	startAuthentication,
	finishAuthentication,
} from '../../services/WebAuthnAuthenticationService.ts'

import NcEmptyContent from '@nextcloud/vue/components/NcEmptyContent'
import NcTextField from '@nextcloud/vue/components/NcTextField'

import InformationIcon from 'vue-material-design-icons/InformationOutline.vue'
import LoginButton from './LoginButton.vue'
import LockOpenIcon from 'vue-material-design-icons/LockOpen.vue'
import logger from '../../logger'

export default defineComponent({
	name: 'PasswordLessLoginForm',
	components: {
		LoginButton,
		InformationIcon,
		LockOpenIcon,
		NcEmptyContent,
		NcTextField,
	},
	props: {
		username: {
			type: String,
			default: '',
		},
		redirectUrl: {
			type: [String, Boolean],
			default: false,
		},
		autoCompleteAllowed: {
			type: Boolean,
			default: true,
		},
		isHttps: {
			type: Boolean,
			default: false,
		},
		isLocalhost: {
			type: Boolean,
			default: false,
		},
	},

	setup() {
		return {
			supportsWebauthn: browserSupportsWebAuthn(),
		}
	},

	data() {
		return {
			user: this.username,
			loading: false,
			validCredentials: true,
		}
	},
	methods: {
		async authenticate() {
			// check required fields
			if (!this.$refs.loginForm.checkValidity()) {
				return
			}

			console.debug('passwordless login initiated')

			try {
				const params = await startAuthentication(this.user)
				await this.completeAuthentication(params)
			} catch (error) {
				if (error instanceof NoValidCredentials) {
					this.validCredentials = false
					return
				}
				logger.debug(error)
			}
		},
		changeUsername(username) {
			this.user = username
			this.$emit('update:username', this.user)
		},
		completeAuthentication(challenge) {
			const redirectUrl = this.redirectUrl

			return finishAuthentication(challenge)
				.then(({ defaultRedirectUrl }) => {
					console.debug('Logged in redirecting')
					// Redirect url might be false so || should be used instead of ??.
					window.location.href = redirectUrl || defaultRedirectUrl
				})
				.catch(error => {
					console.debug('GOT AN ERROR WHILE SUBMITTING CHALLENGE!')
					console.debug(error) // Example: timeout, interaction refused...
				})
		},
		submit() {
			// noop
		},
	},
})
</script>

<style lang="scss" scoped>
.password-less-login-form {
	display: flex;
	flex-direction: column;
	gap: 0.5rem;
	margin: 0;
}
</style>