Skip to content

Commit b7d3088

Browse files
committed
[CHORE]: fix dns01
1 parent d566612 commit b7d3088

File tree

1 file changed

+70
-35
lines changed

1 file changed

+70
-35
lines changed

src/app-auth/services/app-auth.service.ts

Lines changed: 70 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -382,59 +382,94 @@ export class AppAuthService {
382382
}
383383

384384
private async verifyDNS01(domain: URL, txt: string) {
385-
const resolveDNSURL = `${DNS_RESOLVER_URL}?name=${
386-
new URL(domain).host
387-
}&type=TXT`;
388-
const actuaTxt = txt;
389-
const res = await fetch(resolveDNSURL, {
390-
headers: {
391-
'Content-Type': 'Application/json',
392-
},
393-
});
385+
// Sanitize domain url: remove www. prefix and normalize
386+
let hostname = domain.hostname;
387+
if (hostname.startsWith('www.')) {
388+
hostname = hostname.substring(4);
389+
}
394390

395-
const json = await res.json();
396-
const txtRecords = json.Answer?.filter((record: any) => record.type === 16);
397-
const txtRecord = txtRecords?.find((record: any) =>
398-
record.data.includes(txt),
399-
);
400-
if (!txtRecord) {
391+
const resolveDNSURL = `${DNS_RESOLVER_URL}?name=${hostname}&type=TXT`;
392+
Logger.debug(`Resolving DNS TXT record for domain: ${hostname}`);
393+
394+
try {
395+
const res = await fetch(resolveDNSURL, {
396+
headers: {
397+
'Content-Type': 'application/json',
398+
},
399+
});
400+
401+
if (!res.ok) {
402+
return {
403+
verified: false,
404+
error: new Error(
405+
`DNS resolution failed with status ${res.status}. Please try again later.`,
406+
),
407+
};
408+
}
409+
410+
const json = await res.json();
411+
Logger.debug(`DNS response for ${hostname}:`, json);
412+
413+
const txtRecords = json.Answer?.filter(
414+
(record: any) => record.type === 16,
415+
);
416+
const txtRecord = txtRecords?.find((record: any) =>
417+
record.data.includes(txt),
418+
);
419+
420+
if (!txtRecord) {
421+
return {
422+
verified: false,
423+
error: new Error(
424+
`DNS TXT record "${txt}" not found for domain ${hostname}. Please ensure you have added the correct DNS record and wait for propagation.`,
425+
),
426+
};
427+
}
428+
429+
Logger.debug(`DNS TXT record verified successfully for ${hostname}`);
401430
return {
402-
verified: false,
403-
error: new Error('DNS TXT record not found'),
431+
TXT: txtRecord,
432+
verified: true,
404433
};
405-
}
406-
if (txtRecord.data !== actuaTxt) {
434+
} catch (error) {
435+
Logger.error(`Error during DNS verification: ${error.message}`);
407436
return {
408437
verified: false,
409-
error: new Error('DNS TXT record not found'),
438+
error: new Error(
439+
`Failed to verify DNS TXT record: ${error.message}. Please try again later.`,
440+
),
410441
};
411442
}
412-
413-
return {
414-
TXT: txtRecord,
415-
verified: true,
416-
};
417443
}
418444

419445
private async verifyDNS01Validation(domain, txtRecord) {
420-
// verify DNS-01 domain
421-
// const domainLinkage = new DomainLinkage(domain);
422-
const d = new URL(domain.includes('http') ? domain : 'https://' + domain);
423-
const fetchedTxtRecord = await this.verifyDNS01(d, txtRecord);
446+
// Verify DNS-01 domain validation
447+
// Sanitize domain: remove www., normalize protocol
448+
let domainUrl = domain.trim();
449+
450+
// Add https:// if no protocol specified
451+
if (!domainUrl.includes('http://') && !domainUrl.includes('https://')) {
452+
domainUrl = 'https://' + domainUrl;
453+
}
454+
455+
const urlObj = new URL(domainUrl);
456+
const fetchedTxtRecord = await this.verifyDNS01(urlObj, txtRecord);
457+
424458
if (fetchedTxtRecord && fetchedTxtRecord.error) {
425459
throw new BadRequestException([
426-
fetchedTxtRecord.error?.message +
427-
'. If you have already added then it may take a while to complete. Please try again in sometime.',
460+
fetchedTxtRecord.error?.message ||
461+
'DNS verification failed. If you have recently added the record, it may take some time to propagate. Please try again later.',
428462
]);
429463
}
430-
if (fetchedTxtRecord.verified) {
464+
465+
if (fetchedTxtRecord && fetchedTxtRecord.verified) {
431466
return {
432467
verified: true,
433468
};
434469
} else {
435-
return {
436-
verified: false,
437-
};
470+
throw new BadRequestException([
471+
'Domain verification failed. Please check your DNS records and try again.',
472+
]);
438473
}
439474
}
440475

0 commit comments

Comments
 (0)