-
Notifications
You must be signed in to change notification settings - Fork 153
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: introduce local auth for running commands (#257)
- Loading branch information
Showing
35 changed files
with
924 additions
and
231 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<script setup lang="ts"> | ||
import { AuthComfirm } from '~/composables/dialog' | ||
</script> | ||
|
||
<template> | ||
<AuthComfirm v-slot="{ resolve }"> | ||
<NDialog :model-value="true" @close="resolve(false)"> | ||
<div p4 flex="~ col gap-2"> | ||
<h3 class="mb2 text-lg font-medium leading-6" flex="~ items-center gap-1" text-orange> | ||
<span class="i-carbon-information-square" /> Permissions required | ||
</h3> | ||
<p> | ||
This operation requires permissions for running command and access files from the browser. | ||
</p> | ||
<p> | ||
A request is sent to the server.<br> | ||
Please check your terminal for the instructions and then come back. | ||
</p> | ||
<div flex="~ gap-3" mt2 justify-end> | ||
<NButton @click="resolve(false)"> | ||
Cancel | ||
</NButton> | ||
<NButton disabled icon="i-carbon-time"> | ||
Waiting for authorization... | ||
</NButton> | ||
</div> | ||
</div> | ||
</NDialog> | ||
</AuthComfirm> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { until } from '@vueuse/core' | ||
import { UAParser } from 'ua-parser-js' | ||
|
||
export const devAuthToken = ref<string | null>(localStorage.getItem('__nuxt_dev_token__')) | ||
|
||
export const isDevAuthed = ref(false) | ||
|
||
const bc = new BroadcastChannel('__nuxt_dev_token__') | ||
|
||
bc.addEventListener('message', (e) => { | ||
if (e.data.event === 'new-token') { | ||
const token = e.data.data | ||
rpc.verifyAuthToken(token) | ||
.then((result) => { | ||
devAuthToken.value = result ? token : null | ||
isDevAuthed.value = result | ||
}) | ||
} | ||
}) | ||
|
||
export async function ensureDevAuthToken() { | ||
if (isDevAuthed.value) | ||
return devAuthToken.value! | ||
|
||
if (!devAuthToken.value) { | ||
const info = new UAParser(navigator.userAgent).getResult() | ||
const desc = [ | ||
info.browser.name, | ||
info.browser.version, | ||
'|', | ||
info.os.name, | ||
info.os.version, | ||
info.device.type, | ||
].filter(i => i).join(' ') | ||
rpc.requestForAuth(desc) | ||
|
||
const result = await Promise.race([ | ||
AuthComfirm.start(), | ||
until(devAuthToken.value).toBeTruthy(), | ||
]) | ||
|
||
if (result === false) { | ||
// @unocss-include | ||
showNotification({ | ||
message: 'Action canceled', | ||
icon: 'carbon-close', | ||
classes: 'text-orange', | ||
}) | ||
throw new Error('User canceled auth') | ||
} | ||
} | ||
|
||
isDevAuthed.value = await rpc.verifyAuthToken(devAuthToken.value!) | ||
if (!isDevAuthed.value) { | ||
devAuthToken.value = null | ||
showNotification({ | ||
message: 'Invalid auth token, action canceled', | ||
icon: 'i-carbon-warning-alt', | ||
classes: 'text-red', | ||
}) | ||
throw new Error('Invalid auth token') | ||
} | ||
|
||
return devAuthToken.value! | ||
} |
Oops, something went wrong.