forked from pryv/lib-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthController.js
More file actions
286 lines (251 loc) · 8.78 KB
/
AuthController.js
File metadata and controls
286 lines (251 loc) · 8.78 KB
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
/**
* @license
* [BSD-3-Clause](https://github.com/pryv/lib-js/blob/master/LICENSE)
*/
const utils = require('../utils');
const AuthStates = require('./AuthStates');
const Messages = require('./LoginMessages');
/**
* Controller for authentication flow
* @memberof pryv.Auth
*/
class AuthController {
/**
* Create an AuthController
* @param {AuthSettings} settings - Authentication settings
* @param {Service} service - Pryv service instance
* @param {CustomLoginButton} loginButton - Login button implementation
*/
constructor (settings, service, loginButton) {
this.settings = settings;
validateSettings.call(this, settings);
this.stateChangeListeners = [];
if (this.settings.onStateChange) {
this.stateChangeListeners.push(this.settings.onStateChange);
}
this.service = service;
// probably remove
this.languageCode = this.settings.authRequest.languageCode || 'en';
this.messages = Messages(this.languageCode);
this.loginButton = loginButton;
function validateSettings (settings) {
if (!settings) { throw new Error('settings cannot be null'); }
// -- settings
if (!settings.authRequest) { throw new Error('Missing settings.authRequest'); }
// -- Extract returnURL
settings.authRequest.returnURL =
this.getReturnURL(settings.authRequest.returnURL);
if (!settings.authRequest.requestingAppId) {
throw new Error('Missing settings.authRequest.requestingAppId');
}
if (!settings.authRequest.requestedPermissions) {
throw new Error('Missing settings.authRequest.requestedPermissions');
}
}
}
/**
* Initialize the auth controller. Call this right after instantiation.
* @returns {Promise<Service>} Promise resolving to the Service instance
*/
async init () {
this.serviceInfo = this.service.infoSync();
this.state = { status: AuthStates.LOADING };
this.assets = await loadAssets(this);
const loginButton = this.loginButton;
// initialize human interaction interface
if (loginButton != null) {
this.stateChangeListeners.push(loginButton.onStateChange.bind(loginButton));
// autologin needs cookies/storage implemented in human interaction interface
await checkAutoLogin(this);
}
// if auto login is not prompted
if (this.state.status !== AuthStates.AUTHORIZED) {
this.state = { status: AuthStates.INITIALIZED, serviceInfo: this.serviceInfo };
}
if (loginButton != null && loginButton.finishAuthProcessAfterRedirection != null) {
// @ts-ignore - this is a valid AuthController
await loginButton.finishAuthProcessAfterRedirection(this);
}
return this.service;
}
/**
* Stops poll for auth request
*/
stopAuthRequest (msg) {
this.state = { status: AuthStates.ERROR, message: msg };
}
/**
* Handle button click - triggers appropriate action based on current state
* @returns {Promise<void>}
*/
async handleClick () {
if (isAuthorized.call(this)) {
this.state = { status: AuthStates.SIGNOUT };
} else if (isInitialized.call(this)) {
this.startAuthRequest();
} else if (isNeedSignIn.call(this)) {
// reopen popup (HACK for now: set to private property to avoid self-assignment)
this.state = this._state;
} else {
console.log('Unhandled action in "handleClick()" for status:', this.state.status);
}
function isAuthorized () {
return this.state.status === AuthStates.AUTHORIZED;
}
function isInitialized () {
return this.state.status === AuthStates.INITIALIZED;
}
function isNeedSignIn () {
return this.state.status === AuthStates.NEED_SIGNIN;
}
}
/**
* Compute the return URL for authentication redirect.
* Used only in browser environments.
* @param {string} [returnURL] - The return URL setting ('auto#', 'self#', or custom URL)
* @param {string} [windowLocationForTest] - Mock window.location.href for testing
* @param {string|Navigator} [navigatorForTests] - Mock navigator for testing
* @returns {string|boolean} The computed return URL, or false if using popup mode
*/
getReturnURL (
returnURL,
windowLocationForTest,
navigatorForTests
) {
const RETURN_URL_AUTO = 'auto';
returnURL = returnURL || RETURN_URL_AUTO + '#';
// check the trailer
const trailer = returnURL.slice(-1);
if ('#&?'.indexOf(trailer) < 0) {
throw new Error('Pryv access: Last character of --returnURL setting-- is not ' +
'"?", "&" or "#": ' + returnURL);
}
// auto mode for desktop
if (returnUrlIsAuto(returnURL) &&
!utils.browserIsMobileOrTablet(navigatorForTests)) {
return false;
// auto mode for mobile or self
} else if ((returnUrlIsAuto(returnURL) &&
utils.browserIsMobileOrTablet(navigatorForTests)) ||
returnURL.indexOf('self') === 0) {
// set self as return url?
// eventually clean-up current url from previous pryv returnURL
const locationHref = windowLocationForTest || window.location.href;
returnURL = locationHref + returnURL.substring(4);
}
return utils.cleanURLFromPrYvParams(returnURL);
function returnUrlIsAuto (returnURL) {
return returnURL.indexOf(RETURN_URL_AUTO) === 0;
}
}
/**
* Start the authentication request and polling process
* @returns {Promise<void>}
* @see https://pryv.github.io/reference/#auth-request
*/
async startAuthRequest () {
// @ts-ignore - postAccess uses .call(this) for context
this.state = await postAccess.call(this);
await doPolling.call(this);
/** @this {AuthController} */
async function postAccess () {
try {
const { response, body } = await utils.fetchPost(
// @ts-ignore - this is bound via .call()
this.serviceInfo.access,
// @ts-ignore - this is bound via .call()
this.settings.authRequest
);
if (!response.ok) {
throw new Error('Access request failed: ' + JSON.stringify(body));
}
return body;
} catch (e) {
this.state = {
status: AuthStates.ERROR,
message: 'Requesting access',
error: e
};
throw e; // forward error
}
}
/** @this {AuthController} */
async function doPolling () {
// @ts-ignore - this is bound via .call()
if (this.state?.status !== AuthStates.NEED_SIGNIN) {
return;
}
// @ts-ignore - this is bound via .call()
const pollResponse = await pollAccess(this.state?.poll);
if (pollResponse.status === AuthStates.NEED_SIGNIN) {
// @ts-ignore - this is bound via .call()
setTimeout(await doPolling.bind(this), this.state?.poll_rate_ms);
} else {
this.state = pollResponse;
}
async function pollAccess (pollUrl) {
try {
const { response, body } = await utils.fetchGet(pollUrl);
if (response.status === 403 && body?.status === 'REFUSED') {
return { status: AuthStates.INITIALIZED };
}
return body;
} catch (e) {
return { status: AuthStates.ERROR, message: 'Error while polling for auth request', error: e };
}
}
}
}
// -------------- state listeners ---------------------
set state (newState) {
// retro-compatibility for lib-js < 2.0.9
newState.id = newState.status;
this._state = newState;
this.stateChangeListeners.forEach((listener) => {
try {
listener(this.state);
} catch (e) {
console.log('Error during set state ()', e);
}
});
}
get state () {
return this._state;
}
}
// ----------- private methods -------------
async function checkAutoLogin (authController) {
const loginButton = authController.loginButton;
if (loginButton == null) {
return;
}
const storedCredentials = await loginButton.getAuthorizationData();
if (storedCredentials != null) {
authController.state = Object.assign({}, { status: AuthStates.AUTHORIZED }, storedCredentials);
}
}
// ------------------ ACTIONS ----------- //
async function loadAssets (authController) {
let loadedAssets = {};
try {
loadedAssets = await authController.service.assets();
if (typeof location !== 'undefined') {
await loadedAssets.loginButtonLoadCSS();
const thisMessages = await loadedAssets.loginButtonGetMessages();
if (thisMessages.LOADING) {
authController.messages = Messages(authController.languageCode, thisMessages);
} else {
console.log('WARNING Messages cannot be loaded using defaults: ', thisMessages);
}
}
} catch (e) {
authController.state = {
status: AuthStates.ERROR,
message: 'Cannot fetch button visuals',
error: e
};
throw e; // forward error
}
return loadedAssets;
}
module.exports = AuthController;