-
-
Notifications
You must be signed in to change notification settings - Fork 193
Expand file tree
/
Copy pathlogin-service.js
More file actions
129 lines (110 loc) · 4.61 KB
/
login-service.js
File metadata and controls
129 lines (110 loc) · 4.61 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
/*
* 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.
*
*/
/**
* Shared Login Service
*
* This module contains shared login service functionality used by both
* browser and desktop login implementations, including entitlements management.
*/
define(function (require, exports, module) {
require("./setup-login-service"); // this adds loginService to KernalModeTrust
require("./promotions");
const KernalModeTrust = window.KernalModeTrust;
if(!KernalModeTrust){
// integrated extensions will have access to kernal mode, but not external extensions
throw new Error("Login service should have access to KernalModeTrust. Cannot boot without trust ring");
}
const LoginService = KernalModeTrust.loginService;
// Event constants
const EVENT_ENTITLEMENTS_CHANGED = "entitlements_changed";
// Cached entitlements data
let cachedEntitlements = null;
/**
* Get entitlements from API or cache
* Returns null if user is not logged in
*/
async function getEntitlements(forceRefresh = false) {
// Return null if not logged in
if (!LoginService.isLoggedIn()) {
return null;
}
// Return cached data if available and not forcing refresh
if (cachedEntitlements && !forceRefresh) {
return cachedEntitlements;
}
try {
const accountBaseURL = LoginService.getAccountBaseURL();
const language = brackets.getLocale();
let url = `${accountBaseURL}/getAppEntitlements?lang=${language}`;
let fetchOptions = {
method: 'GET',
headers: {
'Accept': 'application/json'
}
};
// Handle different authentication methods for browser vs desktop
if (Phoenix.isNativeApp) {
// Desktop app: use appSessionID and validationCode
const profile = LoginService.getProfile();
if (profile && profile.apiKey && profile.validationCode) {
url += `&appSessionID=${encodeURIComponent(profile.apiKey)}&validationCode=${encodeURIComponent(profile.validationCode)}`;
} else {
console.error('Missing appSessionID or validationCode for desktop app entitlements');
return null;
}
} else {
// Browser app: use session cookies
fetchOptions.credentials = 'include';
}
const response = await fetch(url, fetchOptions);
if (response.ok) {
const result = await response.json();
if (result.isSuccess) {
// Check if entitlements actually changed
const entitlementsChanged = JSON.stringify(cachedEntitlements) !== JSON.stringify(result);
cachedEntitlements = result;
// Trigger event if entitlements changed
if (entitlementsChanged) {
LoginService.trigger(EVENT_ENTITLEMENTS_CHANGED, result);
}
return cachedEntitlements;
}
}
} catch (error) {
console.error('Failed to fetch entitlements:', error);
}
return null;
}
/**
* Clear cached entitlements and trigger change event
* Called when user logs out
*/
function clearEntitlements() {
if (cachedEntitlements) {
cachedEntitlements = null;
// Trigger event when entitlements are cleared
if (LoginService.trigger) {
LoginService.trigger(EVENT_ENTITLEMENTS_CHANGED, null);
}
}
}
// Add functions to secure exports
LoginService.getEntitlements = getEntitlements;
LoginService.clearEntitlements = clearEntitlements;
LoginService.EVENT_ENTITLEMENTS_CHANGED = EVENT_ENTITLEMENTS_CHANGED;
});