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 (!this.user.isLogged()) {
 | 
			
		||||
                // 如果没有用户登录,则直接登录;
 | 
			
		||||
                await devstarAPIHandler.verifyToken(access_token, devstar_username)
 | 
			
		||||
                  .then(async (res) => {
 | 
			
		||||
                const res = await this.user.login(access_token, devstar_username)
 | 
			
		||||
                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)
 | 
			
		||||
                                }
 | 
			
		||||
                              })
 | 
			
		||||
                          })
 | 
			
		||||
                } else {
 | 
			
		||||
                        // 公钥已上传,直接打开项目
 | 
			
		||||
                        await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
 | 
			
		||||
                  console.error(res)
 | 
			
		||||
                }
 | 
			
		||||
                    }
 | 
			
		||||
                  })
 | 
			
		||||
              } else if (devstar_username === this.user.getUsernameFromLocal()) {
 | 
			
		||||
                // 如果同用户已经登录,则忽略;
 | 
			
		||||
                // 直接打开项目
 | 
			
		||||
@@ -73,33 +52,13 @@ export class DevStarExtension {
 | 
			
		||||
                const selection = await vscode.window.showWarningMessage(`已登录用户:${this.user.getUsernameFromLocal()},是否切换用户?`,
 | 
			
		||||
                  'Yes', 'No',);
 | 
			
		||||
                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') {
 | 
			
		||||
                      // 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)
 | 
			
		||||
                                }
 | 
			
		||||
                              })
 | 
			
		||||
                          })
 | 
			
		||||
                  } else {
 | 
			
		||||
                        // 公钥已上传,直接打开项目
 | 
			
		||||
                        await this.remoteContainer.firstOpenProject(container_host, container_port, container_username, project_path)
 | 
			
		||||
                    console.error(res)
 | 
			
		||||
                  }
 | 
			
		||||
                    }
 | 
			
		||||
                  })
 | 
			
		||||
                } else if (selection === 'No') {
 | 
			
		||||
                  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 os from 'os';
 | 
			
		||||
import * as fs from 'fs';
 | 
			
		||||
import DevstarAPIHandler from './devstar-api';
 | 
			
		||||
const {
 | 
			
		||||
  generateKeyPairSync,
 | 
			
		||||
  createHash
 | 
			
		||||
@@ -22,7 +23,7 @@ export default class User {
 | 
			
		||||
    this.userToken = this.context.globalState.get(this.userTokenKey);
 | 
			
		||||
 | 
			
		||||
    // 提取devstar domain的主域名,用于本地ssh key的命名
 | 
			
		||||
    let devstarDomainFromConfig: string|undefined;
 | 
			
		||||
    let devstarDomainFromConfig: string | undefined;
 | 
			
		||||
    let devstarDomainURL: string;
 | 
			
		||||
    devstarDomainFromConfig = vscode.workspace.getConfiguration('devstar').get('devstarDomain')
 | 
			
		||||
    // 如果没有配置devstar domain,则默认domain为https://devstar.cn
 | 
			
		||||
@@ -31,6 +32,43 @@ export default class User {
 | 
			
		||||
    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() {
 | 
			
		||||
    var existUsername = false;
 | 
			
		||||
    var existUserToken = false;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user