3 const axios
= require('axios');
4 const { mf2
} = require('microformats-parser');
5 const { parse: parseLinkHeader
} = require('@squeep/web-linking');
6 const { Iconv
} = require('iconv');
7 const { version: packageVersion
, name: packageName
} = require('../package.json');
8 const { performance
} = require('perf_hooks');
9 const { randomBytes
, createHash
} = require('crypto');
10 const { promisify
} = require('util');
11 const randomBytesAsync
= promisify(randomBytes
);
12 const common
= require('./common');
13 const Enum
= require('./enum');
15 const _fileScope
= common
.fileScope(__filename
);
19 * @param {Console} logger
20 * @param {Object} options
21 * @param {Object=} options.userAgent
22 * @param {String=} options.userAgent.product
23 * @param {String=} options.userAgent.version
24 * @param {String=} options.userAgent.implementation
26 constructor(logger
, options
= {}) {
28 this.options
= options
;
29 this.axios
= axios
.create({
31 [Enum
.Header
.UserAgent
]: Communication
._userAgentString(options
.userAgent
),
32 [Enum
.Header
.Accept
]: 'text/html, text/*;q=0.9, application/xhtml+xml;q=0.8, application/xml;q=0.8, */*;q=0.1',
35 this.axios
.interceptors
.request
.use((request
) => {
36 request
.startTimestampMs
= performance
.now();
39 this.axios
.interceptors
.response
.use((response
) => {
40 response
.elapsedTimeMs
= performance
.now() - response
.config
.startTimestampMs
;
46 static _challengeFromVerifier(verifier
) {
47 const hash
= createHash('sha256');
48 hash
.update(verifier
);
49 return common
.base64ToBase64URL(hash
.digest('base64'));
53 * Create a code verifier and its challenge.
54 * @param {Number} length
57 static async
generatePKCE(length
= 128) {
58 if (length
< 43 || length
> 128) {
59 throw new RangeError('InvalidLength');
62 const bufferLength
= Math
.floor(length
* 3 / 4);
63 const randomBuffer
= await
randomBytesAsync(bufferLength
);
64 const verifier
= common
.base64ToBase64URL(randomBuffer
.toString('base64'));
66 const challenge
= Communication
._challengeFromVerifier(verifier
);
69 codeChallengeMethod: 'S256',
70 codeVerifier: verifier
,
71 codeChallenge: challenge
,
77 * Check a challenge with a verifier.
78 * @param {String} codeChallenge
79 * @param {String} codeVerifier
80 * @param {String} codeChallengeMethod
83 static verifyChallenge(codeChallenge
, codeVerifier
, codeChallengeMethod
) {
84 switch (codeChallengeMethod
) {
87 const challenge
= Communication
._challengeFromVerifier(codeVerifier
);
88 return challenge
=== codeChallenge
;
92 throw new Error('unsupported challenge method');
98 * Assemble a suitable User-Agent value.
99 * @param {Object} userAgentConfig
100 * @param {String=} userAgentConfig.product
101 * @param {String=} userAgentConfig.version
102 * @param {String=} userAgentConfig.implementation
105 static _userAgentString(userAgentConfig
) {
106 // eslint-disable-next-line security/detect-object-injection
107 const _conf
= (field
, def
) => (userAgentConfig
&& field
in userAgentConfig
) ? userAgentConfig
[field
] : def
;
108 const product
= _conf('product', packageName
).split('/').pop();
109 const version
= _conf('version', packageVersion
);
110 let implementation
= _conf('implementation', Enum
.Specification
);
111 if (implementation
) {
112 implementation
= ` (${implementation})`;
114 return `${product}/${version}${implementation}`;
119 * A request config skeleton.
120 * @param {String} method
121 * @param {URL} urlObj
122 * @param {String=} body
123 * @param {Object=} params
124 * @param {Object=} headers
127 static _axiosConfig(method
, urlObj
, body
, params
= {}, headers
= {}) {
130 url: `${urlObj.origin}${urlObj.pathname}`,
131 params: urlObj
.searchParams
,
133 ...(body
&& { data: body
}),
134 // Setting this does not appear to be enough to keep axios from parsing JSON response into object
135 responseType: 'text',
136 // So force the matter by eliding all response transformations
137 transformResponse: [ (res
) => res
],
139 Object
.entries(params
).map(([k
, v
]) => config
.params
.set(k
, v
));
145 * Isolate the base of a url.
146 * mf2 parser needs this so that relative links can be made absolute.
147 * @param {URL} urlObj
150 static _baseUrlString(urlObj
) {
151 const baseUrl
= new URL(urlObj
);
152 const lastSlashIdx
= baseUrl
.pathname
.lastIndexOf('/');
153 if (lastSlashIdx
> 0) {
154 baseUrl
.pathname
= baseUrl
.pathname
.slice(0, lastSlashIdx
+ 1);
161 * Convert a Content-Type string to normalized components.
163 * N.B. this ill-named non-parsing implementation will not work
164 * if a parameter value for some reason includes a ; or = within
166 * @param {String} contentTypeHeader
167 * @returns {Object} contentType
168 * @returns {String} contentType.mediaType
169 * @returns {Object} contentType.params
171 static _parseContentType(contentTypeHeader
, defaultContentType
= Enum
.ContentType
.ApplicationOctetStream
) {
172 const [ mediaType
, ...params
] = (contentTypeHeader
|| '').split(/ *; */
);
174 mediaType: mediaType
.toLowerCase() || defaultContentType
,
175 params: params
.reduce((obj
, param
) => {
176 const [field
, value
] = param
.split('=');
177 const isQuoted
= value
&& value
.charAt(0) === '"' && value
.charAt(value
.length
- 1) === '"';
178 obj
[field
.toLowerCase()] = isQuoted
? value
.slice(1, value
.length
- 1) : value
;
186 * Parse and add any header link relations to mf data.
187 * @param {Object} microformat
188 * @param {Object} response
190 _mergeLinkHeader(microformat
, response
) {
191 const _scope
= _fileScope('_mergeLinkHeader');
193 // Establish that microformat has expected structure
194 ['rels', 'rel-urls'].forEach((p
) => {
195 if (!(p
in microformat
)) {
196 microformat
[p
] = {}; // eslint-disable-line security/detect-object-injection
199 if (!('items' in microformat
)) {
200 microformat
.items
= [];
203 const linkHeader
= response
.headers
[Enum
.Header
.Link
.toLowerCase()];
207 links
.push(...parseLinkHeader(linkHeader
));
209 this.logger
.error(_scope
, 'failed to parse link header', { error: e
, linkHeader
});
214 // Push header link rels into microformat form.
215 // Inserted at front of lists, as headers take precedence.
216 links
.forEach((link
) => {
217 link
.attributes
.forEach((attr
) => {
218 if (attr
.name
=== 'rel') {
219 if (!(attr
.value
in microformat
.rels
)) {
220 microformat
.rels
[attr
.value
] = [];
222 microformat
.rels
[attr
.value
].unshift(link
.target
);
224 if (!(link
.target
in microformat
['rel-urls'])) {
225 microformat
['rel-urls'][link
.target
] = {
230 microformat
['rel-urls'][link
.target
].rels
.unshift(attr
.value
);
238 * Retrieve and parse microformat data from url.
239 * N.B. this absorbs any errors!
240 * @param {URL} urlObj
243 async
fetchMicroformat(urlObj
) {
244 const _scope
= _fileScope('fetchMicroformat');
245 const logInfoData
= {
247 microformat: undefined,
252 const fetchMicroformatConfig
= Communication
._axiosConfig('GET', urlObj
);
253 response
= await
this.axios(fetchMicroformatConfig
);
255 this.logger
.error(_scope
, 'microformat request failed', { error: e
, ...logInfoData
});
258 logInfoData
.response
= common
.axiosResponseLogData(response
);
260 // Normalize to utf8.
261 let body
= response
.data
;
262 const contentType
= Communication
._parseContentType(response
.headers
[Enum
.Header
.ContentType
.toLowerCase()]);
263 const nonUTF8Charset
= !/utf-*8/i.test(contentType
.params
.charset
) && contentType
.params
.charset
;
264 if (nonUTF8Charset
) {
266 const iconv
= new Iconv(nonUTF8Charset
, 'utf-8//translit//ignore');
267 body
= iconv
.convert(body
).toString('utf8');
269 // istanbul ignore next
270 this.logger
.error(_scope
, 'iconv conversion error', { error: e
, ...logInfoData
});
271 // Try to carry on, maybe the encoding will work anyhow...
275 let microformat
= {};
277 microformat
= mf2(body
, {
278 baseUrl: Communication
._baseUrlString(urlObj
),
281 this.logger
.error(_scope
, 'failed to parse microformat data', { error: e
, ...logInfoData
});
282 // Try to carry on, maybe there are link headers...
285 this._mergeLinkHeader(microformat
, response
);
287 logInfoData
.microformat
= microformat
;
289 this.logger
.debug(_scope
, 'parsed microformat data', logInfoData
);
295 * Retrieve and parse JSON.
296 * N.B. this absorbs any errors!
297 * @param {URL} urlObj
300 async
fetchJSON(urlObj
) {
301 const _scope
= _fileScope('fetchJSON');
302 const logInfoData
= {
308 const fetchJSONConfig
= Communication
._axiosConfig('GET', urlObj
, undefined, undefined, {
309 [Enum
.Header
.Accept
]: [Enum
.ContentType
.ApplicationJson
, Enum
.ContentType
.Any
+ ';q=0.1'].join(', '),
311 response
= await
this.axios(fetchJSONConfig
);
313 this.logger
.error(_scope
, 'json request failed', { error: e
, ...logInfoData
});
316 logInfoData
.response
= common
.axiosResponseLogData(response
);
320 data
= JSON
.parse(response
.data
);
322 this.logger
.error(_scope
, 'json parsing failed', { error: e
, ...logInfoData
});
330 * @typedef {Object} ClientIdentifierData
331 * @property {Object} rels - keyed by relation to array of uris
332 * @property {HAppData[]} items
335 * Retrieve and parse client identifier endpoint data.
336 * @param {URL} urlObj
337 * @returns {ClientIdentifierData|undefined} mf2 data filtered for h-app items, or undefined if url could not be fetched
339 async
fetchClientIdentifier(urlObj
) {
340 const mfData
= await
this.fetchMicroformat(urlObj
);
345 // Only return h-app items with matching url field.
347 rels: mfData
.rels
|| {},
348 items: (mfData
.items
|| []).filter((item
) => {
349 let urlMatched
= false;
350 const itemType
= item
.type
|| [];
351 if ((itemType
.includes('h-app') || itemType
.includes('h-x-app'))
352 && (item
.properties
&& item
.properties
.url
)) {
353 item
.properties
.url
.forEach((url
) => {
355 const hUrl
= new URL(url
);
356 if (hUrl
.href
=== urlObj
.href
) {
369 * @typedef ProfileData
370 * @property {String} name
371 * @property {String} photo
372 * @property {String} url
373 * @property {String} email
374 * @property {String} authorizationEndpoint - deprecated, backwards compatibility for 20201126 spec
375 * @property {String} tokenEndpoint - deprecated, backwards compatibility for 20201126 spec
376 * @property {String} indieauthMetadata authorization server metadata endpoint
377 * @property {Object} metadata - authorization server metadata for profile
378 * @property {String} metadata.issuer
379 * @property {String} metadata.authorizationEndpoint
380 * @property {String} metadata.tokenEndpoint
381 * @property {String} metadata.introspectionEndpoint
382 * @property {String} metadata.introspectionEndpointAuthMethodsSupported
383 * @property {String} metadata.revocationEndpoint
384 * @property {String} metadata.revocationEndpointAuthMethodsSupported
385 * @property {String} metadata.scopesSupported
386 * @property {String} metadata.responseTypesSupported
387 * @property {String} metadata.grantTypesSupported
388 * @property {String} metadata.serviceDocumentation
389 * @property {String} metadata.codeChallengeMethodsSupported
390 * @property {String} metadata.authorizationResponseIssParameterSupported
391 * @property {String} metadata.userinfoEndpoint
394 * Fetch the relevant microformat data from profile url h-card information,
395 * and authorization server metadata.
396 * @param {URL} urlObj
397 * @returns {ProfileData} mf2 data filtered for select fields from h-card
399 async
fetchProfile(urlObj
) {
400 const _scope
= _fileScope('fetchProfile');
402 const mfData
= await
this.fetchMicroformat(urlObj
);
411 // Locate h-card mf2 items with url field matching profile url,
412 // and populate profile fields with first-encountered card values.
413 if (mfData
&& 'items' in mfData
) {
414 const hCards
= mfData
.items
.filter((item
) =>
415 item
.type
&& item
.type
.includes('h-card') &&
416 item
.properties
&& item
.properties
.url
&& item
.properties
.url
.includes(urlObj
.href
));
417 hCards
.forEach((hCard
) => {
418 Object
.keys(profile
).forEach((key
) => {
419 if (!profile
[key
] && key
in hCard
.properties
) { // eslint-disable-line security/detect-object-injection
420 profile
[key
] = hCard
.properties
[key
][0]; // eslint-disable-line security/detect-object-injection
426 // Populate legacy mf2 fields from relation links.
427 // These will be overwritten if they also exist in server metadata.
429 authorizationEndpoint: 'authorization_endpoint', // backwards compatibility
430 tokenEndpoint: 'token_endpoint', // backwards compatibility
431 }).forEach(([p
, r
]) => {
432 if (mfData
&& r
in mfData
.rels
) {
433 profile
.metadata
[p
] = profile
[p
] = mfData
.rels
[r
][0]; // eslint-disable-line security/detect-object-injection
437 // Set metadata field.
438 if (mfData
&& 'indieauth-metadata' in mfData
.rels
) {
439 profile
.indieauthMetadata
= mfData
.rels
['indieauth-metadata'][0];
442 // Attempt to populate metadata from authorization server.
443 if (profile
.indieauthMetadata
) {
446 mdURL
= new URL(profile
.indieauthMetadata
);
447 } catch (e
) /* istanbul ignore next */ {
448 this.logger
.error(_scope
, 'invalid authorization server metadata url', { profile
});
450 /* istanbul ignore else */
452 const metadataResponse
= await
this.fetchJSON(mdURL
);
453 if (metadataResponse
) {
454 // Map snake_case fields to camelCase.
457 authorizationEndpoint: 'authorization_endpoint',
458 tokenEndpoint: 'token_endpoint',
459 introspectionEndpoint: 'introspection_endpoint',
460 introspectionEndpointAuthMethodsSupported: 'introspection_endpoint_auth_methods_supported',
461 revocationEndpoint: 'revocation_endpoint',
462 revocationEndpointAuthMethodsSupported: 'revocation_endpoint_auth_methods_supported',
463 scopesSupported: 'scopes_supported',
464 responseTypesSupported: 'response_types_supported',
465 grantTypesSupported: 'grant_types_supported',
466 serviceDocumentation: 'service_documentation',
467 codeChallengeMethodsSupported: 'code_challenge_methods_supported',
468 authorizationResponseIssParameterSupported: 'authorization_response_iss_parameter_supported',
469 userinfoEndpoint: 'userinfo_endpoint',
470 }).forEach(([c
, s
]) => {
471 if (s
in metadataResponse
) {
472 profile
.metadata
[c
] = metadataResponse
[s
]; // eslint-disable-line security/detect-object-injection
476 // Populate legacy profile fields.
477 ['authorizationEndpoint', 'tokenEndpoint'].forEach((f
) => {
478 if (f
in profile
.metadata
) {
479 profile
[f
] = profile
.metadata
[f
]; // eslint-disable-line security/detect-object-injection
491 * POST to the auth endpoint, to redeem a code for a profile object.
492 * @param {URL} urlObj
493 * @param {String} code
494 * @param {String} codeVerifier
495 * @param {String} clientId
496 * @param {String} redirectURI
499 async
redeemProfileCode(urlObj
, code
, codeVerifier
, clientId
, redirectURI
) {
500 const _scope
= _fileScope('redeemProfileCode');
502 const data
= new URLSearchParams();
504 'grant_type': 'authorization_code',
506 'client_id': clientId
,
507 'redirect_uri': redirectURI
,
508 'code_verifier': codeVerifier
,
509 }).forEach(([name
, value
]) => data
.set(name
, value
));
511 const postRedeemProfileCodeConfig
= Communication
._axiosConfig('POST', urlObj
, data
.toString(), {}, {
512 [Enum
.Header
.ContentType
]: Enum
.ContentType
.ApplicationForm
,
513 [Enum
.Header
.Accept
]: `${Enum.ContentType.ApplicationJson}, ${Enum.ContentType.Any};q=0.1`,
517 const response
= await
this.axios(postRedeemProfileCodeConfig
);
519 return JSON
.parse(response
.data
);
521 this.logger
.error(_scope
, 'failed to parse json', { error: e
, response
});
525 this.logger
.error(_scope
, 'redeem profile code request failed', { error: e
, url: urlObj
.href
});
532 module
.exports
= Communication
;