@vuu-ui/vuu-data-remote 3.3.10 → 4.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,89 +0,0 @@
1
- function _to_primitive(input, hint) {
2
- if ("object" !== _type_of(input) || null === input) return input;
3
- var prim = input[Symbol.toPrimitive];
4
- if (void 0 !== prim) {
5
- var res = prim.call(input, hint || "default");
6
- if ("object" !== _type_of(res)) return res;
7
- throw new TypeError("@@toPrimitive must return a primitive value.");
8
- }
9
- return ("string" === hint ? String : Number)(input);
10
- }
11
- function _to_property_key(arg) {
12
- var key = _to_primitive(arg, "string");
13
- return "symbol" === _type_of(key) ? key : String(key);
14
- }
15
- function _type_of(obj) {
16
- return obj && "u" > typeof Symbol && obj.constructor === Symbol ? "symbol" : typeof obj;
17
- }
18
- let _computedKey;
19
- class RetryOptionsImpl {
20
- retryIntervals;
21
- ind = 0;
22
- constructor(retryIntervals){
23
- this.retryIntervals = retryIntervals;
24
- console.log("[RetryOptionsImpl] constructor");
25
- }
26
- next() {
27
- this.ind = Math.min(this.retryIntervals.length, this.ind + 1);
28
- }
29
- get remaining() {
30
- return this.retryIntervals.length - this.ind;
31
- }
32
- get interval() {
33
- if (this.ind === this.retryIntervals.length) return -1;
34
- return 1000 * this.retryIntervals[this.ind];
35
- }
36
- }
37
- function RetryOptions(retryIntervals) {
38
- if (Array.isArray(retryIntervals)) return new RetryOptionsImpl(retryIntervals);
39
- throw Error("[lostConnectionHandler] RetryOptions, invalid retryIntervals");
40
- }
41
- const defaultRetryIntervals = [
42
- 1,
43
- 2,
44
- 3,
45
- 5,
46
- 10,
47
- 30,
48
- 60,
49
- 120
50
- ];
51
- _computedKey = _to_property_key(Symbol.asyncIterator);
52
- class RetryGenerator {
53
- vuuAuth;
54
- options;
55
- constructor(vuuAuth, options){
56
- this.vuuAuth = vuuAuth;
57
- this.options = options;
58
- console.log("[RetryGenerator] constructor");
59
- }
60
- async *[_computedKey]() {
61
- let connected = false;
62
- do {
63
- await new Promise((resolve)=>setTimeout(resolve, this.options.interval));
64
- try {
65
- await this.vuuAuth.login();
66
- connected = true;
67
- yield "connected";
68
- } catch (err) {}
69
- this.options.next();
70
- }while (!connected && -1 !== this.options.interval)
71
- if (!connected) yield "connection-failed";
72
- }
73
- }
74
- class LostConnectionHandler {
75
- vuuAuth;
76
- retryIntervals;
77
- constructor(vuuAuth, retryIntervals = defaultRetryIntervals){
78
- this.vuuAuth = vuuAuth;
79
- this.retryIntervals = retryIntervals;
80
- }
81
- async reconnect() {
82
- for await (const result of new RetryGenerator(this.vuuAuth, RetryOptions(this.retryIntervals))){
83
- console.log(` ... async iterator result = ${result}`);
84
- if ("connected" === result) return result;
85
- }
86
- return "connection-failed";
87
- }
88
- }
89
- export { LostConnectionHandler, RetryGenerator, RetryOptions };
@@ -1,78 +0,0 @@
1
- import { getCookieValue } from "@vuu-ui/vuu-utils";
2
- import { parseVuuUserFromToken } from "./authenticate.js";
3
- class VuuAuthProvider {
4
- authEndpoint;
5
- constructor(authEndpoint){
6
- this.authEndpoint = authEndpoint;
7
- }
8
- login = async (username, password)=>{
9
- const date = new Date();
10
- const days = 1;
11
- date.setTime(date.getTime() + 24 * days * 3600000);
12
- if (username && password) {
13
- const { token } = await this.getVuuTokenWithUsernameAndPassword(username, password);
14
- document.cookie = `vuu-auth-user=${username};expires=${date.toUTCString()};path=/`;
15
- document.cookie = `vuu-auth-password=${password};expires=${date.toUTCString()};path=/`;
16
- document.cookie = `vuu-auth-token=${token};expires=${date.toUTCString()};path=/`;
17
- return this.redirectToApplication();
18
- }
19
- {
20
- const userName = getCookieValue("vuu-auth-user");
21
- const password = getCookieValue("vuu-auth-password");
22
- if (!userName || !password) return this.redirectToLoginPage();
23
- {
24
- const { authorizations, token } = await this.getVuuTokenWithUsernameAndPassword(userName, password);
25
- document.cookie = `vuu-auth-token=${token};expires=${date.toUTCString()};path=/`;
26
- return {
27
- authorizations,
28
- user: {
29
- userName
30
- },
31
- token
32
- };
33
- }
34
- }
35
- };
36
- async getVuuTokenWithUsernameAndPassword(username, password) {
37
- const response = await fetch(this.authEndpoint, {
38
- method: "POST",
39
- credentials: "include",
40
- headers: {
41
- "Content-Type": "application/json",
42
- "access-control-allow-origin": location.host
43
- },
44
- body: JSON.stringify({
45
- username,
46
- password
47
- })
48
- });
49
- if (response.ok) {
50
- const vuuAuthToken = response.headers.get("vuu-auth-token");
51
- if ("string" == typeof vuuAuthToken && vuuAuthToken.length > 0) {
52
- const { authorizations } = parseVuuUserFromToken(vuuAuthToken);
53
- return {
54
- authorizations,
55
- token: vuuAuthToken
56
- };
57
- }
58
- throw Error("Authentication failed auth token not returned by server");
59
- }
60
- throw Error(`Authentication failed, ${response.status}`);
61
- }
62
- clear() {
63
- document.cookie = "vuu-auth-user= ; expires = Thu, 01 Jan 1970 00:00:00 GMT";
64
- document.cookie = "vuu-auth-password= ; expires = Thu, 01 Jan 1970 00:00:00 GMT";
65
- document.cookie = "vuu-auth-token= ; expires = Thu, 01 Jan 1970 00:00:00 GMT";
66
- }
67
- redirectToLoginPage() {
68
- window.location.href = "login.html";
69
- }
70
- redirectToApplication() {
71
- window.location.href = "index.html";
72
- }
73
- logout() {
74
- this.clear();
75
- this.redirectToLoginPage();
76
- }
77
- }
78
- export { VuuAuthProvider };
@@ -1,36 +0,0 @@
1
- import ConnectionManager from "./ConnectionManager.js";
2
- const VuuAuthTokenIssuePolicy = {
3
- BearerToken: "bearer-token",
4
- UsernamePassword: "username-password"
5
- };
6
- class VuuAuthenticator {
7
- authProvider;
8
- authTokenIssuePolicy;
9
- websocketUrl;
10
- constructor({ authProvider, authTokenIssuePolicy = VuuAuthTokenIssuePolicy.BearerToken, websocketUrl }){
11
- this.authProvider = authProvider;
12
- this.authTokenIssuePolicy = authTokenIssuePolicy;
13
- this.websocketUrl = websocketUrl;
14
- }
15
- openWebsocketConnection = async (vuuToken)=>{
16
- await ConnectionManager.connect({
17
- url: this.websocketUrl,
18
- token: vuuToken
19
- }, true);
20
- };
21
- login = async ()=>{
22
- const { authorizations, user, token, websocket = true } = await this.authProvider.login();
23
- if (token && user) {
24
- if (websocket) await this.openWebsocketConnection(token);
25
- return [
26
- user,
27
- authorizations
28
- ];
29
- }
30
- throw Error("[VuuAuthenticator] login failed");
31
- };
32
- logout = ()=>{
33
- this.authProvider.logout();
34
- };
35
- }
36
- export { VuuAuthTokenIssuePolicy, VuuAuthenticator };
@@ -1,50 +0,0 @@
1
- const defaultAuthUrl = "api/authn";
2
- const isValidVuuUser = (response)=>"object" == typeof response && null !== response && "name" in response && "authorizations" in response && "string" == typeof response.name && Array.isArray(response.authorizations);
3
- const parseVuuUserFromToken = (token)=>{
4
- const [base64EncodedVuuUser] = token.split(".");
5
- const jsonString = atob(base64EncodedVuuUser);
6
- const response = JSON.parse(jsonString);
7
- if (isValidVuuUser(response)) return response;
8
- throw Error("auth token does not containe VuuUser");
9
- };
10
- const getVuuAuthToken = async (authUrl, token)=>{
11
- const response = await fetch(authUrl, {
12
- headers: {
13
- Authorization: `Bearer ${token}`
14
- }
15
- });
16
- if (!response.ok) throw Error("Authentication error: Auth token failure");
17
- const json = await response.json();
18
- const vuuUser = parseVuuUserFromToken(json.token);
19
- return {
20
- authorizations: vuuUser.authorizations,
21
- token: json.token
22
- };
23
- };
24
- const authenticate = async (username, password, authUrl = defaultAuthUrl)=>fetch(authUrl, {
25
- method: "POST",
26
- credentials: "include",
27
- headers: {
28
- "Content-Type": "application/json",
29
- "access-control-allow-origin": location.host
30
- },
31
- body: JSON.stringify({
32
- username,
33
- password
34
- })
35
- }).then((response)=>{
36
- if (response.ok) {
37
- const authToken = response.headers.get("vuu-auth-token");
38
- if ("string" == typeof authToken && authToken.length > 0) try {
39
- return {
40
- token: authToken,
41
- user: parseVuuUserFromToken(authToken)
42
- };
43
- } catch (e) {
44
- throw Error("Authentication error: vuu auth token decoding failed.");
45
- }
46
- throw Error("Authentication failed auth token not returned by server");
47
- }
48
- throw Error(`Authentication failed ${response.status} ${response.statusText}`);
49
- });
50
- export { authenticate, getVuuAuthToken, parseVuuUserFromToken };