-
Notifications
You must be signed in to change notification settings - Fork 61
/
InternetAccountModel.ts
286 lines (282 loc) · 7.95 KB
/
InternetAccountModel.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
import React from 'react'
import { Instance, types } from 'mobx-state-tree'
import { ConfigurationReference, getConf } from '../../configuration'
import { RemoteFileWithRangeCache } from '../../util/io'
import { ElementId } from '../../util/types/mst'
import { UriLocation, AnyReactComponentType } from '../../util/types'
import { BaseInternetAccountConfig } from './baseInternetAccountConfig'
const inWebWorker = typeof sessionStorage === 'undefined'
/**
* #stateModel BaseInternetAccountModel
* #category internetAccount
*/
function x() {} // eslint-disable-line @typescript-eslint/no-unused-vars
export const InternetAccount = types
.model('InternetAccount', {
/**
* #property
*/
id: ElementId,
/**
* #property
*/
type: types.string,
/**
* #property
*/
configuration: ConfigurationReference(BaseInternetAccountConfig),
})
.views(self => ({
/**
* #getter
*/
get name(): string {
return getConf(self, 'name')
},
/**
* #getter
*/
get description(): string {
return getConf(self, 'description')
},
/**
* #getter
*/
get internetAccountId(): string {
return getConf(self, 'internetAccountId') // NOTE: this is the explicitIdentifier of the config schema
},
/**
* #getter
*/
get authHeader(): string {
return getConf(self, 'authHeader')
},
/**
* #getter
*/
get tokenType(): string {
return getConf(self, 'tokenType')
},
/**
* #getter
*/
get domains(): string[] {
return getConf(self, 'domains')
},
/**
* #getter
* Can use this to customize what is displayed in fileSelector's toggle box
*/
get toggleContents(): React.ReactNode {
return null
},
/**
* #getter
* Can use this to customize what the fileSelector. It takes a prop called
* `setLocation` that should be used to set a UriLocation
*/
get SelectorComponent(): AnyReactComponentType | undefined {
return undefined
},
/**
* #getter
* Can use this to add a label to the UrlChooser. Has no effect if a custom
* SelectorComponent is supplied
*/
get selectorLabel(): string | undefined {
return undefined
},
}))
.views(self => ({
/**
* #method
* Determine whether this internetAccount provides credentials for a URL
* @param location - UriLocation of resource
* @returns true or false
*/
handlesLocation(location: UriLocation) {
return self.domains.some(domain => location.uri.includes(domain))
},
/**
* #getter
* The key used to store this internetAccount's token in sessionStorage
*/
get tokenKey() {
return `${self.internetAccountId}-token`
},
}))
.actions(self => ({
/**
* #action
* Must be implemented by a model extending or composing this one. Pass the
* user's token to `resolve`.
* @param resolve - Pass the token to this function
* @param reject - If there is an error getting the token, call this function
*/
getTokenFromUser(
_resolve: (token: string) => void,
_reject: (error: Error) => void,
): void {
throw new Error('getTokenFromUser must be implemented by extending model')
},
/**
* #action
*/
storeToken(token: string) {
sessionStorage.setItem(self.tokenKey, token)
},
/**
* #action
*/
removeToken() {
sessionStorage.removeItem(self.tokenKey)
},
/**
* #action
*/
retrieveToken() {
return sessionStorage.getItem(self.tokenKey)
},
/**
* #action
* This can be used by an internetAccount to validate a token works before
* it is used. This is run when preAuthorizationInformation is requested,
* so it can be used to check that a token is valid before sending it to a
* worker thread. It expects the token to be returned so that this action
* can also be used to generate a new token (e.g. by using a refresh token)
* if the original one was invalid. Should throw an error if a token is
* invalid.
*
* @param token - Auth token
* @param loc - UriLocation of the resource
* @returns - Valid auth token
*/
async validateToken(token: string, _loc: UriLocation) {
return token
},
}))
.actions(self => {
let tokenPromise: Promise<string> | undefined = undefined
return {
/**
* #action
* Try to get the token from the location pre-auth, from local storage,
* or from a previously cached promise. If token is not available, uses
* `getTokenFromUser`.
*
* @param location - UriLocation of the resource
* @returns A promise for the token
*/
async getToken(location?: UriLocation): Promise<string> {
if (tokenPromise) {
return tokenPromise
}
let token = location?.internetAccountPreAuthorization?.authInfo?.token
if (token) {
tokenPromise = Promise.resolve(token)
return tokenPromise
}
if (inWebWorker) {
throw new Error(
'Did not get internet account pre-authorization info in worker',
)
}
token = self.retrieveToken()
if (token) {
tokenPromise = Promise.resolve(token)
return tokenPromise
}
tokenPromise = new Promise((resolve, reject) => {
self.getTokenFromUser(
token => {
self.storeToken(token)
resolve(token)
},
error => {
self.removeToken()
reject(error)
},
)
})
return tokenPromise
},
}
})
.actions(self => ({
/**
* #action
*/
addAuthHeaderToInit(init?: RequestInit, token?: string) {
return {
...init,
headers: new Headers({
...init?.headers,
...(token
? {
[self.authHeader]: self.tokenType
? `${self.tokenType} ${token}`
: token,
}
: {}),
}),
}
},
/**
* #action
* Gets the token and returns it along with the information needed to
* create a new internetAccount.
*
* @param location - UriLocation of the resource
* @returns
*/
async getPreAuthorizationInformation(location: UriLocation) {
const authToken = await self.getToken(location)
let validatedToken: string | undefined
try {
validatedToken = await self.validateToken(authToken, location)
} catch (error) {
self.removeToken()
throw error
}
return {
internetAccountType: self.type,
authInfo: {
token: validatedToken,
configuration: getConf(self),
},
}
},
}))
.actions(self => ({
/**
* #action
* Get a fetch method that will add any needed authentication headers to
* the request before sending it. If location is provided, it will be
* checked to see if it includes a token in it pre-auth information.
*
* @param loc - UriLocation of the resource
* @returns A function that can be used to fetch
*/
getFetcher(loc?: UriLocation) {
return async (input: RequestInfo, init?: RequestInit) => {
const authToken = await self.getToken(loc)
const newInit = self.addAuthHeaderToInit(init, authToken)
return fetch(input, newInit)
}
},
}))
.actions(self => ({
/**
* #action
* Gets a filehandle that uses a fetch that adds auth headers
* @param location - UriLocation of the resource
* @returns A filehandle
*/
openLocation(location: UriLocation) {
return new RemoteFileWithRangeCache(location.uri, {
fetch: self.getFetcher(location),
})
},
}))
export type BaseInternetAccountStateModel = typeof InternetAccount
export type BaseInternetAccountModel = Instance<BaseInternetAccountStateModel>