@openstax/ts-utils 1.32.0 → 1.32.2
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/cjs/services/authProvider/browser.d.ts +6 -38
- package/dist/cjs/services/authProvider/browser.js +1 -0
- package/dist/cjs/services/authProvider/index.d.ts +1 -0
- package/dist/cjs/services/authProvider/index.js +10 -5
- package/dist/cjs/services/authProvider/subrequest.js +8 -6
- package/dist/cjs/services/authProvider/utils/embeddedAuthProvider.d.ts +2 -2
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/services/authProvider/browser.d.ts +6 -38
- package/dist/esm/services/authProvider/browser.js +1 -0
- package/dist/esm/services/authProvider/index.d.ts +1 -0
- package/dist/esm/services/authProvider/index.js +10 -5
- package/dist/esm/services/authProvider/subrequest.js +8 -6
- package/dist/esm/services/authProvider/utils/embeddedAuthProvider.d.ts +2 -2
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { ConfigProviderForConfig } from '../../config';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { GenericFetch } from '../../fetch';
|
|
3
|
+
import { UserData } from './utils/embeddedAuthProvider';
|
|
4
|
+
import { ApiUser, AuthProvider } from '.';
|
|
4
5
|
type Config = {
|
|
5
6
|
accountsBase: string;
|
|
6
7
|
};
|
|
@@ -27,46 +28,13 @@ export interface Window {
|
|
|
27
28
|
removeEventListener: (event: 'message', callback: EventHandler) => void;
|
|
28
29
|
}
|
|
29
30
|
export type UpdatableUserFields = Partial<Pick<ApiUser, 'consent_preferences' | 'first_name' | 'last_name'>>;
|
|
30
|
-
export
|
|
31
|
-
/**
|
|
32
|
-
* gets the authentication token
|
|
33
|
-
*/
|
|
34
|
-
getAuthToken: () => Promise<string | null>;
|
|
35
|
-
/**
|
|
36
|
-
* adds auth parameters to the url. this is only safe to use when using javascript to navigate
|
|
37
|
-
* within the current window, eg `window.location = 'https://my.otherservice.com';` anchors
|
|
38
|
-
* should use getAuthorizedLinkUrl for their href.
|
|
39
|
-
*
|
|
40
|
-
* result unreliable unless `getUser` is resolved first.
|
|
41
|
-
*/
|
|
31
|
+
export type BrowserAuthProvider = AuthProvider & {
|
|
42
32
|
getAuthorizedUrl: (urlString: string) => string;
|
|
43
|
-
/**
|
|
44
|
-
* all link href-s must be rendered with auth tokens so that they work when opened in a new tab
|
|
45
|
-
*
|
|
46
|
-
* result unreliable unless `getUser` is resolved first.
|
|
47
|
-
*/
|
|
48
33
|
getAuthorizedLinkUrl: (urlString: string) => string;
|
|
49
|
-
/**
|
|
50
|
-
* gets an authorized url for an iframe src. sets params on the url and saves its
|
|
51
|
-
* origin to trust releasing user identity to it
|
|
52
|
-
*/
|
|
53
34
|
getAuthorizedEmbedUrl: (urlString: string, extraParams?: {
|
|
54
35
|
[key: string]: string;
|
|
55
36
|
}) => string;
|
|
56
|
-
|
|
57
|
-
* gets second argument for `fetch` that has authentication token or cookie
|
|
58
|
-
*/
|
|
59
|
-
getAuthorizedFetchConfig: () => Promise<FetchConfig>;
|
|
60
|
-
/**
|
|
61
|
-
* loads current user identity. does not reflect changes in identity after being called the first time.
|
|
62
|
-
*/
|
|
63
|
-
getUser: () => Promise<User | undefined>;
|
|
64
|
-
/**
|
|
65
|
-
* updates user settings, for example the cookie consent preferences
|
|
66
|
-
*/
|
|
67
|
-
updateUser: (updates: UpdatableUserFields) => Promise<{
|
|
68
|
-
user: ApiUser;
|
|
69
|
-
token: string | null;
|
|
70
|
-
}>;
|
|
37
|
+
updateUser: (updates: UpdatableUserFields) => Promise<UserData<ApiUser>>;
|
|
71
38
|
};
|
|
39
|
+
export declare const browserAuthProvider: <C extends string = "auth">({ window, configSpace }: Initializer<C>) => (configProvider: { [_key in C]: ConfigProviderForConfig<Config>; }) => BrowserAuthProvider;
|
|
72
40
|
export {};
|
|
@@ -142,6 +142,7 @@ export const browserAuthProvider = ({ window, configSpace }) => (configProvider)
|
|
|
142
142
|
* loads current user identity. does not reflect changes in identity after being called the first time.
|
|
143
143
|
*/
|
|
144
144
|
getUser,
|
|
145
|
+
loadUserData: getUser,
|
|
145
146
|
/**
|
|
146
147
|
* updates user settings, for example the cookie consent preferences
|
|
147
148
|
*/
|
|
@@ -43,6 +43,7 @@ export type AuthProvider = {
|
|
|
43
43
|
* gets second argument for `fetch` that has authentication token or cookie
|
|
44
44
|
*/
|
|
45
45
|
getAuthorizedFetchConfig: () => Promise<FetchConfig>;
|
|
46
|
+
loadUserData: () => Promise<ApiUser | undefined>;
|
|
46
47
|
};
|
|
47
48
|
export type CookieAuthProviderRequest = {
|
|
48
49
|
cookies?: string[];
|
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
import cookie from 'cookie';
|
|
2
2
|
import { tuple } from '../../misc/helpers';
|
|
3
3
|
import { getHeader } from '../../routing/helpers';
|
|
4
|
-
export const stubAuthProvider = (user) =>
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
4
|
+
export const stubAuthProvider = (user) => {
|
|
5
|
+
const getUser = () => Promise.resolve(user);
|
|
6
|
+
return {
|
|
7
|
+
getAuthToken: () => Promise.resolve('authToken'),
|
|
8
|
+
getUser,
|
|
9
|
+
getAuthorizedFetchConfig: () => Promise.resolve(user ? { headers: { Authorization: user.uuid } } : {}),
|
|
10
|
+
// This is not technically correct, but most tests won't care
|
|
11
|
+
loadUserData: getUser
|
|
12
|
+
};
|
|
13
|
+
};
|
|
9
14
|
export const getAuthTokenOrCookie = (request, cookieName, queryKey = 'auth') => {
|
|
10
15
|
var _a, _b;
|
|
11
16
|
const authParam = request.queryStringParameters ? request.queryStringParameters[queryKey] : undefined;
|
|
@@ -29,15 +29,17 @@ export const subrequestAuthProvider = (initializer) => (configProvider) => {
|
|
|
29
29
|
}
|
|
30
30
|
return user;
|
|
31
31
|
};
|
|
32
|
+
const getUser = async () => {
|
|
33
|
+
if (!user) {
|
|
34
|
+
user = await loadUser();
|
|
35
|
+
}
|
|
36
|
+
return user;
|
|
37
|
+
};
|
|
32
38
|
return {
|
|
33
39
|
getAuthToken,
|
|
34
40
|
getAuthorizedFetchConfig,
|
|
35
|
-
getUser
|
|
36
|
-
|
|
37
|
-
user = await loadUser();
|
|
38
|
-
}
|
|
39
|
-
return user;
|
|
40
|
-
}
|
|
41
|
+
getUser,
|
|
42
|
+
loadUserData: getUser
|
|
41
43
|
};
|
|
42
44
|
};
|
|
43
45
|
};
|