19
19
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
*--------------------------------------------------------------------------------------------*/
22
- import { AuthenticationSession , window , EventEmitter , AuthenticationProviderAuthenticationSessionsChangeEvent , env , Uri } from '@podman-desktop/api' ;
22
+ import { AuthenticationSession , window , EventEmitter , AuthenticationProviderAuthenticationSessionsChangeEvent , env , Uri , ExtensionContext , Disposable } from '@podman-desktop/api' ;
23
23
import { ServerResponse } from 'node:http' ;
24
24
import { Client , generators , Issuer , TokenSet } from 'openid-client' ;
25
25
import { createServer , startServer } from './authentication-server' ;
26
26
import { AuthConfig } from './configuration' ;
27
- import { Keychain } from './keychain' ;
28
27
import Logger from './logger' ;
29
28
30
29
interface IToken {
@@ -92,11 +91,11 @@ export class RedHatAuthenticationService {
92
91
private _tokens : IToken [ ] = [ ] ;
93
92
private _refreshTimeouts : Map < string , NodeJS . Timeout > = new Map < string , NodeJS . Timeout > ( ) ;
94
93
//private _uriHandler: UriEventHandler;
94
+ private _disposables : Disposable [ ] = [ ] ;
95
95
private client : Client ;
96
- private keychain : Keychain ;
97
96
private config : AuthConfig ;
98
97
99
- constructor ( issuer : Issuer < Client > , config : AuthConfig ) {
98
+ constructor ( issuer : Issuer < Client > , private context : ExtensionContext , config : AuthConfig ) {
100
99
//this._uriHandler = new UriEventHandler();
101
100
//this._disposables.push(vscode.window.registerUriHandler(this._uriHandler));
102
101
this . config = config ;
@@ -105,19 +104,18 @@ export class RedHatAuthenticationService {
105
104
response_types : [ 'code' ] ,
106
105
token_endpoint_auth_method : 'none' ,
107
106
} ) ;
108
- this . keychain = new Keychain ( config . serviceId ) ;
109
107
}
110
108
111
- public static async build ( config : AuthConfig ) : Promise < RedHatAuthenticationService > {
109
+ public static async build ( context : ExtensionContext , config : AuthConfig ) : Promise < RedHatAuthenticationService > {
112
110
Logger . info ( `Configuring ${ config . serviceId } {auth: ${ config . authUrl } , api: ${ config . apiUrl } }` ) ;
113
111
const issuer = await Issuer . discover ( config . authUrl ) ;
114
112
115
- const provider = new RedHatAuthenticationService ( issuer , config ) ;
113
+ const provider = new RedHatAuthenticationService ( issuer , context , config ) ;
116
114
return provider ;
117
115
}
118
116
119
117
public async initialize ( ) : Promise < void > {
120
- const storedData = await this . keychain . getToken ( ) ;
118
+ const storedData = await this . context . secrets . get ( this . config . serviceId ) ;
121
119
if ( storedData ) {
122
120
try {
123
121
const sessions = this . parseStoredData ( storedData ) ;
@@ -159,6 +157,10 @@ export class RedHatAuthenticationService {
159
157
Logger . info ( 'Failed to initialize stored data' ) ;
160
158
await this . clearSessions ( ) ;
161
159
}
160
+
161
+ this . _disposables . push ( this . context . secrets . onDidChange ( ( ) => {
162
+ this . checkForUpdates ( ) ;
163
+ } ) ) ;
162
164
}
163
165
}
164
166
@@ -167,7 +169,7 @@ export class RedHatAuthenticationService {
167
169
}
168
170
169
171
private async storeTokenData ( ) : Promise < void > {
170
- const serializedData : IStoredSession [ ] = this . _tokens . map ( token => {
172
+ const storedSessions : IStoredSession [ ] = this . _tokens . map ( token => {
171
173
return {
172
174
id : token . sessionId ,
173
175
refreshToken : token . refreshToken ,
@@ -176,13 +178,13 @@ export class RedHatAuthenticationService {
176
178
} ;
177
179
} ) ;
178
180
179
- await this . keychain . setToken ( JSON . stringify ( serializedData ) ) ;
181
+ await this . context . secrets . store ( this . config . serviceId , JSON . stringify ( storedSessions ) ) ;
180
182
}
181
183
182
184
private async checkForUpdates ( ) : Promise < void > {
183
185
const added : RedHatAuthenticationSession [ ] = [ ] ;
184
186
let removed : RedHatAuthenticationSession [ ] = [ ] ;
185
- const storedData = await this . keychain . getToken ( ) ;
187
+ const storedData = await this . context . secrets . get ( this . config . serviceId ) ;
186
188
if ( storedData ) {
187
189
try {
188
190
const sessions = this . parseStoredData ( storedData ) ;
@@ -394,6 +396,11 @@ export class RedHatAuthenticationService {
394
396
response . end ( ) ;
395
397
}
396
398
399
+ public dispose ( ) : void {
400
+ this . _disposables . forEach ( disposable => disposable . dispose ( ) ) ;
401
+ this . _disposables = [ ] ;
402
+ }
403
+
397
404
private async setToken ( token : IToken , scope : string ) : Promise < void > {
398
405
const existingTokenIndex = this . _tokens . findIndex ( t => t . sessionId === token . sessionId ) ;
399
406
if ( existingTokenIndex > - 1 ) {
@@ -546,7 +553,7 @@ export class RedHatAuthenticationService {
546
553
session = convertToSession ( token ) ;
547
554
}
548
555
if ( this . _tokens . length === 0 ) {
549
- await this . keychain . deleteToken ( ) ;
556
+ await this . context . secrets . delete ( this . config . serviceId ) ;
550
557
} else {
551
558
this . storeTokenData ( ) ;
552
559
}
@@ -556,7 +563,7 @@ export class RedHatAuthenticationService {
556
563
public async clearSessions ( ) {
557
564
Logger . info ( 'Logging out of all sessions' ) ;
558
565
this . _tokens = [ ] ;
559
- await this . keychain . deleteToken ( ) ;
566
+ await this . context . secrets . delete ( this . config . serviceId ) ;
560
567
561
568
this . _refreshTimeouts . forEach ( timeout => {
562
569
clearTimeout ( timeout ) ;
0 commit comments