@txstate-mws/sveltekit-utils 1.3.0 → 1.3.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.
- package/dist/unifiedauth.js +10 -10
- package/package.json +1 -1
package/dist/unifiedauth.js
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import { redirect } from '@sveltejs/kit';
|
|
2
2
|
import { isBlank, Cache } from 'txstate-utils';
|
|
3
3
|
import { decodeJwt } from 'jose';
|
|
4
|
-
const mayImpersonateAnyCache = new Cache(async (api) => {
|
|
5
|
-
if (isBlank(api.token))
|
|
6
|
-
return false;
|
|
4
|
+
const mayImpersonateAnyCache = new Cache(async (token, api) => {
|
|
7
5
|
const authUrl = new URL(api.authRedirect);
|
|
8
6
|
const mayImpersonateUrl = new URL('/mayImpersonate', authUrl.origin);
|
|
9
7
|
try {
|
|
10
8
|
const resp = await fetch(mayImpersonateUrl, {
|
|
11
9
|
method: 'POST',
|
|
12
10
|
headers: {
|
|
13
|
-
'Authorization': `Bearer ${
|
|
11
|
+
'Authorization': `Bearer ${token}`,
|
|
14
12
|
'Content-Type': 'application/json'
|
|
15
13
|
},
|
|
16
14
|
body: JSON.stringify({})
|
|
@@ -24,16 +22,14 @@ const mayImpersonateAnyCache = new Cache(async (api) => {
|
|
|
24
22
|
return false;
|
|
25
23
|
}
|
|
26
24
|
});
|
|
27
|
-
const mayImpersonateNetidCache = new Cache(async (
|
|
28
|
-
if (isBlank(api.token))
|
|
29
|
-
return false;
|
|
25
|
+
const mayImpersonateNetidCache = new Cache(async ({ token, netid }, api) => {
|
|
30
26
|
const authUrl = new URL(api.authRedirect);
|
|
31
27
|
const mayImpersonateUrl = new URL('/mayImpersonate', authUrl.origin);
|
|
32
28
|
try {
|
|
33
29
|
const resp = await fetch(mayImpersonateUrl, {
|
|
34
30
|
method: 'POST',
|
|
35
31
|
headers: {
|
|
36
|
-
'Authorization': `Bearer ${
|
|
32
|
+
'Authorization': `Bearer ${token}`,
|
|
37
33
|
'Content-Type': 'application/json'
|
|
38
34
|
},
|
|
39
35
|
body: JSON.stringify({ netid })
|
|
@@ -170,7 +166,9 @@ export const unifiedAuth = {
|
|
|
170
166
|
* @returns A promise that resolves to true if authorized to impersonate, false otherwise
|
|
171
167
|
*/
|
|
172
168
|
async mayImpersonateAny(api) {
|
|
173
|
-
|
|
169
|
+
if (isBlank(api.token))
|
|
170
|
+
return false;
|
|
171
|
+
return await mayImpersonateAnyCache.get(api.token, api);
|
|
174
172
|
},
|
|
175
173
|
/**
|
|
176
174
|
* Check if the current user is authorized to impersonate a specific user.
|
|
@@ -181,6 +179,8 @@ export const unifiedAuth = {
|
|
|
181
179
|
* @returns A promise that resolves to true if authorized, false otherwise
|
|
182
180
|
*/
|
|
183
181
|
async mayImpersonate(api, netid) {
|
|
184
|
-
|
|
182
|
+
if (isBlank(api.token))
|
|
183
|
+
return false;
|
|
184
|
+
return await mayImpersonateNetidCache.get({ token: api.token, netid }, api);
|
|
185
185
|
}
|
|
186
186
|
};
|