@openstax/ts-utils 1.38.3 → 1.39.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/cjs/services/httpMessageVerifier/index.d.ts +1 -0
- package/dist/cjs/services/httpMessageVerifier/index.js +52 -12
- package/dist/cjs/services/lrsGateway/addStatementDefaultFields.d.ts +3 -2
- package/dist/cjs/services/lrsGateway/attempt-utils.d.ts +1 -1
- package/dist/cjs/services/lrsGateway/attempt-utils.js +2 -2
- package/dist/cjs/services/lrsGateway/index.d.ts +1 -1
- package/dist/cjs/services/lrsGateway/index.js +5 -3
- package/dist/cjs/tsconfig.without-specs.cjs.tsbuildinfo +1 -1
- package/dist/esm/services/httpMessageVerifier/index.d.ts +1 -0
- package/dist/esm/services/httpMessageVerifier/index.js +52 -12
- package/dist/esm/services/lrsGateway/addStatementDefaultFields.d.ts +3 -2
- package/dist/esm/services/lrsGateway/attempt-utils.d.ts +1 -1
- package/dist/esm/services/lrsGateway/attempt-utils.js +2 -2
- package/dist/esm/services/lrsGateway/index.d.ts +1 -1
- package/dist/esm/services/lrsGateway/index.js +5 -3
- package/dist/esm/tsconfig.without-specs.esm.tsbuildinfo +1 -1
- package/package.json +2 -1
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { createHash } from 'crypto';
|
|
3
3
|
import { createVerifier, httpbis } from 'http-message-signatures';
|
|
4
4
|
import { SigningKeyNotFoundError } from 'jwks-rsa';
|
|
5
|
+
import { ByteSequence, parseDictionary, parseItem, Token } from 'structured-headers';
|
|
5
6
|
import { assertString } from '../../assertions';
|
|
6
7
|
import { resolveConfigValue } from '../../config';
|
|
7
8
|
import { InvalidRequestError } from '../../errors';
|
|
@@ -9,6 +10,7 @@ import { once } from '../../misc/helpers';
|
|
|
9
10
|
import { getJwksClient } from '../../misc/jwks';
|
|
10
11
|
export const createHttpMessageVerifier = ({ configSpace, fetcher }) => (configProvider) => {
|
|
11
12
|
const config = configProvider[configSpace !== null && configSpace !== void 0 ? configSpace : 'verifier'];
|
|
13
|
+
const getApiHost = once(async () => await resolveConfigValue(config.apiHost));
|
|
12
14
|
const getBypassSignatureVerification = once(async () => (await resolveConfigValue(config.bypassSignatureVerification)) === 'true');
|
|
13
15
|
return ({ request }) => ({
|
|
14
16
|
verify: async ({ configOverride, signatureAgentVerifier }) => {
|
|
@@ -27,9 +29,25 @@ export const createHttpMessageVerifier = ({ configSpace, fetcher }) => (configPr
|
|
|
27
29
|
// but we do not bother matching the Signature-Agent names with the Signature names
|
|
28
30
|
// as that would be awkward with the packages we use
|
|
29
31
|
const signatureAgentString = (_a = headers['signature-agent']) !== null && _a !== void 0 ? _a : '';
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
32
|
+
let rawValues;
|
|
33
|
+
// Try parsing as RFC 8941 Item first (e.g., "https://url" or bare token)
|
|
34
|
+
try {
|
|
35
|
+
rawValues = [parseItem(signatureAgentString)];
|
|
36
|
+
}
|
|
37
|
+
catch {
|
|
38
|
+
// If item parsing fails, try parsing as a Dictionary (e.g., sig="https://url")
|
|
39
|
+
rawValues = Array.from(parseDictionary(signatureAgentString).values());
|
|
40
|
+
}
|
|
41
|
+
// Convert values to strings (handle Token, string, and reject others) and deduplicate
|
|
42
|
+
const signatureAgents = [...new Set(rawValues.map(([bareItem]) => {
|
|
43
|
+
if (bareItem instanceof Token) {
|
|
44
|
+
return bareItem.toString();
|
|
45
|
+
}
|
|
46
|
+
return assertString(bareItem, new InvalidRequestError('Signature-Agent values must be strings or tokens'));
|
|
47
|
+
}))];
|
|
48
|
+
if (signatureAgents.length === 0) {
|
|
49
|
+
throw new InvalidRequestError('Signature-Agent header is required');
|
|
50
|
+
}
|
|
33
51
|
const keys = (await Promise.all(signatureAgents.map(async (signatureAgent) => {
|
|
34
52
|
if (!await signatureAgentVerifier(signatureAgent)) {
|
|
35
53
|
throw new InvalidRequestError('Signature-Agent verification failed');
|
|
@@ -41,8 +59,7 @@ export const createHttpMessageVerifier = ({ configSpace, fetcher }) => (configPr
|
|
|
41
59
|
body,
|
|
42
60
|
headers,
|
|
43
61
|
method: requestContext.http.method,
|
|
44
|
-
|
|
45
|
-
url: requestContext.http.path,
|
|
62
|
+
url: `${await getApiHost()}${requestContext.http.path}${request.rawQueryString ? `?${request.rawQueryString}` : ''}`,
|
|
46
63
|
};
|
|
47
64
|
if (!await httpbis.verifyMessage({
|
|
48
65
|
all: true,
|
|
@@ -75,14 +92,37 @@ export const createHttpMessageVerifier = ({ configSpace, fetcher }) => (configPr
|
|
|
75
92
|
// For example, if a GET request uses configOverride to make content-digest not required
|
|
76
93
|
if (!headers['content-digest'])
|
|
77
94
|
return true;
|
|
78
|
-
|
|
79
|
-
|
|
95
|
+
let contentDigest;
|
|
96
|
+
try {
|
|
97
|
+
contentDigest = parseDictionary(headers['content-digest']);
|
|
98
|
+
}
|
|
99
|
+
catch {
|
|
80
100
|
throw new InvalidRequestError('Unsupported Content-Digest header format');
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
101
|
+
}
|
|
102
|
+
if (contentDigest.size === 0) {
|
|
103
|
+
throw new InvalidRequestError('Content-Digest header is required');
|
|
104
|
+
}
|
|
105
|
+
// Map Content-Digest algorithm names to Node's crypto algorithm names
|
|
106
|
+
const algorithmMap = {
|
|
107
|
+
'sha-256': 'sha256',
|
|
108
|
+
'sha-512': 'sha512',
|
|
109
|
+
};
|
|
110
|
+
for (const [algorithm, value] of contentDigest.entries()) {
|
|
111
|
+
const [bareItem] = value;
|
|
112
|
+
// Convert ByteSequence to string, or assert it's already a string
|
|
113
|
+
const hashValue = bareItem instanceof ByteSequence
|
|
114
|
+
? bareItem.toBase64()
|
|
115
|
+
: assertString(bareItem, new InvalidRequestError('Content-Digest values must be strings'));
|
|
116
|
+
const cryptoAlgorithm = algorithmMap[algorithm];
|
|
117
|
+
if (!cryptoAlgorithm) {
|
|
118
|
+
throw new InvalidRequestError(`Unsupported Content-Digest algorithm: ${algorithm}`);
|
|
119
|
+
}
|
|
120
|
+
// Calculate the hash
|
|
121
|
+
const calculatedHash = createHash(cryptoAlgorithm).update(assertString(body, new InvalidRequestError('Request body is required when Content-Digest is present'))).digest('base64');
|
|
122
|
+
// Compare with the provided hash
|
|
123
|
+
if (calculatedHash !== hashValue) {
|
|
124
|
+
throw new InvalidRequestError(`Calculated Content-Digest value did not match header for algorithm ${algorithm}`);
|
|
125
|
+
}
|
|
86
126
|
}
|
|
87
127
|
return true;
|
|
88
128
|
},
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { User } from '../authProvider';
|
|
2
1
|
import { EagerXapiStatement, UXapiStatement, XapiStatement } from '.';
|
|
3
2
|
export declare const addStatementDefaultFields: (statement: (Pick<XapiStatement, "object" | "verb" | "context" | "result"> & {
|
|
4
3
|
id?: string;
|
|
5
|
-
}) | UXapiStatement, user:
|
|
4
|
+
}) | UXapiStatement, user: {
|
|
5
|
+
uuid: string;
|
|
6
|
+
}) => EagerXapiStatement;
|
|
@@ -79,7 +79,7 @@ export declare const putAttemptStatement: (gateway: LrsGateway, activity: {
|
|
|
79
79
|
export declare const createAttemptActivityStatement: (attemptStatement: UXapiStatement, verb: UXapiStatement["verb"], result?: UXapiStatement["result"]) => Pick<UXapiStatement, "object" | "verb" | "context" | "result">;
|
|
80
80
|
export declare const putAttemptActivityStatement: (gateway: LrsGateway, attemptStatement: UXapiStatement, verb: UXapiStatement["verb"], result?: UXapiStatement["result"]) => Promise<import(".").EagerXapiStatement>;
|
|
81
81
|
export declare const createCompletedStatement: (attemptStatement: UXapiStatement, result?: UXapiStatement["result"]) => Pick<UXapiStatement, "object" | "verb" | "context" | "result">;
|
|
82
|
-
export declare const putCompletedStatement: (gateway: LrsGateway, attemptStatement: UXapiStatement, result: UXapiStatement["result"]) => Promise<import(".").EagerXapiStatement>;
|
|
82
|
+
export declare const putCompletedStatement: (gateway: LrsGateway, attemptStatement: UXapiStatement, result: UXapiStatement["result"], user?: string) => Promise<import(".").EagerXapiStatement>;
|
|
83
83
|
export declare const createCompletedPendingScoringStatement: (attemptStatement: UXapiStatement) => Pick<UXapiStatement, "object" | "verb" | "context" | "result">;
|
|
84
84
|
export declare const putCompletedPendingScoringStatement: (gateway: LrsGateway, attemptStatement: UXapiStatement) => Promise<import(".").EagerXapiStatement>;
|
|
85
85
|
export declare const createScoredStatement: (attemptStatement: UXapiStatement, result: UXapiStatement["result"]) => Pick<UXapiStatement, "object" | "verb" | "context" | "result">;
|
|
@@ -288,8 +288,8 @@ export const createCompletedStatement = (attemptStatement, result) => {
|
|
|
288
288
|
}
|
|
289
289
|
};
|
|
290
290
|
};
|
|
291
|
-
export const putCompletedStatement = async (gateway, attemptStatement, result) => {
|
|
292
|
-
return (await gateway.putXapiStatements([createCompletedStatement(attemptStatement, result)]))[0];
|
|
291
|
+
export const putCompletedStatement = async (gateway, attemptStatement, result, user) => {
|
|
292
|
+
return (await gateway.putXapiStatements([createCompletedStatement(attemptStatement, result)], user))[0];
|
|
293
293
|
};
|
|
294
294
|
// pending score statement for when the open written response has been graded.
|
|
295
295
|
export const createCompletedPendingScoringStatement = (attemptStatement) => createCompletedStatement(attemptStatement, {
|
|
@@ -84,7 +84,7 @@ export declare const lrsGateway: <C extends string = "lrs">(initializer: Initial
|
|
|
84
84
|
}) => {
|
|
85
85
|
putXapiStatements: (statements: Array<(Pick<XapiStatement, "object" | "verb" | "context" | "result"> & {
|
|
86
86
|
id?: string;
|
|
87
|
-
}) | UXapiStatement
|
|
87
|
+
}) | UXapiStatement>, user?: string) => Promise<EagerXapiStatement[]>;
|
|
88
88
|
getXapiStatements: (params: {
|
|
89
89
|
verb?: string;
|
|
90
90
|
activity?: string;
|
|
@@ -20,9 +20,11 @@ export const lrsGateway = (initializer) => (configProvider) => {
|
|
|
20
20
|
status: [502]
|
|
21
21
|
});
|
|
22
22
|
// Note: This method actually uses POST
|
|
23
|
-
const putXapiStatements = async (statements) => {
|
|
24
|
-
const
|
|
25
|
-
|
|
23
|
+
const putXapiStatements = async (statements, user) => {
|
|
24
|
+
const userObj = user
|
|
25
|
+
? { uuid: user }
|
|
26
|
+
: assertDefined(await authProvider.getUser(), new UnauthorizedError);
|
|
27
|
+
const statementsWithDefaults = statements.map(statement => addStatementDefaultFields(statement, userObj));
|
|
26
28
|
const response = await fetcher((await lrsHost()).replace(/\/+$/, '') + '/data/xAPI/statements', {
|
|
27
29
|
body: JSON.stringify(statementsWithDefaults),
|
|
28
30
|
headers: {
|