refactor: extract code related to logging into login function
This commit is contained in:
51
src/main.ts
51
src/main.ts
@@ -37,33 +37,12 @@ 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)
|
const res = await this.user.login(access_token, devstar_username)
|
||||||
.then(async (res) => {
|
|
||||||
if (res === 'ok') {
|
if (res === 'ok') {
|
||||||
// token与用户名验证通过
|
|
||||||
// 插件登录:存储token与用户名
|
|
||||||
this.user.setUserTokenToLocal(access_token)
|
|
||||||
this.user.setUsernameToLocal(devstar_username)
|
|
||||||
|
|
||||||
// 检查本地是否有用户所属公钥,没有则创建
|
|
||||||
if (!this.user.existUserPublicKey()) {
|
|
||||||
await this.user.createUserSSHKey()
|
|
||||||
.then(async () => {
|
|
||||||
// 上传公钥
|
|
||||||
await devstarAPIHandler.uploadUserPublicKey(this.user)
|
|
||||||
.then(async (res) => {
|
|
||||||
if (res === "ok") {
|
|
||||||
// 打开项目
|
|
||||||
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 {
|
} else {
|
||||||
// 公钥已上传,直接打开项目
|
console.error(res)
|
||||||
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()) {
|
||||||
// 如果同用户已经登录,则忽略;
|
// 如果同用户已经登录,则忽略;
|
||||||
// 直接打开项目
|
// 直接打开项目
|
||||||
@@ -73,33 +52,13 @@ 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') {
|
||||||
await devstarAPIHandler.verifyToken(access_token, devstar_username)
|
// 如果没有用户登录,则直接登录;
|
||||||
.then(async (res) => {
|
const res = await this.user.login(access_token, devstar_username)
|
||||||
if (res === 'ok') {
|
if (res === 'ok') {
|
||||||
// token与用户名验证通过
|
|
||||||
// 插件登录:存储token与用户名
|
|
||||||
this.user.setUserTokenToLocal(access_token)
|
|
||||||
this.user.setUsernameToLocal(devstar_username)
|
|
||||||
|
|
||||||
// 检查本地是否有用户所属公钥,没有则创建
|
|
||||||
if (!this.user.existUserPublicKey()) {
|
|
||||||
await this.user.createUserSSHKey()
|
|
||||||
.then(async () => {
|
|
||||||
// 上传公钥
|
|
||||||
await devstarAPIHandler.uploadUserPublicKey(this.user)
|
|
||||||
.then(async (res) => {
|
|
||||||
if (res === "ok") {
|
|
||||||
// 打开项目
|
|
||||||
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 {
|
} else {
|
||||||
// 公钥已上传,直接打开项目
|
console.error(res)
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
40
src/user.ts
40
src/user.ts
@@ -2,6 +2,7 @@ import * as vscode from 'vscode';
|
|||||||
import * as path from 'path';
|
import * as path from 'path';
|
||||||
import * as os from 'os';
|
import * as os from 'os';
|
||||||
import * as fs from 'fs';
|
import * as fs from 'fs';
|
||||||
|
import DevstarAPIHandler from './devstar-api';
|
||||||
const {
|
const {
|
||||||
generateKeyPairSync,
|
generateKeyPairSync,
|
||||||
createHash
|
createHash
|
||||||
@@ -22,7 +23,7 @@ export default class User {
|
|||||||
this.userToken = this.context.globalState.get(this.userTokenKey);
|
this.userToken = this.context.globalState.get(this.userTokenKey);
|
||||||
|
|
||||||
// 提取devstar domain的主域名,用于本地ssh key的命名
|
// 提取devstar domain的主域名,用于本地ssh key的命名
|
||||||
let devstarDomainFromConfig: string|undefined;
|
let devstarDomainFromConfig: string | undefined;
|
||||||
let devstarDomainURL: string;
|
let devstarDomainURL: string;
|
||||||
devstarDomainFromConfig = vscode.workspace.getConfiguration('devstar').get('devstarDomain')
|
devstarDomainFromConfig = vscode.workspace.getConfiguration('devstar').get('devstarDomain')
|
||||||
// 如果没有配置devstar domain,则默认domain为https://devstar.cn
|
// 如果没有配置devstar domain,则默认domain为https://devstar.cn
|
||||||
@@ -31,6 +32,43 @@ export default class User {
|
|||||||
this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_'); //提取hostname,并用下划线替换.
|
this.devstarHostname = parsedUrl.hostname.replace(/\./g, '_'); //提取hostname,并用下划线替换.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async login(token: string, username: string): Promise<string> {
|
||||||
|
const devstarAPIHandler = new DevstarAPIHandler()
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await devstarAPIHandler.verifyToken(token, username)
|
||||||
|
if (res !== 'ok') {
|
||||||
|
throw new Error('Token verification failed')
|
||||||
|
}
|
||||||
|
|
||||||
|
// token与用户名验证通过
|
||||||
|
// 插件登录:存储token与用户名
|
||||||
|
this.setUserTokenToLocal(token)
|
||||||
|
this.setUsernameToLocal(username)
|
||||||
|
|
||||||
|
// 检查本地是否有用户所属公钥,没有则创建
|
||||||
|
if (!this.existUserPublicKey()) {
|
||||||
|
await this.createUserSSHKey()
|
||||||
|
|
||||||
|
// 上传公钥
|
||||||
|
const uploadResult = await devstarAPIHandler.uploadUserPublicKey(this)
|
||||||
|
if (uploadResult !== 'ok') {
|
||||||
|
throw new Error('Upload user public key failed')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'ok'
|
||||||
|
} catch (error) {
|
||||||
|
console.error(error)
|
||||||
|
return 'login failed'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public logout() {
|
||||||
|
this.setUserTokenToLocal("")
|
||||||
|
this.setUsernameToLocal("")
|
||||||
|
}
|
||||||
|
|
||||||
public isLogged() {
|
public isLogged() {
|
||||||
var existUsername = false;
|
var existUsername = false;
|
||||||
var existUserToken = false;
|
var existUserToken = false;
|
||||||
|
Reference in New Issue
Block a user