2020-01-30 04:37:25 +09:00
|
|
|
<template>
|
2021-11-19 19:36:12 +09:00
|
|
|
<div v-if="$i && fetching" class="">
|
2020-10-17 20:12:00 +09:00
|
|
|
<MkLoading/>
|
2020-01-30 04:37:25 +09:00
|
|
|
</div>
|
2020-12-19 10:55:52 +09:00
|
|
|
<div v-else-if="$i">
|
2020-10-17 20:12:00 +09:00
|
|
|
<XForm
|
2020-01-30 04:37:25 +09:00
|
|
|
v-if="state == 'waiting'"
|
2021-11-19 19:36:12 +09:00
|
|
|
ref="form"
|
|
|
|
class="form"
|
2020-01-30 04:37:25 +09:00
|
|
|
:session="session"
|
|
|
|
@denied="state = 'denied'"
|
|
|
|
@accepted="accepted"
|
|
|
|
/>
|
2021-11-19 19:36:12 +09:00
|
|
|
<div v-if="state == 'denied'" class="denied">
|
2022-11-11 07:12:44 +09:00
|
|
|
<h1>{{ i18n.ts._auth.denied }}</h1>
|
2020-01-30 04:37:25 +09:00
|
|
|
</div>
|
2021-11-19 19:36:12 +09:00
|
|
|
<div v-if="state == 'accepted'" class="accepted">
|
2022-11-16 18:29:18 +09:00
|
|
|
<h1>{{ session.app.isAuthorized ? i18n.t('already-authorized') : i18n.ts.allowed }}</h1>
|
2023-02-12 05:08:12 +09:00
|
|
|
<p v-if="session.app.callbackUrl && !auth_code">{{ i18n.ts._auth.callback }}<MkEllipsis/></p>
|
|
|
|
<MkKeyValue v-if="session.app.callbackUrl && auth_code" :copy="auth_code">
|
|
|
|
<template #key>{{ i18n.ts._auth.copyAsk }}</template>
|
|
|
|
<template #value>{{ auth_code }}</template>
|
|
|
|
</MkKeyValue>
|
2022-11-11 07:12:44 +09:00
|
|
|
<p v-if="!session.app.callbackUrl">{{ i18n.ts._auth.pleaseGoBack }}</p>
|
2020-01-30 04:37:25 +09:00
|
|
|
</div>
|
2021-11-19 19:36:12 +09:00
|
|
|
<div v-if="state == 'fetch-session-error'" class="error">
|
2022-11-11 07:12:44 +09:00
|
|
|
<p>{{ i18n.ts.somethingHappened }}</p>
|
2020-01-30 04:37:25 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-11-19 19:36:12 +09:00
|
|
|
<div v-else class="signin">
|
2020-10-17 20:12:00 +09:00
|
|
|
<MkSignin @login="onLogin"/>
|
2020-01-30 04:37:25 +09:00
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts">
|
2020-10-17 20:12:00 +09:00
|
|
|
import { defineComponent } from 'vue';
|
2020-01-30 04:37:25 +09:00
|
|
|
import XForm from './auth.form.vue';
|
2022-08-31 00:24:33 +09:00
|
|
|
import MkSignin from '@/components/MkSignin.vue';
|
2023-02-12 05:08:12 +09:00
|
|
|
import MkKeyValue from '@/components/MkKeyValue.vue';
|
2021-11-12 02:02:25 +09:00
|
|
|
import * as os from '@/os';
|
|
|
|
import { login } from '@/account';
|
2022-11-11 07:12:44 +09:00
|
|
|
import { i18n } from '@/i18n';
|
2020-01-30 04:37:25 +09:00
|
|
|
|
2020-10-17 20:12:00 +09:00
|
|
|
export default defineComponent({
|
2020-01-30 04:37:25 +09:00
|
|
|
components: {
|
2020-02-20 23:07:20 +09:00
|
|
|
XForm,
|
|
|
|
MkSignin,
|
2023-02-12 05:08:12 +09:00
|
|
|
MkKeyValue
|
2020-01-30 04:37:25 +09:00
|
|
|
},
|
2022-06-20 17:38:49 +09:00
|
|
|
props: ['token'],
|
2020-01-30 04:37:25 +09:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
state: null,
|
|
|
|
session: null,
|
2022-06-20 17:38:49 +09:00
|
|
|
fetching: true,
|
2022-11-16 18:29:18 +09:00
|
|
|
i18n,
|
2023-02-12 05:08:12 +09:00
|
|
|
auth_code: null
|
2020-01-30 04:37:25 +09:00
|
|
|
};
|
|
|
|
},
|
|
|
|
mounted() {
|
2020-12-19 10:55:52 +09:00
|
|
|
if (!this.$i) return;
|
2020-01-30 04:37:25 +09:00
|
|
|
|
|
|
|
// Fetch session
|
2020-10-17 20:12:00 +09:00
|
|
|
os.api('auth/session/show', {
|
2022-06-20 17:38:49 +09:00
|
|
|
token: this.token,
|
2020-01-30 04:37:25 +09:00
|
|
|
}).then(session => {
|
|
|
|
this.session = session;
|
|
|
|
this.fetching = false;
|
|
|
|
|
|
|
|
// 既に連携していた場合
|
|
|
|
if (this.session.app.isAuthorized) {
|
2020-10-17 20:12:00 +09:00
|
|
|
os.api('auth/accept', {
|
2022-06-20 17:38:49 +09:00
|
|
|
token: this.session.token,
|
2020-01-30 04:37:25 +09:00
|
|
|
}).then(() => {
|
|
|
|
this.accepted();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.state = 'waiting';
|
|
|
|
}
|
|
|
|
}).catch(error => {
|
|
|
|
this.state = 'fetch-session-error';
|
|
|
|
this.fetching = false;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
accepted() {
|
|
|
|
this.state = 'accepted';
|
2023-02-10 07:21:50 +09:00
|
|
|
const getUrlParams = () => window.location.search.substring(1).split('&').reduce((result, query) => { const [k, v] = query.split('='); result[k] = decodeURI(v); return result; }, {});
|
2023-03-01 01:23:04 +09:00
|
|
|
const isMastodon = !!getUrlParams().mastodon
|
|
|
|
if (this.session.app.callbackUrl && isMastodon) {
|
|
|
|
const state = getUrlParams().state
|
|
|
|
const stateParam = `&state=${state}`
|
|
|
|
const tokenRaw = this.session.token
|
|
|
|
const token = tokenRaw.replaceAll('-', '')
|
|
|
|
location.href = `${this.session.app.callbackUrl}?code=${token}${stateParam}`;
|
|
|
|
} else if (this.session.app.callbackUrl) {
|
2023-02-11 04:14:33 +09:00
|
|
|
const url = new URL(this.session.app.callbackUrl);
|
|
|
|
if (['javascript:', 'file:', 'data:', 'mailto:', 'tel:'].includes(url.protocol)) throw new Error('invalid url');
|
2023-02-12 05:08:12 +09:00
|
|
|
if (this.session.app.callbackUrl === "urn:ietf:wg:oauth:2.0:oob") {
|
|
|
|
this.auth_code = this.session.token;
|
|
|
|
} else {
|
|
|
|
location.href = `${this.session.app.callbackUrl}?token=${this.session.token}&code=${this.session.token}&state=${getUrlParams().state || ''}`;
|
|
|
|
}
|
2020-01-30 04:37:25 +09:00
|
|
|
}
|
2020-03-14 15:59:02 +09:00
|
|
|
}, onLogin(res) {
|
2020-12-19 10:55:52 +09:00
|
|
|
login(res.i);
|
2022-06-20 17:38:49 +09:00
|
|
|
},
|
|
|
|
},
|
2020-01-30 04:37:25 +09:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style lang="scss" scoped>
|
|
|
|
|
|
|
|
</style>
|