2023-07-27 07:31:52 +02:00
|
|
|
<!--
|
2024-02-13 16:59:27 +01:00
|
|
|
SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
<template>
|
2023-02-09 18:18:27 +01:00
|
|
|
<MkStickyContainer>
|
2023-02-22 07:28:17 +01:00
|
|
|
<template #header><MkPageHeader :actions="headerActions" :tabs="headerTabs"/></template>
|
2023-05-19 09:20:53 +02:00
|
|
|
<MkSpacer :contentMax="500">
|
2023-02-09 18:18:27 +01:00
|
|
|
<div v-if="state == 'fetch-session-error'">
|
|
|
|
<p>{{ i18n.ts.somethingHappened }}</p>
|
|
|
|
</div>
|
|
|
|
<div v-else-if="$i && !session">
|
2023-02-22 07:28:17 +01:00
|
|
|
<MkLoading/>
|
2023-02-09 18:18:27 +01:00
|
|
|
</div>
|
|
|
|
<div v-else-if="$i && session">
|
|
|
|
<XForm
|
|
|
|
v-if="state == 'waiting'"
|
|
|
|
class="form"
|
|
|
|
:session="session"
|
|
|
|
@denied="state = 'denied'"
|
|
|
|
@accepted="accepted"
|
|
|
|
/>
|
|
|
|
<div v-if="state == 'denied'">
|
|
|
|
<h1>{{ i18n.ts._auth.denied }}</h1>
|
|
|
|
</div>
|
|
|
|
<div v-if="state == 'accepted' && session">
|
2024-01-20 00:11:59 +01:00
|
|
|
<h1>{{ session.app.isAuthorized ? i18n.ts['already-authorized'] : i18n.ts.allowed }}</h1>
|
2023-02-22 07:28:17 +01:00
|
|
|
<p v-if="session.app.callbackUrl">
|
|
|
|
{{ i18n.ts._auth.callback }}
|
|
|
|
<MkEllipsis/>
|
2023-02-09 18:18:27 +01:00
|
|
|
</p>
|
|
|
|
<p v-if="!session.app.callbackUrl">{{ i18n.ts._auth.pleaseGoBack }}</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div v-else>
|
|
|
|
<p :class="$style.loginMessage">{{ i18n.ts._auth.pleaseLogin }}</p>
|
2023-02-22 07:28:17 +01:00
|
|
|
<MkSignin @login="onLogin"/>
|
2023-02-09 18:18:27 +01:00
|
|
|
</div>
|
|
|
|
</MkSpacer>
|
|
|
|
</MkStickyContainer>
|
2020-01-29 20:37:25 +01:00
|
|
|
</template>
|
|
|
|
|
2023-02-09 18:18:27 +01:00
|
|
|
<script lang="ts" setup>
|
2023-12-07 06:42:09 +01:00
|
|
|
import { onMounted, ref, computed } from 'vue';
|
2023-09-04 06:33:38 +02:00
|
|
|
import * as Misskey from 'misskey-js';
|
2020-01-29 20:37:25 +01:00
|
|
|
import XForm from './auth.form.vue';
|
2022-08-30 17:24:33 +02:00
|
|
|
import MkSignin from '@/components/MkSignin.vue';
|
2024-01-04 10:32:46 +01:00
|
|
|
import { misskeyApi } from '@/scripts/misskey-api.js';
|
2023-09-19 09:37:43 +02:00
|
|
|
import { $i, login } from '@/account.js';
|
|
|
|
import { definePageMetadata } from '@/scripts/page-metadata.js';
|
|
|
|
import { i18n } from '@/i18n.js';
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-09 18:18:27 +01:00
|
|
|
const props = defineProps<{
|
|
|
|
token: string;
|
|
|
|
}>();
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-09-24 20:44:33 +02:00
|
|
|
const getUrlParams = () =>
|
|
|
|
window.location.search
|
|
|
|
.substring(1)
|
2023-12-23 02:09:23 +01:00
|
|
|
.split('&')
|
2023-09-24 20:44:33 +02:00
|
|
|
.reduce((result, query) => {
|
2023-12-23 02:09:23 +01:00
|
|
|
const [k, v] = query.split('=');
|
2023-09-24 20:44:33 +02:00
|
|
|
result[k] = decodeURI(v);
|
|
|
|
return result;
|
|
|
|
}, {});
|
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
const state = ref<'waiting' | 'accepted' | 'fetch-session-error' | 'denied' | null>(null);
|
|
|
|
const session = ref<Misskey.entities.AuthSessionShowResponse | null>(null);
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-09 18:18:27 +01:00
|
|
|
function accepted() {
|
2023-12-07 06:42:09 +01:00
|
|
|
state.value = 'accepted';
|
2023-09-24 20:44:33 +02:00
|
|
|
const isMastodon = !!getUrlParams().mastodon;
|
2023-12-23 02:09:23 +01:00
|
|
|
if (session.value && session.value.app.callbackUrl && isMastodon) {
|
2023-09-24 20:44:33 +02:00
|
|
|
const redirectUri = decodeURIComponent(getUrlParams().redirect_uri);
|
2023-12-23 02:09:23 +01:00
|
|
|
if (!session.value.app.callbackUrl.includes('elk.zone') && !session.value.app.callbackUrl.split('\n').includes(redirectUri)) {
|
|
|
|
state.value = 'fetch-session-error';
|
|
|
|
throw new Error('Callback URI doesn\'t match registered app');
|
2023-09-24 20:44:33 +02:00
|
|
|
}
|
2023-12-23 02:09:23 +01:00
|
|
|
const callbackUrl = session.value.app.callbackUrl.includes('elk.zone') ? new URL(session.value.app.callbackUrl) : new URL(redirectUri);
|
|
|
|
callbackUrl.searchParams.append('code', session.value.token);
|
|
|
|
if (getUrlParams().state) callbackUrl.searchParams.append('state', getUrlParams().state);
|
2023-09-24 20:44:33 +02:00
|
|
|
location.href = callbackUrl.toString();
|
2023-12-23 02:09:23 +01:00
|
|
|
} else if (session.value && session.value.app.callbackUrl) {
|
2023-12-07 06:42:09 +01:00
|
|
|
const url = new URL(session.value.app.callbackUrl);
|
2023-02-09 18:18:27 +01:00
|
|
|
if (['javascript:', 'file:', 'data:', 'mailto:', 'tel:'].includes(url.protocol)) throw new Error('invalid url');
|
2023-12-07 06:42:09 +01:00
|
|
|
location.href = `${session.value.app.callbackUrl}?token=${session.value.token}`;
|
2023-02-09 18:18:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function onLogin(res) {
|
|
|
|
login(res.i);
|
|
|
|
}
|
|
|
|
|
|
|
|
onMounted(async () => {
|
|
|
|
if (!$i) return;
|
|
|
|
|
|
|
|
try {
|
2024-01-04 10:32:46 +01:00
|
|
|
session.value = await misskeyApi('auth/session/show', {
|
2023-02-09 18:18:27 +01:00
|
|
|
token: props.token,
|
2020-01-29 20:37:25 +01:00
|
|
|
});
|
2023-02-09 18:18:27 +01:00
|
|
|
|
|
|
|
// 既に連携していた場合
|
2023-12-07 06:42:09 +01:00
|
|
|
if (session.value.app.isAuthorized) {
|
2024-01-04 10:32:46 +01:00
|
|
|
await misskeyApi('auth/accept', {
|
2023-12-07 06:42:09 +01:00
|
|
|
token: session.value.token,
|
2023-02-09 18:18:27 +01:00
|
|
|
});
|
|
|
|
accepted();
|
|
|
|
} else {
|
2023-12-07 06:42:09 +01:00
|
|
|
state.value = 'waiting';
|
2023-02-09 18:18:27 +01:00
|
|
|
}
|
2023-02-22 07:28:17 +01:00
|
|
|
} catch (err) {
|
2023-12-07 06:42:09 +01:00
|
|
|
state.value = 'fetch-session-error';
|
2023-02-09 18:18:27 +01:00
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
});
|
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
const headerActions = computed(() => []);
|
2023-02-09 18:18:27 +01:00
|
|
|
|
2023-12-07 06:42:09 +01:00
|
|
|
const headerTabs = computed(() => []);
|
2023-02-09 18:18:27 +01:00
|
|
|
|
2024-02-16 08:17:09 +01:00
|
|
|
definePageMetadata(() => ({
|
2023-02-09 18:18:27 +01:00
|
|
|
title: i18n.ts._auth.shareAccessTitle,
|
2024-06-22 15:16:19 +02:00
|
|
|
icon: 'ti ti-apps',
|
2024-02-16 08:17:09 +01:00
|
|
|
}));
|
2023-02-09 18:18:27 +01:00
|
|
|
</script>
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-02-09 18:18:27 +01:00
|
|
|
<style lang="scss" module>
|
|
|
|
.loginMessage {
|
|
|
|
text-align: center;
|
|
|
|
margin: 8px 0 24px;
|
|
|
|
}
|
2020-01-29 20:37:25 +01:00
|
|
|
</style>
|