-
-
Notifications
You must be signed in to change notification settings - Fork 431
/
Copy pathauthentication-service-impl.ts
93 lines (82 loc) · 2.56 KB
/
authentication-service-impl.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
import { injectable } from 'inversify';
import {
Disposable,
DisposableCollection,
} from '@theia/core/lib/common/disposable';
import { BackendApplicationContribution } from '@theia/core/lib/node/backend-application';
import {
AuthenticationService,
AuthenticationServiceClient,
AuthenticationSession,
} from '../../common/protocol/authentication-service';
import { ArduinoAuthenticationProvider } from './arduino-auth-provider';
import { AuthOptions } from './types';
@injectable()
export class AuthenticationServiceImpl
implements AuthenticationService, BackendApplicationContribution
{
protected readonly delegate = new ArduinoAuthenticationProvider();
protected readonly clients: AuthenticationServiceClient[] = [];
protected readonly toDispose = new DisposableCollection();
private initialized = false;
async onStart(): Promise<void> {
this.toDispose.pushAll([
this.delegate,
this.delegate.onDidChangeSessions(({ added, removed, changed }) => {
added?.forEach((session) =>
this.clients.forEach((client) =>
client.notifySessionDidChange(session)
)
);
changed?.forEach((session) =>
this.clients.forEach((client) =>
client.notifySessionDidChange(session)
)
);
removed?.forEach(() =>
this.clients.forEach((client) => client.notifySessionDidChange())
);
}),
Disposable.create(() =>
this.clients.forEach((client) => this.disposeClient(client))
),
]);
}
async initAuthSession(): Promise<void> {
if (!this.initialized) {
await this.delegate.init();
this.initialized = true;
}
}
setOptions(authOptions: AuthOptions): Promise<void> {
return this.delegate.setOptions(authOptions);
}
async login(): Promise<AuthenticationSession> {
return this.delegate.createSession();
}
async logout(): Promise<void> {
this.delegate.logout();
}
async session(): Promise<AuthenticationSession | undefined> {
const sessions = await this.delegate.getSessions();
return sessions[0];
}
dispose(): void {
this.toDispose.dispose();
}
setClient(client: AuthenticationServiceClient | undefined): void {
if (client) {
this.clients.push(client);
}
}
disposeClient(client: AuthenticationServiceClient) {
const index = this.clients.indexOf(client);
if (index === -1) {
console.warn(
'Could not dispose authentications service client. It was not registered. Skipping.'
);
return;
}
this.clients.splice(index, 1);
}
}