security: verify token and username before storing

This commit is contained in:
Levi Yan
2025-03-23 17:17:32 +08:00
parent fe52e2bf8f
commit a55ee0cf90

View File

@@ -17,6 +17,8 @@ export class DevStarExtension {
const handler = vscode.window.registerUriHandler({ const handler = vscode.window.registerUriHandler({
handleUri: async (uri: vscode.Uri) => { handleUri: async (uri: vscode.Uri) => {
const devstarAPIHandler = new DevstarAPIHandler()
if (uri.path === '/openProject') { if (uri.path === '/openProject') {
const params = new URLSearchParams(uri.query); const params = new URLSearchParams(uri.query);
const host = params.get('host'); const host = params.get('host');
@@ -35,6 +37,11 @@ export class DevStarExtension {
if (access_token && devstar_username) { if (access_token && devstar_username) {
if (!this.user.isLogged()) { if (!this.user.isLogged()) {
// 如果没有用户登录,则直接登录; // 如果没有用户登录,则直接登录;
await devstarAPIHandler.verifyToken(access_token, devstar_username)
.then(async (res) => {
if (res === 'ok') {
// token与用户名验证通过
// 插件登录存储token与用户名
this.user.setUserTokenToLocal(access_token) this.user.setUserTokenToLocal(access_token)
this.user.setUsernameToLocal(devstar_username) this.user.setUsernameToLocal(devstar_username)
@@ -43,7 +50,6 @@ export class DevStarExtension {
await this.user.createUserSSHKey() await this.user.createUserSSHKey()
.then(async () => { .then(async () => {
// 上传公钥 // 上传公钥
const devstarAPIHandler = new DevstarAPIHandler()
await devstarAPIHandler.uploadUserPublicKey(this.user) await devstarAPIHandler.uploadUserPublicKey(this.user)
.then(async (res) => { .then(async (res) => {
if (res === "ok") { if (res === "ok") {
@@ -53,9 +59,11 @@ export class DevStarExtension {
}) })
}) })
} else { } else {
// 直接打开项目 // 公钥已上传,直接打开项目
await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path) await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
} }
}
})
} else if (devstar_username === this.user.getUsernameFromLocal()) { } else if (devstar_username === this.user.getUsernameFromLocal()) {
// 如果同用户已经登录,则忽略; // 如果同用户已经登录,则忽略;
// 直接打开项目 // 直接打开项目
@@ -65,15 +73,19 @@ export class DevStarExtension {
const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`, const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
'Yes', 'No',); 'Yes', 'No',);
if (selection === 'Yes') { if (selection === 'Yes') {
this.user.setUserTokenToLocal(access_token); await devstarAPIHandler.verifyToken(access_token, devstar_username)
this.user.setUsernameToLocal(devstar_username); .then(async (res) => {
if (res === 'ok') {
// token与用户名验证通过
// 插件登录存储token与用户名
this.user.setUserTokenToLocal(access_token)
this.user.setUsernameToLocal(devstar_username)
// 检查本地是否有用户所属公钥,没有则创建 // 检查本地是否有用户所属公钥,没有则创建
if (!this.user.existUserPublicKey()) { if (!this.user.existUserPublicKey()) {
await this.user.createUserSSHKey() await this.user.createUserSSHKey()
.then(async () => { .then(async () => {
// 上传公钥 // 上传公钥
const devstarAPIHandler = new DevstarAPIHandler()
await devstarAPIHandler.uploadUserPublicKey(this.user) await devstarAPIHandler.uploadUserPublicKey(this.user)
.then(async (res) => { .then(async (res) => {
if (res === "ok") { if (res === "ok") {
@@ -83,9 +95,11 @@ export class DevStarExtension {
}) })
}) })
} else { } else {
// 直接打开项目 // 公钥已上传,直接打开项目
await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path) await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
} }
}
})
} else if (selection === 'No') { } else if (selection === 'No') {
await openProjectWithoutLogging(container_host, container_port, container_username, project_path); await openProjectWithoutLogging(container_host, container_port, container_username, project_path);
} }