-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathTabnineAuthenticationProvider.ts
191 lines (155 loc) · 4.69 KB
/
TabnineAuthenticationProvider.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import {
authentication,
AuthenticationProvider,
AuthenticationProviderAuthenticationSessionsChangeEvent,
AuthenticationSession,
commands,
Disposable,
Event,
EventEmitter,
} from "vscode";
import { once, EventEmitter as Emitter } from "events";
import { once as runOnce } from "underscore";
import { State } from "../binary/state";
import { BRAND_NAME } from "../globals/consts";
import { sleep } from "../utils/utils";
import { callForLogin, callForLogout } from "./authentication.api";
import TabnineSession from "./TabnineSession";
import BINARY_STATE from "../binary/binaryStateSingleton";
import { deriveNonNullState } from "../state/deriveState";
const LOGIN_HAPPENED_EVENT = "loginHappened";
type UserAuthData = {
username: string;
accessToken: string;
};
type AuthStateData = {
current: UserAuthData | null;
last: UserAuthData | null;
};
const AUTH_INITIAL_STATE = {
last: null,
current: null,
};
function toSession({ accessToken, username }: UserAuthData): TabnineSession {
return new TabnineSession(username, accessToken);
}
const setAuthenticationReadyOnce = runOnce(setAuthenticationReady);
export default class TabnineAuthenticationProvider
implements AuthenticationProvider, Disposable {
public readonly id: string = BRAND_NAME;
public readonly label: string = BRAND_NAME;
private initializedDisposable: Disposable | undefined;
private onDidLogin = new Emitter();
private sessionsChangeEventEmitter = new EventEmitter<AuthenticationProviderAuthenticationSessionsChangeEvent>();
private authState = deriveNonNullState(
BINARY_STATE,
calculateAuthState,
AUTH_INITIAL_STATE
);
constructor() {
this.initializedDisposable = Disposable.from(
this.authState,
this.onDerivedAuthStateChanged(),
listenForSessionChangeFromVscode()
);
}
get onDidChangeSessions(): Event<AuthenticationProviderAuthenticationSessionsChangeEvent> {
return this.sessionsChangeEventEmitter.event;
}
getSessions(): Promise<readonly AuthenticationSession[]> {
const userData = this.authState.get().current;
return Promise.resolve(userData ? [toSession(userData)] : []);
}
async createSession(): Promise<AuthenticationSession> {
await callForLogin();
const userAuth = await this.waitForLogin();
return toSession(userAuth);
}
private async waitForLogin(): Promise<UserAuthData> {
return ((await once(this.onDidLogin, LOGIN_HAPPENED_EVENT)) as [
UserAuthData
])[0];
}
// eslint-disable-next-line class-methods-use-this
async removeSession(): Promise<void> {
await callForLogout();
await sleep(5000);
}
dispose(): void {
this.initializedDisposable?.dispose();
}
private onDerivedAuthStateChanged(): Disposable {
return this.authState.onChange(async ({ current, last }) => {
await setAuthenticationReadyOnce();
if (current && !last) {
this.onDidLogin.emit(LOGIN_HAPPENED_EVENT, current);
}
if (!current) {
await clearSessionPreference();
}
await setAuthenticationState(Boolean(current));
this.notifyVscodeOfAuthStateChanges(current, last);
});
}
private notifyVscodeOfAuthStateChanges(
current: UserAuthData | null,
last: UserAuthData | null
) {
if (!last && current) {
this.sessionsChangeEventEmitter.fire({
added: [toSession(current)],
});
}
if (last && !current) {
this.sessionsChangeEventEmitter.fire({
removed: [toSession(last)],
});
}
if (last && current) {
this.sessionsChangeEventEmitter.fire({
removed: [toSession(last)],
added: [toSession(current)],
});
}
}
}
async function clearSessionPreference() {
await authentication.getSession(BRAND_NAME, [], {
clearSessionPreference: true,
});
}
function listenForSessionChangeFromVscode(): Disposable {
// This fires when the user initiates a "silent" auth flow via the Accounts menu.
return authentication.onDidChangeSessions((e) => {
if (e.provider.id === BRAND_NAME) {
void BINARY_STATE.checkForUpdates();
}
});
}
function calculateAuthState(binartState: State, value: AuthStateData) {
const newValue: AuthStateData = {
last: value.current,
current: null,
};
if (binartState.is_logged_in) {
newValue.current = {
accessToken: binartState.access_token || "",
username: binartState.user_name,
};
}
return newValue;
}
async function setAuthenticationState(authenticated: boolean) {
return commands.executeCommand(
"setContext",
"tabnine.authenticated",
authenticated
);
}
async function setAuthenticationReady() {
return commands.executeCommand(
"setContext",
"tabnine.authentication.ready",
true
);
}