-
-
Notifications
You must be signed in to change notification settings - Fork 195
Expand file tree
/
Copy pathlogin-utils.js
More file actions
150 lines (132 loc) · 5.44 KB
/
Copy pathlogin-utils.js
File metadata and controls
150 lines (132 loc) · 5.44 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
/*
* GNU AGPL-3.0 License
*
* Copyright (c) 2021 - present core.ai . All rights reserved.
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://opensource.org/licenses/AGPL-3.0.
*
*/
/**
* Login Service Utilities
*
* This module contains utility functions for login service operations,
* including entitlements expiration checking and change detection.
*/
define(function (require, exports, module) {
const KernalModeTrust = window.KernalModeTrust;
if(!KernalModeTrust){
// integrated extensions will have access to kernal mode, but not external extensions
throw new Error("Login utils should have access to KernalModeTrust. Cannot boot without trust ring");
}
/**
* Check if any validTill time has expired
*
* @param {Object|null} entitlements - Current entitlements object
* @param {Object|null} lastRecordedEntitlement - Previously recorded entitlements
* @returns {string|null} - Name of expired plan/entitlement or null if none expired
*/
function validTillExpired(entitlements, lastRecordedEntitlement) {
if (!entitlements) {
return null;
}
const now = Date.now();
function isNewlyExpired(validTill, lastValidTill) {
return (
validTill &&
validTill < now && // expired now
(!lastValidTill || lastValidTill >= now) // but wasn't expired before
);
}
// Check plan validTill
if (entitlements.plan) {
const validTill = entitlements.plan.validTill;
const lastValidTill = (lastRecordedEntitlement && lastRecordedEntitlement.plan)
? lastRecordedEntitlement.plan.validTill
: null;
if (isNewlyExpired(validTill, lastValidTill)) {
return entitlements.plan.name || brackets.config.main_pro_plan;
}
}
// Check entitlements validTill
if (entitlements.entitlements) {
for (const key in entitlements.entitlements) {
const entitlement = entitlements.entitlements[key];
if (!entitlement) {
continue;
}
const validTill = entitlement.validTill;
const lastValidTill = (lastRecordedEntitlement &&
lastRecordedEntitlement.entitlements &&
lastRecordedEntitlement.entitlements[key])
? lastRecordedEntitlement.entitlements[key].validTill
: null;
if (isNewlyExpired(validTill, lastValidTill)) {
return key;
}
}
}
return null;
}
/**
* Check if entitlements have changed from last recorded state
*
* @param {Object|null} current - Current entitlements object
* @param {Object|null} last - Last recorded entitlements object
* @returns {boolean} - True if entitlements have changed, false otherwise
*/
function haveEntitlementsChanged(current, last) {
if (!last && !current) {
return false;
}
if ((!last && current) || (!current && last)) {
return true;
}
if ((!last.entitlements && current.entitlements) || (!current.entitlements && last.entitlements)) {
return true;
}
// Check paidSubscriber changes
const currentPaidSub = current.plan && current.plan.paidSubscriber;
const lastPaidSub = last.plan && last.plan.paidSubscriber;
// Check isSubscriber changes
const currentIsSubscriber = current.plan && current.plan.isSubscriber;
const lastIsSubscriber = last.plan && last.plan.isSubscriber;
if (currentIsSubscriber !== lastIsSubscriber || currentPaidSub !== lastPaidSub) {
return true;
}
// Check plan name changes
const currentPlanName = current.plan && current.plan.name;
const lastPlanName = last.plan && last.plan.name;
if (currentPlanName !== lastPlanName) {
return true;
}
// Check entitlement activations
if (current.entitlements && last.entitlements) {
for (const key of Object.keys(current.entitlements)) {
const currentActivated = current.entitlements[key] && current.entitlements[key].activated;
const lastActivated = last.entitlements[key] && last.entitlements[key].activated;
if (currentActivated !== lastActivated) {
return true;
}
}
}
return false;
}
KernalModeTrust.LoginUtils = {
validTillExpired,
haveEntitlementsChanged
};
// Test only Export functions
if(Phoenix.isTestWindow) {
exports.validTillExpired = validTillExpired;
exports.haveEntitlementsChanged = haveEntitlementsChanged;
}
});