2025-03-03 13:20:44 +08:00
|
|
|
import * as os from 'os';
|
|
|
|
|
|
|
|
import User from "./user";
|
|
|
|
import * as utils from './utils';
|
|
|
|
|
|
|
|
export default class DevstarAPIHandler {
|
|
|
|
|
|
|
|
private devstarDomain: string;
|
|
|
|
|
2025-03-23 16:34:03 +08:00
|
|
|
constructor() {
|
2025-03-03 13:20:44 +08:00
|
|
|
// 获取domain
|
|
|
|
const devstarDomainFromUserConfig = utils.getDevstarDomain()
|
|
|
|
if (undefined == devstarDomainFromUserConfig || "" == devstarDomainFromUserConfig) {
|
|
|
|
this.devstarDomain = "https://devstar.cn";
|
|
|
|
} else {
|
|
|
|
this.devstarDomain = devstarDomainFromUserConfig.endsWith('/') ? devstarDomainFromUserConfig.slice(0, -1) : devstarDomainFromUserConfig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-23 21:59:57 +08:00
|
|
|
public async verifyToken(token: string, username: string): Promise<boolean> {
|
|
|
|
try {
|
|
|
|
const response = await fetch(this.devstarDomain + `/api/devcontainer/user`, {
|
|
|
|
method: 'GET',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'Authorization': 'token ' + token
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// 处理非200响应状态码
|
|
|
|
if (!response.ok) {
|
|
|
|
const text = await response.text(); // 先读取文本防止json解析失败
|
2025-03-24 16:24:46 +08:00
|
|
|
if (response.status == 401) {
|
|
|
|
throw new Error('Token错误')
|
|
|
|
} else {
|
|
|
|
throw new Error(`HTTP Error: ${response.status} - ${text}`);
|
|
|
|
}
|
2025-03-23 21:59:57 +08:00
|
|
|
}
|
|
|
|
|
2025-03-24 16:24:46 +08:00
|
|
|
const responseData = await response.json();
|
|
|
|
const data = responseData.data
|
2025-03-24 10:51:56 +08:00
|
|
|
if (data.username == undefined || data.username == "") {
|
|
|
|
throw new Error('Token对应用户不存在')
|
|
|
|
} else {
|
|
|
|
// 验证用户名匹配
|
|
|
|
if (data.username !== username) {
|
|
|
|
throw new Error('Token与用户名不符');
|
|
|
|
}
|
2025-03-23 17:16:35 +08:00
|
|
|
}
|
2025-03-23 21:59:57 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error)
|
|
|
|
return false
|
|
|
|
}
|
2025-03-23 17:16:35 +08:00
|
|
|
}
|
|
|
|
|
2025-03-03 13:20:44 +08:00
|
|
|
// 上传公钥
|
2025-03-23 16:34:03 +08:00
|
|
|
public async uploadUserPublicKey(user: User): Promise<string> {
|
2025-03-12 10:23:31 +08:00
|
|
|
return new Promise(async (resolve) => {
|
|
|
|
// 获取机器名称
|
|
|
|
const machineName = os.hostname();
|
|
|
|
// 组成公钥名称
|
|
|
|
const timestamp = Date.now();
|
2025-03-23 16:34:03 +08:00
|
|
|
const keyTitle = `${user.getUsernameFromLocal()}-${machineName}-${timestamp}`
|
2025-03-12 10:23:31 +08:00
|
|
|
const postData = {
|
2025-03-23 16:34:03 +08:00
|
|
|
"key": user.getUserPublicKey(),
|
2025-03-12 10:23:31 +08:00
|
|
|
"title": keyTitle
|
|
|
|
}
|
|
|
|
|
|
|
|
const uploadUrl = this.devstarDomain + `/api/v1/user/keys`
|
2025-03-03 13:20:44 +08:00
|
|
|
|
2025-03-12 10:23:31 +08:00
|
|
|
// 上传公钥
|
|
|
|
fetch(uploadUrl, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
2025-03-23 16:34:03 +08:00
|
|
|
'Authorization': 'token ' + user.getUserTokenFromLocal()
|
2025-03-12 10:23:31 +08:00
|
|
|
},
|
|
|
|
body: JSON.stringify(postData)
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
response.json().then(data => {
|
|
|
|
if (response.ok) {
|
|
|
|
console.log("Successfully upload new created public key.\n", data)
|
|
|
|
resolve("ok")
|
|
|
|
} else {
|
|
|
|
throw new Error(`Failed to upload new created public key!\nError: ${data.message}`)
|
|
|
|
}
|
|
|
|
})
|
2025-03-03 13:20:44 +08:00
|
|
|
})
|
2025-03-12 10:23:31 +08:00
|
|
|
.catch(error => {
|
|
|
|
console.error(error);
|
|
|
|
});
|
|
|
|
|
2025-03-23 17:16:35 +08:00
|
|
|
})
|
2025-03-03 13:20:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|