@@ -4,6 +4,7 @@ import { nanoid } from 'nanoid';
4
4
import { keychain } from './common/keychain' ;
5
5
import Logger from './common/logger'
6
6
import { CodingServer } from './codingServer' ;
7
+ import { RepoInfo } from './typings/types' ;
7
8
8
9
interface SessionData {
9
10
id : string ;
@@ -30,10 +31,16 @@ export const onDidChangeSessions = new vscode.EventEmitter<vscode.Authentication
30
31
export class CodingAuthenticationProvider {
31
32
private _sessions : vscode . AuthenticationSession [ ] = [ ] ;
32
33
private _codingServer = new CodingServer ( ) ;
33
- private _team : string ;
34
-
35
- public constructor ( team : string ) {
36
- this . _team = team ;
34
+ private _repo : RepoInfo = {
35
+ team : `` ,
36
+ project : `` ,
37
+ repo : `` ,
38
+ } ;
39
+
40
+ public constructor ( repo : RepoInfo | null ) {
41
+ if ( repo ) {
42
+ this . _repo = repo ;
43
+ }
37
44
}
38
45
39
46
public async initialize ( context : vscode . ExtensionContext ) : Promise < void > {
@@ -47,45 +54,45 @@ export class CodingAuthenticationProvider {
47
54
}
48
55
49
56
private async _checkForUpdates ( ) {
50
- let storedSessions : vscode . AuthenticationSession [ ] ;
51
- try {
52
- storedSessions = await this . _readSessions ( ) ;
53
- } catch ( e ) {
54
- // Ignore, network request failed
55
- return ;
56
- }
57
-
58
- const added : string [ ] = [ ] ;
59
- const removed : string [ ] = [ ] ;
60
-
61
- storedSessions . forEach ( session => {
62
- const matchesExisting = this . _sessions . some ( s => s . id === session . id ) ;
63
- // Another window added a session to the keychain, add it to our state as well
64
- if ( ! matchesExisting ) {
65
- Logger . info ( 'Adding session found in keychain' ) ;
66
- this . _sessions . push ( session ) ;
67
- added . push ( session . id ) ;
68
- }
69
- } ) ;
70
-
71
- this . _sessions . map ( session => {
72
- const matchesExisting = storedSessions . some ( s => s . id === session . id ) ;
73
- // Another window has logged out, remove from our state
74
- if ( ! matchesExisting ) {
75
- Logger . info ( 'Removing session no longer found in keychain' ) ;
76
- const sessionIndex = this . _sessions . findIndex ( s => s . id === session . id ) ;
77
- if ( sessionIndex > - 1 ) {
78
- this . _sessions . splice ( sessionIndex , 1 ) ;
79
- }
80
-
81
- removed . push ( session . id ) ;
82
- }
83
- } ) ;
84
-
85
- if ( added . length || removed . length ) {
86
- onDidChangeSessions . fire ( { added, removed, changed : [ ] } ) ;
87
- }
88
- }
57
+ let storedSessions : vscode . AuthenticationSession [ ] ;
58
+ try {
59
+ storedSessions = await this . _readSessions ( ) ;
60
+ } catch ( e ) {
61
+ // Ignore, network request failed
62
+ return ;
63
+ }
64
+
65
+ const added : string [ ] = [ ] ;
66
+ const removed : string [ ] = [ ] ;
67
+
68
+ storedSessions . forEach ( session => {
69
+ const matchesExisting = this . _sessions . some ( s => s . id === session . id ) ;
70
+ // Another window added a session to the keychain, add it to our state as well
71
+ if ( ! matchesExisting ) {
72
+ Logger . info ( 'Adding session found in keychain' ) ;
73
+ this . _sessions . push ( session ) ;
74
+ added . push ( session . id ) ;
75
+ }
76
+ } ) ;
77
+
78
+ this . _sessions . map ( session => {
79
+ const matchesExisting = storedSessions . some ( s => s . id === session . id ) ;
80
+ // Another window has logged out, remove from our state
81
+ if ( ! matchesExisting ) {
82
+ Logger . info ( 'Removing session no longer found in keychain' ) ;
83
+ const sessionIndex = this . _sessions . findIndex ( s => s . id === session . id ) ;
84
+ if ( sessionIndex > - 1 ) {
85
+ this . _sessions . splice ( sessionIndex , 1 ) ;
86
+ }
87
+
88
+ removed . push ( session . id ) ;
89
+ }
90
+ } ) ;
91
+
92
+ if ( added . length || removed . length ) {
93
+ onDidChangeSessions . fire ( { added, removed, changed : [ ] } ) ;
94
+ }
95
+ }
89
96
90
97
private async _readSessions ( ) : Promise < vscode . AuthenticationSession [ ] > {
91
98
const storedSessions = await keychain . getToken ( ) || await keychain . tryMigrate ( ) ;
@@ -96,7 +103,7 @@ export class CodingAuthenticationProvider {
96
103
const needsUserInfo = ! session . account ;
97
104
let userInfo : { id : string , accountName : string } ;
98
105
if ( needsUserInfo ) {
99
- userInfo = await this . _codingServer . getUserInfo ( this . _team , session . accessToken ) ;
106
+ userInfo = await this . _codingServer . getUserInfo ( this . _repo . team , session . accessToken ) ;
100
107
}
101
108
102
109
return {
@@ -126,8 +133,8 @@ export class CodingAuthenticationProvider {
126
133
return [ ] ;
127
134
}
128
135
129
- private async _tokenToSession ( team : string , token : string , scopes : string [ ] ) : Promise < vscode . AuthenticationSession > {
130
- const userInfo = await this . _codingServer . getUserInfo ( team , token ) ;
136
+ private async _tokenToSession ( token : string , scopes : string [ ] ) : Promise < vscode . AuthenticationSession > {
137
+ const userInfo = await this . _codingServer . getUserInfo ( this . _repo . team , token ) ;
131
138
132
139
return {
133
140
id : nanoid ( ) ,
@@ -142,7 +149,7 @@ export class CodingAuthenticationProvider {
142
149
143
150
public async login ( team : string , scopes : string = SCOPES ) : Promise < vscode . AuthenticationSession > {
144
151
const { access_token : token } = await this . _codingServer . login ( team , scopes ) ;
145
- const session = await this . _tokenToSession ( team , token , scopes . split ( ' ' ) ) ;
152
+ const session = await this . _tokenToSession ( token , scopes . split ( ' ' ) ) ;
146
153
await this . _setToken ( session ) ;
147
154
return session ;
148
155
}
@@ -163,16 +170,16 @@ export class CodingAuthenticationProvider {
163
170
}
164
171
165
172
// @ts -ignore
166
- get sessions ( ) : vscode . AuthenticationSession [ ] {
167
- return this . _sessions ;
173
+ get sessions ( ) : vscode . AuthenticationSession [ ] {
174
+ return this . _sessions ;
168
175
}
169
-
176
+
170
177
public async logout ( id : string ) {
171
- const sessionIndex = this . _sessions . findIndex ( session => session . id === id ) ;
172
- if ( sessionIndex > - 1 ) {
173
- this . _sessions . splice ( sessionIndex , 1 ) ;
174
- }
178
+ const sessionIndex = this . _sessions . findIndex ( session => session . id === id ) ;
179
+ if ( sessionIndex > - 1 ) {
180
+ this . _sessions . splice ( sessionIndex , 1 ) ;
181
+ }
175
182
176
- await this . _storeSessions ( ) ;
177
- }
183
+ await this . _storeSessions ( ) ;
184
+ }
178
185
}
0 commit comments