11import { localize } from "vscode-nls-i18n" ;
2- import { ExtensionContext , workspace , ConfigurationTarget , window } from "vscode" ;
2+ import { ExtensionContext , workspace , ConfigurationTarget , window , ProgressLocation } from "vscode" ;
33
44import { container } from "../../container" ;
55import { Context } from "../../context" ;
66import { tree } from "../treeDataProvider" ;
77import { getCredentailByInput } from "./auth" ;
8+ import { AbstractClient } from "tencentcloud-sdk-nodejs/tencentcloud/common/abstract_client" ;
9+ import { Credential } from "tencentcloud-sdk-nodejs/tencentcloud/common/interface" ;
810import { LoginProvider } from "../../../views/login/loginExplorer" ;
911import { terraformShellManager } from "../../../client/terminal/terraformShellManager" ;
12+ import { getStsClient } from "@/connectivity/client" ;
13+ import { StreamingStatistics } from "tencentcloud-sdk-nodejs/tencentcloud/services/dlc/v20210125/dlc_models" ;
1014
1115export namespace user {
1216 interface UserInfo {
1317 secretId : string ;
1418 secretKey : string ;
1519 token ?: string ;
1620 uin : string ;
21+ subuin ?: string ;
22+ type ?: string ;
23+ appid ?: string ;
24+ region ?: string ;
1725 }
1826
1927 export const AKSK_TITLE = "TcTerraform.pickup.aksk" ;
@@ -28,6 +36,7 @@ export namespace user {
2836 const oauth = localize ( OAUTH_TITLE ) ;
2937 const pick = await window . showQuickPick ( [ aksk , oauth ] ) ;
3038
39+ // only support aksk way right now
3140 if ( aksk === pick ) {
3241 const credential = await getCredentailByInput ( ) ;
3342 const accessKey = credential . secretId ;
@@ -51,20 +60,37 @@ export namespace user {
5160 process . env . TENCENTCLOUD_SECRET_ID = accessKey ;
5261 process . env . TENCENTCLOUD_SECRET_KEY = secretKey ;
5362
54- tree . refreshTreeData ( ) ;
55- window . showInformationMessage ( localize ( "TcTerraform.login.success" ) ) ;
63+ // query user info
64+ const resp = await ( await getStsClient ( ) ) . GetCallerIdentity ( { } ) .
65+ then (
66+ ( result ) => {
67+ console . debug ( '[DEBUG]--------------------------------result:' , result ) ;
68+ if ( ! result ) {
69+ throw new Error ( '[Warn] GetCallerIdentity result.TotalCount is 0.' ) ;
70+ }
71+ return result ;
72+ } ,
73+ ( err ) => {
74+ console . error ( '[TencentCloudSDKError] GetCallerIdentity got a error from SDK.' , err . message ) ;
75+ window . showErrorMessage ( 'Login Failed. Reason:' + err . message ) ;
76+ return err ;
77+ }
78+ ) ;
79+
80+ // set user info
81+ let userinfo : UserInfo = {
82+ secretId : accessKey ,
83+ secretKey : secretKey ,
84+ uin : resp . PrincipalId ?? resp . UserId ?? "-" ,
85+ type : resp . Type ?? "unknow"
86+ } ;
87+ setInfo ( userinfo ) ;
88+
89+ // tree.refreshTreeData();
5690 }
57- }
58-
59- export async function getInfo ( ) : Promise < UserInfo | undefined > {
60- const { secrets } = container . get < ExtensionContext > ( Context ) ;
61- const userinfo = await secrets . get ( USER_INFO ) ;
62-
63- if ( userinfo ) {
64- return JSON . parse ( userinfo ) as UserInfo ;
91+ if ( oauth === pick ) {
92+ // to do
6593 }
66-
67- return undefined ;
6894 }
6995
7096 export async function loginOut ( ) {
@@ -86,6 +112,90 @@ export namespace user {
86112
87113 tree . refreshTreeData ( ) ;
88114 }
115+
116+ async function loginBySecret ( credential : Credential ) {
117+ const client = new AbstractClient (
118+ "open.test.tencentcloudapi.com" ,
119+ "2018-12-25" ,
120+ {
121+ credential,
122+ profile : {
123+ httpProfile : {
124+ proxy : "http://9.135.97.58:8899" ,
125+ } ,
126+ } ,
127+ }
128+ ) ;
129+ try {
130+ const res = await client . request ( "GetUserAuthInfo" , { } ) ;
131+
132+ const { Error : error , ...rest } = res ;
133+
134+ if ( error ) {
135+ const err = new Error ( error . Message ) ;
136+ err . stack = JSON . stringify ( error ) ;
137+
138+ return Promise . reject ( err ) ;
139+ }
140+
141+ return rest ;
142+ } catch ( e ) {
143+ throw e ;
144+ }
145+ }
146+
147+ async function loginByCredentail ( ) {
148+ let userInfo : UserInfo | undefined ;
149+ const credential = await getCredentailByInput ( ) ;
150+
151+ if ( credential ) {
152+ try {
153+ await window . withProgress (
154+ {
155+ title : localize ( "login.title" ) ,
156+ location : ProgressLocation . Notification ,
157+ } ,
158+ async ( ) => {
159+ const res = await loginBySecret ( credential ) ;
160+ if ( res ) {
161+ userInfo = {
162+ uin : res . Uin ,
163+ secretId : credential . secretId ,
164+ secretKey : credential . secretKey ,
165+ token : res . token ,
166+ } ;
167+ setInfo ( userInfo ) ;
168+ }
169+ }
170+ ) ;
171+ } catch ( error ) {
172+ console . error ( "loginByCredentail" , error ) ;
173+
174+ const message = error instanceof Error ? `: ${ error . message } ` : "" ;
175+ window . showErrorMessage ( localize ( "login.fail" , message ) ) ;
176+ }
177+ }
178+
179+ return userInfo ;
180+ }
181+
182+ async function setInfo ( info : UserInfo ) {
183+ const { secrets } = container . get < ExtensionContext > ( Context ) ;
184+
185+ await secrets . store ( USER_INFO , JSON . stringify ( info ) ) ;
186+ tree . refreshTreeData ( ) ;
187+ }
188+
189+ export async function getInfo ( ) : Promise < UserInfo | undefined > {
190+ const { secrets } = container . get < ExtensionContext > ( Context ) ;
191+ const userinfo = await secrets . get ( USER_INFO ) ;
192+
193+ if ( userinfo ) {
194+ return JSON . parse ( userinfo ) as UserInfo ;
195+ }
196+
197+ return undefined ;
198+ }
89199}
90200
91201export default user ;
0 commit comments