use native dns promises
[squeep-indieauth-helper] / lib / communication.js
index 82400c354e2864d5124b605fcaf965169188ecf8..ce2bc4e25b8bc05b281b86f970cd1e152b30c35c 100644 (file)
@@ -11,7 +11,6 @@ const { promisify } = require('util');
 const randomBytesAsync = promisify(randomBytes);
 const { Address4, Address6 } = require('ip-address');
 const dns = require('dns');
-dns.lookupAsync = dns.lookupAsync || promisify(dns.lookup);
 const common = require('./common');
 const Enum = require('./enum');
 const { ValidationError } = require('./errors');
@@ -439,7 +438,7 @@ class Communication {
       if (!urlObj.hostname.endsWith('.')) {
         hostnames.push(urlObj.hostname + '.');
       }
-      const settledResolutions = await Promise.allSettled(hostnames.map((hostname) => dns.lookupAsync(hostname, {
+      const settledResolutions = await Promise.allSettled(hostnames.map((hostname) => dns.promises.lookup(hostname, {
         all: true,
         verbatim: true,
       })));
@@ -519,7 +518,7 @@ class Communication {
       Communication._urlValidScheme(profile);
       Communication._urlPartsDisallowed(profile, ['hash', 'username', 'password', 'port']);
       Communication._urlPathNoDots(url);
-      Communication._urlNamedHost(profile, options.allowLoopback, options.resolveHostname);
+      await Communication._urlNamedHost(profile, options.allowLoopback, options.resolveHostname);
     } catch (e) {
       this.logger.debug(_scope, 'profile url not valid', { url, error: e });
       throw new ValidationError(`${errorScope}: ${e.message}`);
@@ -607,7 +606,7 @@ class Communication {
         let urlMatched = false;
         const itemType = item.type || [];
         if ((itemType.includes('h-app') || itemType.includes('h-x-app'))
-        &&  (item.properties && item.properties.url)) {
+        &&  (item?.properties?.url)) {
           item.properties.url.forEach((url) => {
             try {
               const hUrl = new URL(url);
@@ -671,7 +670,7 @@ class Communication {
     // and populate profile fields with first-encountered card values.
     if (mfData && 'items' in mfData) {
       const hCards = mfData.items.filter((item) =>
-        item.type && item.type.includes('h-card') &&
+        item?.type?.includes('h-card') &&
         item.properties && item.properties.url && item.properties.url.includes(urlObj.href));
       hCards.forEach((hCard) => {
         Object.keys(profile).forEach((key) => {
@@ -749,8 +748,8 @@ class Communication {
 
 
   /**
-   * POST to the auth endpoint, to redeem a code for a profile object.
-   * FIXME: [name] this isn't specific to profile redemption, it works for tokens too
+   * POST to the auth endpoint, to redeem a code for a profile or token.
+   * N.B. this absorbs any errors!
    * @param {URL} urlObj
    * @param {String} code
    * @param {String} codeVerifier
@@ -758,8 +757,8 @@ class Communication {
    * @param {String} redirectURI
    * @returns {Object}
    */
-  async redeemProfileCode(urlObj, code, codeVerifier, clientId, redirectURI) {
-    const _scope = _fileScope('redeemProfileCode');
+  async redeemCode(urlObj, code, codeVerifier, clientId, redirectURI) {
+    const _scope = _fileScope('redeemCode');
 
     const formData = common.formData({
       'grant_type': 'authorization_code',
@@ -769,13 +768,13 @@ class Communication {
       'code_verifier': codeVerifier,
     });
 
-    const postRedeemProfileCodeConfig = Communication._axiosConfig('POST', urlObj, formData, {}, {
+    const postRedeemCodeConfig = Communication._axiosConfig('POST', urlObj, formData, {}, {
       [Enum.Header.ContentType]: Enum.ContentType.ApplicationForm,
       [Enum.Header.Accept]: `${Enum.ContentType.ApplicationJson}, ${Enum.ContentType.Any};q=0.1`,
     });
 
     try {
-      const response = await this.axios(postRedeemProfileCodeConfig);
+      const response = await this.axios(postRedeemCodeConfig);
       try {
         return JSON.parse(response.data);
       } catch (e) {
@@ -783,14 +782,29 @@ class Communication {
         throw e;
       }
     } catch (e) {
-      this.logger.error(_scope, 'redeem profile code request failed', { error: e, url: urlObj.href });
+      this.logger.error(_scope, 'redeem code request failed', { error: e, url: urlObj.href });
       return;
     }
   }
 
 
   /**
-   * Verify a token with an IdP endpoint, using the Authentication header supplied.
+   * Deprecated method name.
+   * @see redeemCode
+   * @param {URL} urlObj
+   * @param {String} code
+   * @param {Strin} codeVerifier
+   * @param {String} clientId
+   * @param {String} redirectURI
+   * @returns {Object}
+   */
+  async redeemProfileCode(urlObj, code, codeVerifier, clientId, redirectURI) {
+    return this.redeemCode(urlObj, code, codeVerifier, clientId, redirectURI);
+  }
+
+
+  /**
+   * Verify a token with an IdP endpoint, using the Authorization header supplied.
    * @param {URL} introspectionUrlObj
    * @param {String} authorizationHeader
    * @param {String} token
@@ -844,10 +858,10 @@ class Communication {
   /**
    * Attempt to deliver a ticket to an endpoint.
    * N.B. does not absorb errors
-   * @param {*} ticketEndpointUrlObj
-   * @param {*} resourceUrlObj
-   * @param {*} subjectUrlObj
-   * @param {*} ticket
+   * @param {URL} ticketEndpointUrlObj
+   * @param {URL} resourceUrlObj
+   * @param {URL} subjectUrlObj
+   * @param {String} ticket
    * @returns {Promise<AxiosResponse>}
    */
   async deliverTicket(ticketEndpointUrlObj, resourceUrlObj, subjectUrlObj, ticket) {