1
- import { AuthenticationSession as AuthSession } from 'vscode'
1
+ import { AuthenticationSession as AuthSession , AuthenticationSession } from 'vscode'
2
2
import { genVerifyChallengePair } from '@/service/code-challenge'
3
3
import {
4
4
authentication ,
@@ -48,6 +48,7 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
48
48
readonly providerName = '博客园Cnblogs'
49
49
50
50
private _allSessions : AuthSession [ ] = [ ]
51
+ private _usePat = false
51
52
52
53
private readonly _sessionChangeEmitter = new EventEmitter < APASCE > ( )
53
54
private readonly _disposable = Disposable . from (
@@ -64,6 +65,14 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
64
65
return this . _sessionChangeEmitter . event
65
66
}
66
67
68
+ useBrowser ( ) {
69
+ this . _usePat = false
70
+ }
71
+
72
+ usePat ( ) {
73
+ this . _usePat = true
74
+ }
75
+
67
76
async getSessions ( scopes ?: string [ ] ) : Promise < readonly AuthSession [ ] > {
68
77
const sessions = await this . getAllSessions ( )
69
78
const parsedScopes = this . ensureScopes ( scopes )
@@ -72,6 +81,25 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
72
81
}
73
82
74
83
createSession ( scopes : string [ ] ) {
84
+ return this . _usePat ? this . createSessionFromPat ( scopes ) : this . createSessionFromBrowser ( scopes )
85
+ }
86
+
87
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
88
+ async createSessionFromPat ( scopes : string [ ] ) {
89
+ const opt = {
90
+ title : '请输入您的个人访问令牌 (PAT)' ,
91
+ prompt : '可通过 https://account.cnblos.com/tokens 获取' ,
92
+ password : true ,
93
+ validator : ( value : string ) => ( value . length === 0 ? '个人访问令牌(PAT)不能为空' : '' ) ,
94
+ }
95
+
96
+ const pat = await window . showInputBox ( opt )
97
+ if ( ( pat ?? '' ) . length === 0 ) throw new Error ( '个人访问令牌(PAT)不能为空' )
98
+
99
+ return authProvider . onAccessTokenGranted ( pat ?? '' )
100
+ }
101
+
102
+ createSessionFromBrowser ( scopes : string [ ] ) {
75
103
const parsedScopes = this . ensureScopes ( scopes )
76
104
77
105
const cancelTokenSrc = new CancellationTokenSource ( )
@@ -93,7 +121,7 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
93
121
location : ProgressLocation . Notification ,
94
122
}
95
123
96
- return window . withProgress ( options , async ( progress , cancelToken ) => {
124
+ const session = window . withProgress ( options , async ( progress , cancelToken ) => {
97
125
progress . report ( { message : '等待用户在浏览器中进行授权...' } )
98
126
99
127
cancelToken . onCancellationRequested ( ( ) => cancelTokenSrc . cancel ( ) )
@@ -136,6 +164,8 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
136
164
137
165
return fut
138
166
} )
167
+
168
+ return session
139
169
}
140
170
141
171
async removeSession ( sessionId : string ) : Promise < void > {
@@ -161,12 +191,13 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
161
191
162
192
onStateChange ?.( '即将完成...' )
163
193
164
- const session = < AuthSession > {
194
+ const session = < AuthenticationSession > {
165
195
account : accountInfo ,
166
196
id : `${ this . providerId } -${ userInfo . space_user_id } ` ,
167
197
accessToken : token ,
168
198
scopes : this . ensureScopes ( null ) ,
169
199
}
200
+
170
201
await LocalState . setSecret ( ExtConst . EXT_SESSION_STORAGE_KEY , JSON . stringify ( [ session ] ) )
171
202
172
203
this . _sessionChangeEmitter . fire ( {
0 commit comments