aboutsummaryrefslogtreecommitdiffstats
path: root/apps/settings/src/components/WebAuthn/AddDevice.vue
blob: db00bae451a3f1ee36f7624540cbd449e858b194 (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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<!--
  - SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  - SPDX-License-Identifier: AGPL-3.0-or-later
-->

<template>
	<div v-if="!isHttps && !isLocalhost">
		{{ t('settings', 'Passwordless authentication requires a secure connection.') }}
	</div>
	<div v-else>
		<NcButton v-if="step === RegistrationSteps.READY"
			type="primary"
			@click="start">
			{{ t('settings', 'Add WebAuthn device') }}
		</NcButton>

		<div v-else-if="step === RegistrationSteps.REGISTRATION"
			class="new-webauthn-device">
			<span class="icon-loading-small webauthn-loading" />
			{{ t('settings', 'Please authorize your WebAuthn device.') }}
		</div>

		<div v-else-if="step === RegistrationSteps.NAMING"
			class="new-webauthn-device">
			<span class="icon-loading-small webauthn-loading" />
			<form @submit.prevent="submit">
				<NcTextField ref="nameInput"
					class="new-webauthn-device__name"
					:label="t('settings', 'Device name')"
					:value.sync="name"
					show-trailing-button
					:trailing-button-label="t('settings', 'Add')"
					trailing-button-icon="arrowRight"
					@trailing-button-click="submit" />
			</form>
		</div>

		<div v-else-if="step === RegistrationSteps.PERSIST"
			class="new-webauthn-device">
			<span class="icon-loading-small webauthn-loading" />
			{{ t('settings', 'Adding your device …') }}
		</div>

		<div v-else>
			Invalid registration step. This should not have happened.
		</div>
	</div>
</template>

<script>
import { showError } from '@nextcloud/dialogs'
import { confirmPassword } from '@nextcloud/password-confirmation'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcTextField from '@nextcloud/vue/components/NcTextField'

import logger from '../../logger.ts'
import {
	startRegistration,
	finishRegistration,
} from '../../service/WebAuthnRegistrationSerice.ts'

import '@nextcloud/password-confirmation/dist/style.css'

const logAndPass = (text) => (data) => {
	logger.debug(text)
	return data
}

const RegistrationSteps = Object.freeze({
	READY: 1,
	REGISTRATION: 2,
	NAMING: 3,
	PERSIST: 4,
})

export default {
	name: 'AddDevice',

	components: {
		NcButton,
		NcTextField,
	},

	props: {
		httpWarning: Boolean,
		isHttps: {
			type: Boolean,
			default: false,
		},
		isLocalhost: {
			type: Boolean,
			default: false,
		},
	},

	setup() {
		// non reactive props
		return {
			RegistrationSteps,
		}
	},

	data() {
		return {
			name: '',
			credential: {},
			step: RegistrationSteps.READY,
		}
	},

	watch: {
		/**
		 * Auto focus the name input when naming a device
		 */
		step() {
			if (this.step === RegistrationSteps.NAMING) {
				this.$nextTick(() => this.$refs.nameInput?.focus())
			}
		},
	},

	methods: {
		/**
		 * Start the registration process by loading the authenticator parameters
		 * The next step is the naming of the device
		 */
		async start() {
			this.step = RegistrationSteps.REGISTRATION
			console.debug('Starting WebAuthn registration')

			try {
				await confirmPassword()
				this.credential = await startRegistration()
				this.step = RegistrationSteps.NAMING
			} catch (err) {
				showError(err)
				this.step = RegistrationSteps.READY
			}
		},

		/**
		 * Save the new device with the given name on the server
		 */
		submit() {
			this.step = RegistrationSteps.PERSIST

			return confirmPassword()
				.then(logAndPass('confirmed password'))
				.then(this.saveRegistrationData)
				.then(logAndPass('registration data saved'))
				.then(() => this.reset())
				.then(logAndPass('app reset'))
				.catch(console.error)
		},

		async saveRegistrationData() {
			try {
				const device = await finishRegistration(this.name, this.credential)

				logger.info('new device added', { device })

				this.$emit('added', device)
			} catch (err) {
				logger.error('Error persisting webauthn registration', { error: err })
				throw new Error(t('settings', 'Server error while trying to complete WebAuthn device registration'))
			}
		},

		reset() {
			this.name = ''
			this.registrationData = {}
			this.step = RegistrationSteps.READY
		},
	},
}
</script>

<style scoped lang="scss">
.webauthn-loading {
	display: inline-block;
	vertical-align: sub;
	margin-inline: 2px;
}

.new-webauthn-device {
	display: flex;
	gap: 22px;
	align-items: center;

	&__name {
		max-width: min(100vw, 400px);
	}
}
</style>