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