2024-10-23 13:45:19 +08:00
|
|
|
import * as vscode from 'vscode';
|
2024-10-23 17:03:11 +08:00
|
|
|
import * as path from 'path';
|
|
|
|
import * as os from 'os';
|
2024-10-23 18:37:27 +08:00
|
|
|
import * as fs from 'fs';
|
|
|
|
const {
|
|
|
|
generateKeyPairSync,
|
|
|
|
} = require('node:crypto')
|
2024-10-23 13:45:19 +08:00
|
|
|
|
|
|
|
export default class User {
|
|
|
|
private context:vscode.ExtensionContext;
|
2024-10-23 17:03:11 +08:00
|
|
|
private username:string|undefined;
|
|
|
|
private userToken:string|undefined;
|
2024-10-23 13:45:19 +08:00
|
|
|
private usernameKey:string = 'devstarUsername'
|
|
|
|
private userTokenKey:string = 'devstarUserToken'
|
|
|
|
|
|
|
|
constructor(context: vscode.ExtensionContext) {
|
|
|
|
this.context = context;
|
2024-10-23 17:03:11 +08:00
|
|
|
this.username = this.context.globalState.get(this.usernameKey);
|
|
|
|
this.userToken = this.context.globalState.get(this.userTokenKey);
|
2024-10-23 13:45:19 +08:00
|
|
|
}
|
|
|
|
|
2024-10-23 18:37:27 +08:00
|
|
|
private isLogged() {
|
|
|
|
var existUsername = false;
|
|
|
|
var existUserToken = false;
|
|
|
|
if(this.username != undefined && this.username != '') {
|
|
|
|
existUsername = true;
|
|
|
|
}
|
|
|
|
if(this.userToken != undefined && this.userToken != '') {
|
|
|
|
existUserToken = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existUsername && existUserToken) {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-23 13:45:19 +08:00
|
|
|
public getUsernameFromLocal(): string|undefined {
|
2024-10-23 17:03:11 +08:00
|
|
|
return this.username;
|
2024-10-23 13:45:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public getUserTokenFromLocal(): string|undefined{
|
2024-10-23 17:03:11 +08:00
|
|
|
return this.userToken;
|
2024-10-23 13:45:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public setUsernameToLocal(username:string) {
|
2024-10-23 17:03:11 +08:00
|
|
|
this.context.globalState.update(this.usernameKey, username);
|
|
|
|
this.username = username;
|
2024-10-23 13:45:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public setUserTokenToLocal(userToken:string) {
|
|
|
|
this.context.globalState.update(this.userTokenKey, userToken)
|
2024-10-23 17:03:11 +08:00
|
|
|
this.userToken = userToken
|
2024-10-23 13:45:19 +08:00
|
|
|
}
|
|
|
|
|
2024-10-23 18:37:27 +08:00
|
|
|
public getUserPrivateKeyPath() : string{
|
|
|
|
if (!this.isLogged) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.join(os.homedir(), '.ssh', `id_rsa_${this.username}`)
|
|
|
|
}
|
|
|
|
|
|
|
|
public getUserPublicKeyPath() :string{
|
|
|
|
if (!this.isLogged) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return path.join(os.homedir(), '.ssh', `id_rsa_${this.username}.pub`)
|
|
|
|
}
|
|
|
|
|
|
|
|
public existUserPublicKey() :boolean{
|
|
|
|
const userPublicKeyPath = this.getUserPublicKeyPath();
|
|
|
|
return fs.existsSync(userPublicKeyPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
public existUserPrivateKey() :boolean{
|
|
|
|
const userPrivateKeyPath = this.getUserPrivateKeyPath();
|
|
|
|
return fs.existsSync(userPrivateKeyPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
public getUserPublicKey(): string {
|
|
|
|
const userPublicKeyPath = this.getUserPublicKeyPath();
|
|
|
|
const userPublicKey = fs.readFileSync(userPublicKeyPath, 'utf-8');
|
|
|
|
// remove `\r` `\n`
|
|
|
|
const trimmedDefaultPublicKey = userPublicKey.replace(/[\r\n]/g, "");
|
|
|
|
return trimmedDefaultPublicKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
public getUserPrivateKey(): string {
|
|
|
|
const userPrivateKey = this.getUserPrivateKeyPath();
|
|
|
|
return fs.readFileSync(userPrivateKey, 'utf-8');
|
|
|
|
}
|
|
|
|
|
|
|
|
public createUserSSHKey() {
|
|
|
|
if (this.existUserPublicKey() && this.existUserPrivateKey()) {
|
|
|
|
// if both public and private key exists, stop
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
publicKey,
|
|
|
|
privateKey,
|
|
|
|
} = generateKeyPairSync('rsa', {
|
|
|
|
modulusLength: 4096,
|
|
|
|
publicKeyEncoding: {
|
|
|
|
type: 'spki',
|
|
|
|
format: 'pem',
|
|
|
|
},
|
|
|
|
privateKeyEncoding: {
|
|
|
|
type: 'pkcs8',
|
|
|
|
format: 'pem',
|
|
|
|
cipher: 'aes-256-cbc',
|
|
|
|
passphrase: 'devstar'
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
fs.writeFileSync(this.getUserPublicKeyPath(), publicKey);
|
|
|
|
fs.writeFileSync(this.getUserPrivateKeyPath(), privateKey);
|
|
|
|
} catch(error) {
|
|
|
|
console.error("Failed to write public/private key into the default ssh public/key file: ", error);
|
|
|
|
}
|
|
|
|
}
|
2024-10-23 13:45:19 +08:00
|
|
|
}
|