123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <!--
- - @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>
|