-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice-worker.js
254 lines (230 loc) · 7.39 KB
/
service-worker.js
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
import { getPkce } from "./scripts/pkce.js";
import { getState } from "./scripts/state.js";
(() => {
"use strict";
// Update the client ID with the SKY Application ID
var clientId = "<YOUR_SKY_APPLICATION_ID>";
// Update the subscription key with the SKY API Subscription Key
var subscriptionKey = '<YOUR_SKY_API_SUBSCRIPTION_KEY>';
var authorizeUrl = "https://app.blackbaud.com/oauth/authorize";
var tokenUrl = "https://oauth2.sky.blackbaud.com/token";
/**
* Provides the current authorization if the access token is valid.
* Otherwise, starts the authorization process to retrieve a new access token.
*/
function checkAccessToken() {
return new Promise((resolve, reject) => {
getStorageAuth()
.then((auth) => {
if (!!auth.access_token) {
resolve(auth);
} else {
getAccessToken().then(resolve).catch(reject);
}
})
.catch(reject);
});
}
function getStorageAuth() {
return new Promise((resolve) => {
chrome.storage.local.get(["authorization"]).then((result) => {
if (!!result.authorization) {
resolve(JSON.parse(result.authorization));
} else {
resolve({});
}
});
});
}
/**
* Starts the authorization flow and retrieves an access token upon success.
*/
function getAccessToken() {
return new Promise((resolve, reject) => {
var url;
const pkce = getPkce();
const state = getState();
url =
`${authorizeUrl}?client_id=${clientId}&response_type=code&code_challenge_method=S256` +
`&code_challenge=${
pkce.challenge
}&redirect_uri=${chrome.identity.getRedirectURL(
"oauth2"
)}&state=${state}`;
// Starts an authorization flow at the specified URL.
// - https://developer.chrome.com/apps/identity#method-launchWebAuthFlow
chrome.identity.launchWebAuthFlow(
{
url: url,
interactive: true,
},
// Retrieves the value of the `code` and `state`URL parameter.
function handleRedirect(authCodeUrl) {
// Handle any errors encountered.
if (chrome.runtime.lastError) {
console.log(
chrome.runtime.lastError.message +
" Is your SKY API Application redirect URI set to " +
chrome.identity.getRedirectURL("oauth2") +
"?"
);
return reject({
error:
chrome.runtime.lastError.message +
" Check the Background Page console for more info.",
});
}
const authCodeFragments = getUrlParams(
"?" + authCodeUrl.split("?")[1]
);
const code = authCodeFragments.code;
const responseState = authCodeFragments.state;
if (responseState !== state) {
reject("State parameter was not valid");
}
if (!code) {
reject("Error getting authorization code");
}
fetch(tokenUrl, {
method: "post",
body: new URLSearchParams({
client_id: clientId,
grant_type: "authorization_code",
redirect_uri: chrome.identity.getRedirectURL("oauth2"),
code: code,
code_verifier: pkce.verifier,
}),
})
.then((response) => response.json())
.then((body) => {
if (!body.access_token) {
reject(body);
} else {
chrome.storage.local.set({
authorization: JSON.stringify(body),
});
resolve(body);
}
})
.catch(reject);
}
);
});
}
/**
* Makes a request to SKY API Constituent Search endpoint:
* - https://developer.sky.blackbaud.com/docs/services/56b76470069a0509c8f1c5b3/operations/SearchConstituent
* The search text parameter's value is set to an email address.
*/
function getConstituentByEmailAddress(emailAddress) {
return getStorageAuth().then((auth) => {
return fetch(
"https://api.sky.blackbaud.com/constituent/v1/constituents/search?" +
new URLSearchParams({
search_text: emailAddress,
}),
{
headers: {
"bb-api-subscription-key": subscriptionKey,
Authorization: "Bearer " + auth.access_token,
},
}
).then((response) => response.json());
});
}
/**
* Parses URL attributes into a usable object.
*/
function getUrlParams(str) {
var params;
params = {};
if (!str) {
return params;
}
str.replace(/[?&]+([^=&]+)=([^&]*)/gi, (str, key, value) => {
params[key] = value;
});
return params;
}
/**
* Receives (and returns) messages from the content.js script.
*/
function messageHandler(request, sender, callback) {
var emailAddress, parseError;
parseError = (reason) => {
if (typeof reason === "string") {
return callback({
error: reason,
});
}
console.log("MESSAGE ERROR:", reason);
try {
if (!!reason.error) {
reason = `${reason.error}: ${reason.error_description}`;
} else {
reason =
reason.message ||
reason.responseJSON.message ||
JSON.parse(reason.responseText);
}
} catch (error) {
reason =
"Something bad happened. Please reload the page and try again.";
}
return callback({
error: reason,
});
};
switch (request.type) {
// this seemed to be needed for the inbox sdk to work
case "inboxsdk__injectPageWorld":
chrome.scripting.executeScript({
target: { tabId: sender?.tab?.id },
world: "MAIN",
files: ["pageWorld.js"],
});
callback(true);
break;
// Make a request to the constituent search API.
case "apiSearch":
emailAddress = request.message.emailAddress;
checkAccessToken()
.then(() => {
getConstituentByEmailAddress(emailAddress)
.then((data) => {
// The token has expired. Attempt to refresh.
if (data?.statusCode === 401) {
getAccessToken()
.then(() => {
getConstituentByEmailAddress(emailAddress)
.then(callback)
.catch(parseError);
})
.catch(parseError);
}
// All is well, return the constituent data.
else {
callback(data);
}
})
.catch(parseError);
})
.catch(parseError);
break;
// Unrecognized message type.
default:
console.log("Unrecognized request to background script.");
callback({
error: "Invalid message type.",
});
break;
}
// Indicate that we wish to send a response message asynchronously.
// http://developer.chrome.com/extensions/runtime.html#event-onMessage
return true;
}
// clear the authorization if there is one already
chrome.storage.local.remove("authorization");
// Allow content.js to communicate with this script.
chrome.runtime.onMessage.addListener(messageHandler);
})();