- Notifications
You must be signed in to change notification settings - Fork 440
/
Copy pathauthentication-client-service.ts
99 lines (86 loc) · 3.31 KB
/
authentication-client-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import{inject,injectable}from'@theia/core/shared/inversify';
import{Emitter}from'@theia/core/lib/common/event';
import{JsonRpcProxy}from'@theia/core/lib/common/messaging/proxy-factory';
import{WindowService}from'@theia/core/lib/browser/window/window-service';
import{DisposableCollection}from'@theia/core/lib/common/disposable';
import{FrontendApplicationContribution}from'@theia/core/lib/browser/frontend-application-contribution';
import{
CommandRegistry,
CommandContribution,
}from'@theia/core/lib/common/command';
import{
AuthOptions,
AuthenticationService,
AuthenticationServiceClient,
AuthenticationSession,
authServerPort,
}from'../../common/protocol/authentication-service';
import{CloudUserCommands}from'./cloud-user-commands';
import{ArduinoPreferences}from'../arduino-preferences';
@injectable()
exportclassAuthenticationClientService
implements
FrontendApplicationContribution,
CommandContribution,
AuthenticationServiceClient
{
@inject(AuthenticationService)
protectedreadonlyservice: JsonRpcProxy<AuthenticationService>;
@inject(WindowService)
protectedreadonlywindowService: WindowService;
@inject(ArduinoPreferences)
protectedreadonlyarduinoPreferences: ArduinoPreferences;
protectedauthOptions: AuthOptions;
protected_session: AuthenticationSession|undefined;
protectedreadonlytoDispose=newDisposableCollection();
protectedreadonlyonSessionDidChangeEmitter=newEmitter<
AuthenticationSession|undefined
>();
readonlyonSessionDidChange=this.onSessionDidChangeEmitter.event;
asynconStart(): Promise<void>{
this.toDispose.push(this.onSessionDidChangeEmitter);
this.service.setClient(this);
this.service
.session()
.then((session)=>this.notifySessionDidChange(session));
this.setOptions().then(()=>this.service.initAuthSession());
this.arduinoPreferences.onPreferenceChanged((event)=>{
if(event.preferenceName.startsWith('arduino.auth.')){
this.setOptions();
}
});
}
setOptions(): Promise<void>{
returnthis.service.setOptions({
redirectUri: `http://localhost:${authServerPort}/callback`,
responseType: 'code',
clientID: this.arduinoPreferences['arduino.auth.clientID'],
domain: this.arduinoPreferences['arduino.auth.domain'],
audience: this.arduinoPreferences['arduino.auth.audience'],
registerUri: this.arduinoPreferences['arduino.auth.registerUri'],
scopes: ['openid','profile','email','offline_access'],
});
}
protectedupdateSession(session?: AuthenticationSession|undefined){
this._session=session;
this.onSessionDidChangeEmitter.fire(this._session);
}
getsession(): AuthenticationSession|undefined{
returnthis._session;
}
registerCommands(registry: CommandRegistry): void{
registry.registerCommand(CloudUserCommands.LOGIN,{
execute: ()=>this.service.login(),
isEnabled: ()=>!this._session,
isVisible: ()=>!this._session,
});
registry.registerCommand(CloudUserCommands.LOGOUT,{
execute: ()=>this.service.logout(),
isEnabled: ()=>!!this._session,
isVisible: ()=>!!this._session,
});
}
notifySessionDidChange(session: AuthenticationSession|undefined): void{
this.updateSession(session);
}
}