@@ -13,6 +13,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1313} ;
1414Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
1515const os_1 = __importDefault ( require ( "os" ) ) ;
16+ const fs_1 = __importDefault ( require ( "fs" ) ) ;
17+ const path_1 = __importDefault ( require ( "path" ) ) ;
1618const http_1 = __importDefault ( require ( "http" ) ) ;
1719const crypto_1 = __importDefault ( require ( "crypto" ) ) ;
1820const portfinder_1 = __importDefault ( require ( "portfinder" ) ) ;
@@ -26,7 +28,9 @@ const constant_1 = require("../constant");
2628const logger_1 = __importDefault ( require ( "../logger" ) ) ;
2729const utils_1 = require ( "../utils" ) ;
2830const error_1 = require ( "../error" ) ;
31+ const cloud_api_request_1 = require ( "./cloud-api-request" ) ;
2932const logger = new logger_1 . default ( 'Auth' ) ;
33+ const tcbService = new cloud_api_request_1 . CloudApiService ( 'tcb' ) ;
3034const defaultPort = 9012 ;
3135const CliAuthBaseUrl = 'https://console.cloud.tencent.com/tcb/auth' ;
3236const refreshTokenUrl = 'https://iaas.cloud.tencent.com/tcb_refresh' ;
@@ -136,6 +140,30 @@ function createLocalServer() {
136140 } ) ;
137141 } ) ;
138142}
143+ function respondWithFile ( req , res , statusCode , filename ) {
144+ return new Promise ( function ( resolve , reject ) {
145+ fs_1 . default . readFile ( path_1 . default . join ( __dirname , '../../templates' , filename ) , function ( err , response ) {
146+ if ( err ) {
147+ return reject ( err ) ;
148+ }
149+ res . writeHead ( statusCode , {
150+ 'Content-Length' : response . length ,
151+ 'Content-Type' : 'text/html'
152+ } ) ;
153+ res . end ( response ) ;
154+ req . socket . destroy ( ) ;
155+ return resolve ( ) ;
156+ } ) ;
157+ } ) ;
158+ }
159+ function checkAuth ( credential ) {
160+ return __awaiter ( this , void 0 , void 0 , function * ( ) {
161+ const { tmpSecretId, tmpSecretKey, tmpToken } = credential ;
162+ tcbService . setCredential ( tmpSecretId , tmpSecretKey , tmpToken ) ;
163+ return tcbService . request ( 'DescribeEnvs' ) ;
164+ } ) ;
165+ }
166+ exports . checkAuth = checkAuth ;
139167function getAuthTokenFromWeb ( options ) {
140168 return __awaiter ( this , void 0 , void 0 , function * ( ) {
141169 const { getAuthUrl } = options ;
@@ -161,16 +189,32 @@ function getAuthTokenFromWeb(options) {
161189 loading . succeed ( '已打开云开发 CLI 授权页面,请在云开发 CLI 授权页面同意授权!' ) ;
162190 return new Promise ( resolve => {
163191 server . on ( 'request' , ( req , res ) => {
164- var _a ;
192+ var _a , _b ;
165193 const { url } = req ;
166194 const { query } = query_string_1 . default . parseUrl ( url ) ;
195+ if ( ( _a = query ) === null || _a === void 0 ? void 0 : _a . html ) {
196+ return checkAuth ( query )
197+ . then ( ( ) => {
198+ return respondWithFile ( req , res , 200 , 'html/loginSuccess.html' ) ;
199+ } )
200+ . then ( ( ) => {
201+ server . close ( ) ;
202+ resolve ( query ) ;
203+ } )
204+ . catch ( e => {
205+ server . close ( ) ;
206+ return respondWithFile ( req , res , 502 , 'html/loginFail.html' ) ;
207+ } ) ;
208+ }
167209 res . writeHead ( 200 , {
168210 'Access-Control-Allow-Origin' : '*' ,
211+ 'Access-Control-Allow-Methods' : '*' ,
212+ 'Access-Control-Allow-Headers' : '*' ,
169213 'Content-Type' : 'text/plain' ,
170214 Connection : 'close'
171215 } ) ;
172- res . end ( 'ok' ) ;
173- if ( ( _a = query ) === null || _a === void 0 ? void 0 : _a . tmpToken ) {
216+ res . end ( ) ;
217+ if ( ( _b = query ) === null || _b === void 0 ? void 0 : _b . tmpToken ) {
174218 server . close ( ) ;
175219 }
176220 resolve ( query ) ;
0 commit comments