mirror of
https://github.com/unraid/api.git
synced 2026-01-01 06:01:18 -06:00
feat: sso button token exchange
This commit is contained in:
@@ -3,10 +3,15 @@ import Button from '~/components/Brand/Button.vue';
|
|||||||
import { ACCOUNT } from '~/helpers/urls';
|
import { ACCOUNT } from '~/helpers/urls';
|
||||||
|
|
||||||
export interface Props {
|
export interface Props {
|
||||||
ssoenabled?: boolean;
|
ssoenabled?: boolean | string;
|
||||||
|
ssoEnabled?: boolean;
|
||||||
}
|
}
|
||||||
const props = defineProps<Props>();
|
const props = defineProps<Props>();
|
||||||
|
|
||||||
|
const isSsoEnabled = computed<boolean>(
|
||||||
|
() => props['ssoenabled'] === true || props['ssoenabled'] === 'true' || props.ssoEnabled
|
||||||
|
);
|
||||||
|
|
||||||
const enterCallbackTokenIntoField = (token: string) => {
|
const enterCallbackTokenIntoField = (token: string) => {
|
||||||
const passwordField = document.querySelector('input[name=password]') as HTMLInputElement;
|
const passwordField = document.querySelector('input[name=password]') as HTMLInputElement;
|
||||||
const usernameField = document.querySelector('input[name=username]') as HTMLInputElement;
|
const usernameField = document.querySelector('input[name=username]') as HTMLInputElement;
|
||||||
@@ -42,11 +47,11 @@ onMounted(async () => {
|
|||||||
const sessionState = getStateToken();
|
const sessionState = getStateToken();
|
||||||
|
|
||||||
if (code && state === sessionState) {
|
if (code && state === sessionState) {
|
||||||
const token = await fetch(new URL('token', ACCOUNT), {
|
const token = await fetch(new URL('/oauth2/token', ACCOUNT), {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: new URLSearchParams({
|
body: new URLSearchParams({
|
||||||
code,
|
code,
|
||||||
clientId: 'CONNECT_SERVER_SSO',
|
client_id: 'CONNECT_SERVER_SSO',
|
||||||
grant_type: 'authorization_code',
|
grant_type: 'authorization_code',
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
@@ -65,20 +70,21 @@ onMounted(async () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const externalSSOUrl = computed<string>(() => {
|
const navigateToExternalSSOUrl = () => {
|
||||||
const url = new URL('sso', ACCOUNT);
|
const url = new URL('sso', ACCOUNT);
|
||||||
const callbackUrlLogin = new URL('login', window.location.origin);
|
const callbackUrlLogin = new URL('login', window.location.origin);
|
||||||
const state = generateStateToken();
|
const state = generateStateToken();
|
||||||
callbackUrlLogin.searchParams.append('state', state);
|
|
||||||
|
|
||||||
url.searchParams.append('callbackUrl', callbackUrlLogin.toString());
|
url.searchParams.append('callbackUrl', callbackUrlLogin.toString());
|
||||||
return url.toString();
|
url.searchParams.append('state', state);
|
||||||
});
|
|
||||||
|
window.location.href = url.toString();
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<template v-if="props.ssoenabled === true">
|
<template v-if="isSsoEnabled">
|
||||||
<Button target="_blank" :href="externalSSOUrl">Sign In With Unraid.net Account</Button>
|
<Button btnStyle="outline" @click="navigateToExternalSSOUrl" >Sign In With Unraid.net Account</Button>
|
||||||
</template>
|
</template>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|||||||
@@ -156,7 +156,7 @@ onMounted(() => {
|
|||||||
<div class="bg-background">
|
<div class="bg-background">
|
||||||
<hr class="border-black dark:border-white" />
|
<hr class="border-black dark:border-white" />
|
||||||
<h2 class="text-xl font-semibold font-mono">SSO Button Component</h2>
|
<h2 class="text-xl font-semibold font-mono">SSO Button Component</h2>
|
||||||
<SsoButtonCe :ssoEnabled="serverState.ssoEnabled" />
|
<SsoButtonCe :ssoenabled="serverState.ssoEnabled" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</client-only>
|
</client-only>
|
||||||
|
|||||||
14
web/pages/login.vue
Normal file
14
web/pages/login.vue
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
<script lang="ts" setup>
|
||||||
|
import { useRoute, useRouter } from 'vue-router';
|
||||||
|
|
||||||
|
// Access route and router
|
||||||
|
const route = useRoute();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
// Redirect to '/' with the current query parameters
|
||||||
|
router.replace({ path: '/', query: route.query });
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
Redirecting you back to /
|
||||||
|
</template>
|
||||||
@@ -80,7 +80,7 @@ onBeforeMount(() => {
|
|||||||
<h3 class="text-lg font-semibold font-mono">
|
<h3 class="text-lg font-semibold font-mono">
|
||||||
SSOSignInButtonCe
|
SSOSignInButtonCe
|
||||||
</h3>
|
</h3>
|
||||||
<unraid-sso-button :ssoEnabled="serverState.ssoEnabled" />
|
<unraid-sso-button :ssoenabled="serverState.ssoEnabled" />
|
||||||
</unraid-i18n-host>
|
</unraid-i18n-host>
|
||||||
</client-only>
|
</client-only>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
Reference in New Issue
Block a user