1
- import { AuthSession } from '@/auth/session'
2
- import { genCodePair } from '@/services/code-challenge.service'
1
+ import { AuthSession } from '@/auth/auth- session'
2
+ import { genVerifyChallengePair } from '@/services/code-challenge.service'
3
3
import { isArray , isUndefined } from 'lodash-es'
4
4
import {
5
5
authentication ,
@@ -14,9 +14,9 @@ import {
14
14
Uri ,
15
15
window ,
16
16
} from 'vscode'
17
- import { globalCtx } from '@/services/global-state '
17
+ import { globalCtx } from '@/services/global-ctx '
18
18
import RandomString from 'randomstring'
19
- import { OauthApi } from '@/services/oauth.api'
19
+ import { Oauth } from '@/services/oauth.api'
20
20
import extensionUriHandler from '@/utils/uri-handler'
21
21
import { AccountInfo } from '@/auth/account-info'
22
22
import { TokenInfo } from '@/models/token-info'
@@ -35,14 +35,16 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
35
35
protected readonly allScopes = globalCtx . config . oauth . scope . split ( ' ' )
36
36
37
37
private _allSessions ?: AuthSession [ ] | null
38
- private _oauthClient ?: OauthApi | null
38
+
39
39
private readonly _sessionChangeEmitter = new EventEmitter < VscAuthProviderAuthSessionChEv > ( )
40
40
private readonly _disposable = Disposable . from (
41
41
this . _sessionChangeEmitter ,
42
42
authentication . registerAuthenticationProvider ( AuthProvider . providerId , AuthProvider . providerName , this , {
43
43
supportsMultipleAccounts : false ,
44
44
} ) ,
45
- this . onDidChangeSessions ( ( ) => ( this . _allSessions = null ) )
45
+ this . onDidChangeSessions ( ( ) => {
46
+ this . _allSessions = null
47
+ } )
46
48
)
47
49
48
50
static get instance ( ) {
@@ -55,7 +57,7 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
55
57
}
56
58
57
59
protected get context ( ) {
58
- return globalCtx . extensionContext
60
+ return globalCtx . extCtx
59
61
}
60
62
61
63
protected get secretStorage ( ) {
@@ -66,11 +68,6 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
66
68
return globalCtx . config
67
69
}
68
70
69
- protected get oauthClient ( ) {
70
- this . _oauthClient ??= new OauthApi ( )
71
- return this . _oauthClient
72
- }
73
-
74
71
async getSessions ( scopes ?: readonly string [ ] | undefined ) : Promise < readonly AuthSession [ ] > {
75
72
const sessions = await this . getAllSessions ( )
76
73
const parsedScopes = this . ensureScopes ( scopes )
@@ -99,7 +96,7 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
99
96
cancelTokenSrc . cancel ( )
100
97
} , 30 * 60 * 1000 ) // 30 min
101
98
102
- const codeVerifier = this . signInWithBrowser ( { scopes : parsedScopes } )
99
+ const verifyCode = this . signInWithBrowser ( { scopes : parsedScopes } )
103
100
104
101
return window . withProgress < AuthSession > ( options , async ( progress , cancelToken ) => {
105
102
progress . report ( { message : '等待用户在浏览器中进行授权...' } )
@@ -121,15 +118,10 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
121
118
122
119
progress . report ( { message : '已获得授权, 正在获取令牌...' } )
123
120
124
- this . oauthClient
125
- . fetchToken ( {
126
- codeVerifier,
127
- authorizationCode,
128
- cancellationToken : cancelTokenSrc . token ,
129
- } )
121
+ Oauth . fetchToken ( verifyCode , authorizationCode , cancelTokenSrc . token )
130
122
. then ( token =>
131
123
this . onAccessTokenGranted ( token , {
132
- cancellationToken : cancelTokenSrc . token ,
124
+ cancelToken : cancelTokenSrc . token ,
133
125
onStateChange ( state ) {
134
126
progress . report ( { message : state } )
135
127
} ,
@@ -185,15 +177,15 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
185
177
}
186
178
187
179
private signInWithBrowser ( { scopes } : { scopes : readonly string [ ] } ) {
188
- const { codeVerifier , codeChallenge } = genCodePair ( )
180
+ const [ verifyCode , challengeCode ] = genVerifyChallengePair ( )
189
181
const { clientId, responseType, authorizeEndpoint, authority, clientSecret } = this . config . oauth
190
182
191
183
const search = new URLSearchParams ( [
192
184
[ 'client_id' , clientId ] ,
193
185
[ 'response_type' , responseType ] ,
194
186
[ 'redirect_uri' , globalCtx . extensionUrl ] ,
195
187
[ 'nonce' , RandomString . generate ( 32 ) ] ,
196
- [ 'code_challenge' , codeChallenge ] ,
188
+ [ 'code_challenge' , challengeCode ] ,
197
189
[ 'code_challenge_method' , 'S256' ] ,
198
190
[ 'scope' , scopes . join ( ' ' ) ] ,
199
191
[ 'client_secret' , clientSecret ] ,
@@ -204,7 +196,7 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
204
196
console . warn
205
197
)
206
198
207
- return codeVerifier
199
+ return verifyCode
208
200
}
209
201
210
202
private ensureScopes (
@@ -219,17 +211,17 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
219
211
private async onAccessTokenGranted (
220
212
{ accessToken, refreshToken } : TokenInfo ,
221
213
{
222
- cancellationToken ,
214
+ cancelToken ,
223
215
onStateChange,
224
216
shouldFireSessionAddedEvent = true ,
225
217
} : {
226
218
onStateChange ?: ( state : string ) => void
227
- cancellationToken ?: CancellationToken
219
+ cancelToken ?: CancellationToken
228
220
shouldFireSessionAddedEvent ?: boolean
229
221
} = { }
230
222
) {
231
223
const ifNotCancelledThen = < TR > ( f : ( ) => TR ) : TR | undefined => {
232
- if ( cancellationToken ?. isCancellationRequested ) return
224
+ if ( cancelToken ?. isCancellationRequested ) return
233
225
return f ( )
234
226
}
235
227
@@ -238,11 +230,7 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
238
230
try {
239
231
onStateChange ?.( '正在获取账户信息...' )
240
232
241
- const spec = await ifNotCancelledThen ( ( ) =>
242
- this . oauthClient . fetchUserInfo ( accessToken , {
243
- cancellationToken : cancellationToken ,
244
- } )
245
- )
233
+ const spec = await ifNotCancelledThen ( ( ) => Oauth . fetchUserInfo ( accessToken , cancelToken ) )
246
234
247
235
onStateChange ?.( '即将完成...' )
248
236
@@ -277,7 +265,7 @@ export class AuthProvider implements AuthenticationProvider, Disposable {
277
265
} )
278
266
} )
279
267
} finally {
280
- if ( session != null && cancellationToken ?. isCancellationRequested ) await this . removeSession ( session . id )
268
+ if ( session != null && cancelToken ?. isCancellationRequested ) await this . removeSession ( session . id )
281
269
}
282
270
283
271
if ( session == null ) throw new Error ( 'Failed to create session' )
0 commit comments