<commit_before>var dns = require( 'dns' )
var DKIM = require( './dkim' )

/**
 * Retrieve a domain key
 * @memberOf DKIM
 * @todo DNS seems to FORMERR on unregistered / expired domains,
 * which maybe should be a TEMPFAIL (?)
 * @todo make this `public_key = dkim_find_key(q_val, d_val, s_val)`,
 * where `*_val` are the signature's attribute values
 * @todo Throw error if the public key is not a Buffer
 * @param {String} domain
 * @param {String} [selector]
 * @param {Function} callback
 */
function getKey( domain, selector, callback ) {

  if( typeof selector === 'function' ) {
    callback = selector
    selector = null
  } else {
    domain = selector + '._domainkey.' + domain
  }

  dns.resolve( domain, 'TXT', function( error, records ) {

    var key = null

    if( error == null ) {

      var keys = records.map(( record ) => record.join( '' ))

      if( !keys.length ) {
        error = new Error( 'No key for signature' )
        error.code = DKIM.PERMFAIL
        return void callback( error, key )
      }

      if( keys.length > 1 ) {
        return callback( new Error( 'Ambigous key selection' ) )
      }

      // If the result returned from the query does not adhere to the
      // format defined in this specification, the Verifier MUST ignore
      // the key record and return PERMFAIL (key syntax error).
      try {
        key = DKIM.Key.parse( keys[0] )
        if( !Buffer.isBuffer( key.key ) ) {
          throw new Error( 'No public key found' )
        }
      } catch( e ) {
        error = new Error( 'Key syntax error' )
        error.code = DKIM.PERMFAIL
      }

    } else {
      switch( error.code ) {
        // If the query for the public key fails because the corresponding
        // key record does not exist, the Verifier MUST immediately return
        // PERMFAIL (no key for signature).
        case dns.NOTFOUND:
        case dns.NODATA:
        case dns.FORMERR:<commit_msg>feat(lib): TEMPFAIL on ambiguous key selection<commit_after>var dns = require( 'dns' )
var DKIM = require( './dkim' )

/**
 * Retrieve a domain key
 * @memberOf DKIM
 * @todo DNS seems to FORMERR on unregistered / expired domains,
 * which maybe should be a TEMPFAIL (?)
 * @todo make this `public_key = dkim_find_key(q_val, d_val, s_val)`,
 * where `*_val` are the signature's attribute values
 * @todo Throw error if the public key is not a Buffer
 * @param {String} domain
 * @param {String} [selector]
 * @param {Function} callback
 */
function getKey( domain, selector, callback ) {

  if( typeof selector === 'function' ) {
    callback = selector
    selector = null
  } else {
    domain = selector + '._domainkey.' + domain
  }

  dns.resolve( domain, 'TXT', function( error, records ) {

    var key = null

    if( error == null ) {

      var keys = records.map(( record ) => record.join( '' ))

      if( !keys.length ) {
        error = new Error( 'No key for signature' )
        error.code = DKIM.PERMFAIL
        return void callback( error, key )
      }

      if( keys.length > 1 ) {
        error = new Error( 'Ambiguous key selection' )
        error.code = DKIM.TEMPFAIL
        return void callback( error, key )
      }

      // If the result returned from the query does not adhere to the
      // format defined in this specification, the Verifier MUST ignore
      // the key record and return PERMFAIL (key syntax error).
      try {
        key = DKIM.Key.parse( keys[0] )
        if( !Buffer.isBuffer( key.key ) ) {
          throw new Error( 'No public key found' )
        }
      } catch( e ) {
        error = new Error( 'Key syntax error' )
        error.code = DKIM.PERMFAIL
      }

    } else {
      switch( error.code ) {
        // If the query for the public key fails because the corresponding
        // key record does not exist, the Verifier MUST immediately return
        // PERMFAIL (no key for signature).
        case dns.NOTFOUND:
        case dns.NODATA:
        case dns.FORMERR:
{'fraction_non_alphanumeric': 0.10833963214915596, 'fraction_numerical': 0.0010078105316200555, 'mean_word_length': 2.29734219269103, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2523678', 'n_tokens_mistral': 1224, 'n_tokens_neox': 1185, 'n_words': 436}