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
|
<!--
- @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
-
- @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
-
- @license GNU AGPL version 3 or any later version
-
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU Affero General Public License as
- published by the Free Software Foundation, either version 3 of the
- License, or (at your option) any later version.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU Affero General Public License for more details.
-
- You should have received a copy of the GNU Affero General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
-->
<template>
<div id="security" class="section">
<h2>{{ t('settings', 'Devices & sessions', {}, undefined, {sanitize: false}) }}</h2>
<p class="settings-hint hidden-when-empty">
{{ t('settings', 'Web, desktop and mobile clients currently logged in to your account.') }}
</p>
<AuthTokenList :tokens="tokens"
@toggleScope="toggleTokenScope"
@rename="rename"
@delete="deleteToken"
@wipe="wipeToken" />
<AuthTokenSetupDialogue v-if="canCreateToken" :add="addNewToken" />
</div>
</template>
<script>
import axios from '@nextcloud/axios'
import confirmPassword from '@nextcloud/password-confirmation'
import { generateUrl } from '@nextcloud/router'
import AuthTokenList from './AuthTokenList'
import AuthTokenSetupDialogue from './AuthTokenSetupDialogue'
const confirm = () => {
return new Promise(resolve => {
OC.dialogs.confirm(
t('settings', 'Do you really want to wipe your data from this device?'),
t('settings', 'Confirm wipe'),
resolve,
true
)
})
}
/**
* Tap into a promise without losing the value
* @param {Function} cb the callback
* @returns {any} val the value
*/
const tap = cb => val => {
cb(val)
return val
}
export default {
name: 'AuthTokenSection',
components: {
AuthTokenSetupDialogue,
AuthTokenList,
},
props: {
tokens: {
type: Array,
required: true,
},
canCreateToken: {
type: Boolean,
required: true,
},
},
data() {
return {
baseUrl: generateUrl('/settings/personal/authtokens'),
}
},
methods: {
addNewToken(name) {
console.debug('creating a new app token', name)
const data = {
name,
}
return axios.post(this.baseUrl, data)
.then(resp => resp.data)
.then(tap(() => console.debug('app token created')))
// eslint-disable-next-line vue/no-mutating-props
.then(tap(data => this.tokens.push(data.deviceToken)))
.catch(err => {
console.error.bind('could not create app password', err)
OC.Notification.showTemporary(t('core', 'Error while creating device token'))
throw err
})
},
toggleTokenScope(token, scope, value) {
console.debug('updating app token scope', token.id, scope, value)
const oldVal = token.scope[scope]
token.scope[scope] = value
return this.updateToken(token)
.then(tap(() => console.debug('app token scope updated')))
.catch(err => {
console.error.bind('could not update app token scope', err)
OC.Notification.showTemporary(t('core', 'Error while updating device token scope'))
// Restore
token.scope[scope] = oldVal
throw err
})
},
rename(token, newName) {
console.debug('renaming app token', token.id, token.name, newName)
const oldName = token.name
token.name = newName
return this.updateToken(token)
.then(tap(() => console.debug('app token name updated')))
.catch(err => {
console.error.bind('could not update app token name', err)
OC.Notification.showTemporary(t('core', 'Error while updating device token name'))
// Restore
token.name = oldName
})
},
updateToken(token) {
return axios.put(this.baseUrl + '/' + token.id, token)
.then(resp => resp.data)
},
deleteToken(token) {
console.debug('deleting app token', token)
// eslint-disable-next-line vue/no-mutating-props
this.tokens = this.tokens.filter(t => t !== token)
return axios.delete(this.baseUrl + '/' + token.id)
.then(resp => resp.data)
.then(tap(() => console.debug('app token deleted')))
.catch(err => {
console.error.bind('could not delete app token', err)
OC.Notification.showTemporary(t('core', 'Error while deleting the token'))
// Restore
// eslint-disable-next-line vue/no-mutating-props
this.tokens.push(token)
})
},
async wipeToken(token) {
console.debug('wiping app token', token)
try {
await confirmPassword()
if (!(await confirm())) {
console.debug('wipe aborted by user')
return
}
await axios.post(this.baseUrl + '/wipe/' + token.id)
console.debug('app token marked for wipe')
token.type = 2
} catch (err) {
console.error('could not wipe app token', err)
OC.Notification.showTemporary(t('core', 'Error while wiping the device with the token'))
}
},
},
}
</script>
<style scoped>
</style>
|