@openstax/ts-utils 1.5.4 → 1.5.6
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/assertions.d.ts +2 -2
- package/dist/cjs/assertions.js +8 -7
- package/dist/cjs/services/authProvider/index.d.ts +5 -0
- package/dist/cjs/services/authProvider/utils/userRoleValidator.d.ts +13 -0
- package/dist/cjs/services/authProvider/utils/userRoleValidator.js +37 -0
- package/dist/cjs/services/lrsGateway/attempt-utils.d.ts +2 -0
- package/dist/cjs/services/lrsGateway/index.js +1 -1
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/assertions.d.ts +2 -2
- package/dist/esm/assertions.js +1 -1
- package/dist/esm/services/authProvider/index.d.ts +5 -0
- package/dist/esm/services/authProvider/utils/userRoleValidator.d.ts +13 -0
- package/dist/esm/services/authProvider/utils/userRoleValidator.js +33 -0
- package/dist/esm/services/lrsGateway/attempt-utils.d.ts +2 -0
- package/dist/esm/services/lrsGateway/index.js +1 -1
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/script/.build-dist.swo +0 -0
- package/script/.build-dist.swp +0 -0
- package/script/bin/.copy-from-template.bash.swp +0 -0
package/dist/esm/assertions.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
declare type AssertionFailed = string | Error | (() => never) | undefined;
|
|
1
|
+
export declare type AssertionFailed = string | Error | (() => never) | undefined;
|
|
2
|
+
export declare const doThrow: (failed: AssertionFailed) => never;
|
|
2
3
|
/**
|
|
3
4
|
* Asserts that the given value is true.
|
|
4
5
|
*
|
|
@@ -82,4 +83,3 @@ export declare const assertInstanceOf: <T>(thing: any, constructable: Function &
|
|
|
82
83
|
* @see assertInstanceOf
|
|
83
84
|
*/
|
|
84
85
|
export declare const assertErrorInstanceOf: <T extends Error>(thing: unknown, constructable: Function & (new (...args: any[]) => T)) => T;
|
|
85
|
-
export {};
|
package/dist/esm/assertions.js
CHANGED
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
* stack trace more useful, and allows you to use specific error types
|
|
12
12
|
* that might be handled differently.
|
|
13
13
|
*/
|
|
14
|
-
const doThrow = (failed) => {
|
|
14
|
+
export const doThrow = (failed) => {
|
|
15
15
|
if (typeof failed === 'string') {
|
|
16
16
|
throw new Error(failed);
|
|
17
17
|
}
|
|
@@ -18,6 +18,11 @@ export interface ApiUser extends TokenUser {
|
|
|
18
18
|
is_verified: boolean;
|
|
19
19
|
is_guessed_preferred: boolean;
|
|
20
20
|
}>;
|
|
21
|
+
applications: Array<{
|
|
22
|
+
id: number;
|
|
23
|
+
name: string;
|
|
24
|
+
roles: string[];
|
|
25
|
+
}>;
|
|
21
26
|
external_ids: string[];
|
|
22
27
|
is_not_gdpr_location: boolean;
|
|
23
28
|
signed_contract_names: string[];
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { AuthProvider } from '..';
|
|
2
|
+
import { AssertionFailed } from '../../../assertions';
|
|
3
|
+
import { ConfigProviderForConfig } from '../../../config';
|
|
4
|
+
declare type Config = {
|
|
5
|
+
application: string;
|
|
6
|
+
};
|
|
7
|
+
export declare const createUserRoleValidator: (auth: AuthProvider, config: ConfigProviderForConfig<Config>) => {
|
|
8
|
+
getUserRoles: () => Promise<string[]>;
|
|
9
|
+
userHasRole: (role: string[]) => Promise<boolean>;
|
|
10
|
+
assertUserRole: (role: string[], fail?: AssertionFailed) => Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
export declare type UserRoleValidator = ReturnType<typeof createUserRoleValidator>;
|
|
13
|
+
export {};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { doThrow } from '../../../assertions';
|
|
2
|
+
import { resolveConfigValue } from '../../../config/resolveConfigValue';
|
|
3
|
+
import { UnauthorizedError } from '../../../errors';
|
|
4
|
+
import { once } from '../../../misc/helpers';
|
|
5
|
+
export const createUserRoleValidator = (auth, config) => {
|
|
6
|
+
const application = once(() => resolveConfigValue(config.application));
|
|
7
|
+
const getUserRoles = async () => {
|
|
8
|
+
var _a;
|
|
9
|
+
const user = await auth.getUser();
|
|
10
|
+
const appName = await application();
|
|
11
|
+
if (!user || !('applications' in user)) {
|
|
12
|
+
return [];
|
|
13
|
+
}
|
|
14
|
+
return ((_a = user.applications.find(a => a.name === appName)) === null || _a === void 0 ? void 0 : _a.roles) || [];
|
|
15
|
+
};
|
|
16
|
+
const userHasRole = async (role) => {
|
|
17
|
+
const roles = await getUserRoles();
|
|
18
|
+
if (!roles.some(r => role.includes(r))) {
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
};
|
|
23
|
+
const assertUserRole = async (role, fail = new UnauthorizedError()) => {
|
|
24
|
+
if (!await userHasRole(role)) {
|
|
25
|
+
return doThrow(fail);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
return {
|
|
29
|
+
getUserRoles,
|
|
30
|
+
userHasRole,
|
|
31
|
+
assertUserRole
|
|
32
|
+
};
|
|
33
|
+
};
|
|
@@ -24,8 +24,10 @@ export declare const resolveAttemptInfo: (statements: XapiStatement[], options?:
|
|
|
24
24
|
currentPreference?: "latest" | "oldest" | undefined;
|
|
25
25
|
} | undefined) => ActivityState;
|
|
26
26
|
export declare const loadStatementsForActivityAndFirstChildren: (gateway: LrsGateway, activityIRI: string, options?: {
|
|
27
|
+
anyUser?: boolean | undefined;
|
|
27
28
|
attempt?: string | undefined;
|
|
28
29
|
ensureSync?: boolean | undefined;
|
|
30
|
+
user?: string | undefined;
|
|
29
31
|
} | undefined) => Promise<XapiStatement[]>;
|
|
30
32
|
export declare const loadActivityAttemptInfo: (gateway: LrsGateway, activityIRI: string, options?: {
|
|
31
33
|
currentAttempt?: string | undefined;
|
|
@@ -88,7 +88,7 @@ ${await response.text()}`);
|
|
|
88
88
|
throw new Error(`xAPI consistent through ${consistentThrough}; not in sync with current date ${date}.`);
|
|
89
89
|
}
|
|
90
90
|
return formatGetXapiStatementsResponse(responsePromise);
|
|
91
|
-
});
|
|
91
|
+
}, { retries: 4 });
|
|
92
92
|
}
|
|
93
93
|
else {
|
|
94
94
|
return formatGetXapiStatementsResponse(fetchXapiStatements(fetchParams));
|