From 94d5fa1c862f0e2397dd1dbd55587978b69c384d Mon Sep 17 00:00:00 2001 From: Rupam Biswal Date: Mon, 9 Jul 2018 13:59:22 +0530 Subject: [PATCH] added new function and update read me --- README.md | 30 ++++++++++++++++---- index.js | 85 ++++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 96 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index d1261e0..79cb6de 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,21 @@ # domain-verification -Verify domain ownsership, currently supports 3 methods: +This module help you to verify domain ownership. -## 1. htmlVerification: +## Install + + $ npm install domain-verification + +## Usage + + consta domainVerification = require('domain-verification'); + domainVerification.html(domain_url,domain_html_name,hash_value); + domainVerification.txt(domain_url,domain_key,domain_value); + domainVerification.metatag(domain_url,domain_key,domain_value); + domainVerification.all(domain_url,domain_key,domain_value,domain_html_name,hash_value); + + We are supporting Promise. + +## 1. html: Verify by uploading a specific HTML to a given domain. @@ -14,18 +28,22 @@ Verify domain ownsership, currently supports 3 methods: * The domain to be verified has the HTML file specified in the url specified by 'domain_url'. * The filename is of the form: 'domain_html_name'.html, eg. 'domainVerified-1233ask.html' -## 2. txtVerification: +## 2. txt: Verify by Txt in the DNS of the domain: - ### Parameters: +### Parameters: * domain_url: The URL of the domain that has the Txt file added * domain_key: The Txt key * domain_value: The Txt value -## 3. metaTagVerification: +## 3. metatag: Verify by metatag information of a domain: ### Parameters: * domain_url: The URL of the domain containing the metatag * domain_key: The meta tag key/name - * domain_value: The value of the meta tag key/name \ No newline at end of file + * domain_value: The value of the meta tag key/name + +## 3. all: + This methods call all the above 3 function and give you the result: + \ No newline at end of file diff --git a/index.js b/index.js index b37343d..da8b9ff 100644 --- a/index.js +++ b/index.js @@ -11,8 +11,10 @@ var DomainVerification = (function() { var htmlVerification = function(domain_url,domain_html_name,hash_value) { var original_args = arguments; return new Promise(function(resolve,reject){ - if(original_args.length == 4) + if(original_args.length == 3) { + var obj = {}; + obj.verified = "Html Verification"; var url = domain_url+'/'+domain_html_name+'.html'; var options = { method: 'GET', @@ -21,13 +23,16 @@ var DomainVerification = (function() { request(options,function(error,response,body){ if(error) { - resolve(false); + obj.status = false; + resolve(obj); } else { if(hash_value == body) { - resolve(true); + obj.status = true; + resolve(obj); } else { - resolve(false); + obj.status = false; + resolve(obj); } } }); @@ -41,19 +46,24 @@ var DomainVerification = (function() { var original_args = arguments; return new Promise(function (resolve,reject){ if(original_args.length == 3){ + var obj = {}; + obj.verified = "Txt Verification"; dns.resolveTxt(domain_url, function(error,records){ if(records == undefined) { - resolve(false); + obj.status = false; + resolve(obj); } else { records.forEach(function(record){ var expected = domain_key+'='+domain_value; if(expected == record[0]) { - resolve(true); + obj.status = true; + resolve(obj); } else { - resolve(false); + obj.status = false; + resolve(obj); } }); } @@ -68,24 +78,31 @@ var DomainVerification = (function() { var metaTagVerification = function(domain_url,domain_key,domain_value) { var original_args = arguments; return new Promise(function(resolve,reject){ + var obj = {}; + obj.verified = "Meta tag Verification"; + if(original_args.length == 3) { metafetch.fetch(domain_url,function(err,result){ if(err) { - resolve(false); + obj.status = false; + resolve(obj); } else { var check_key = result.meta[domain_key]; if( check_key != undefined) { if(check_key === domain_value) { - resolve(true); + obj.status = true; + resolve(obj); } else { - resolve(false); + obj.status = false; + resolve(obj); } } else { - resolve(false); + obj.status = false; + resolve(obj); } } }); @@ -93,12 +110,54 @@ var DomainVerification = (function() { reject('Mismatch arguments'); } }); - } + }; + + var verifyAll = function(domain_url,domain_key,domain_value,domain_html_name,hash_value) + { + var original_args = arguments; + return new Promise(function(resolve,reject){ + var obj = {}; + var success = []; + var failure = []; + if(original_args.length == 5) + { + Promise.all([ + htmlVerification(domain_url,domain_html_name,hash_value), + txtVerification(domain_url,domain_key,domain_value), + metaTagVerification(domain_url,domain_key,domain_value) + ]).then(results_data =>{ + results_data.forEach( data=>{ + if(data.status) + { + success.push(data.name); + } else { + failure.push(data.name); + } + }); + if(success.length > 0) + { + obj.status = true; + obj.verified = success; + } else { + obj.status = false; + obj.verified = []; + } + resolve(obj); + }).catch( error =>{ + reject(error); + }); + } else { + reject('Mismatch arguments'); + } + }); + }; + return { html: htmlVerification, txt: txtVerification, - metatag: metaTagVerification + metatag: metaTagVerification, + all:verifyAll }; })();