UNPKG

tckimlikdogrula

Version:

TC Kimlik numarası doğrulama paketi - NVI web servisi

106 lines (87 loc) 3.52 kB
const https = require('https'); class TCKimlikDogrulama { constructor() { this.serviceUrl = 'tckimlik.nvi.gov.tr'; this.servicePath = '/service/kpspublic.asmx'; } /** * TC Kimlik numarası doğrulama * @param {string} tcKimlikNo - 11 haneli TC Kimlik numarası * @param {string} ad - Ad * @param {string} soyad - Soyad * @param {number} dogumYili - Doğum yılı * @returns {Promise<boolean>} - Doğrulama sonucu */ async dogrula(tcKimlikNo, ad, soyad, dogumYili) { return new Promise((resolve, reject) => { const soapEnvelope = this.createSoapEnvelope(tcKimlikNo, ad, soyad, dogumYili); const options = { hostname: this.serviceUrl, port: 443, path: this.servicePath, method: 'POST', headers: { 'Content-Type': 'application/soap+xml; charset=utf-8', 'Content-Length': Buffer.byteLength(soapEnvelope) } }; const req = https.request(options, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { try { const result = this.parseSoapResponse(data); resolve(result); } catch (error) { reject(new Error(`Response parse hatası: ${error.message}`)); } }); }); req.on('error', (error) => { reject(new Error(`İstek hatası: ${error.message}`)); }); req.write(soapEnvelope); req.end(); }); } createSoapEnvelope(tcKimlikNo, ad, soyad, dogumYili) { return `<?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <TCKimlikNoDogrula xmlns="http://tckimlik.nvi.gov.tr/WS"> <TCKimlikNo>${tcKimlikNo}</TCKimlikNo> <Ad>${ad.toUpperCase()}</Ad> <Soyad>${soyad.toUpperCase()}</Soyad> <DogumYili>${dogumYili}</DogumYili> </TCKimlikNoDogrula> </soap12:Body> </soap12:Envelope>`; } parseSoapResponse(xmlData) { const resultMatch = xmlData.match(/<TCKimlikNoDogrulaResult>(.*?)<\/TCKimlikNoDogrulaResult>/); if (!resultMatch) { throw new Error('Geçersiz response formatı'); } return resultMatch[1] === 'true'; } static validateTCKimlikNo(tcKimlikNo) { if (!tcKimlikNo || tcKimlikNo.length !== 11) { return false; } if (!/^\d+$/.test(tcKimlikNo)) { return false; } if (tcKimlikNo[0] === '0') { return false; } const digits = tcKimlikNo.split('').map(Number); const sum1 = digits[0] + digits[2] + digits[4] + digits[6] + digits[8]; const sum2 = digits[1] + digits[3] + digits[5] + digits[7]; const checkDigit1 = (sum1 * 7 - sum2) % 10; const checkDigit2 = (sum1 + sum2 + checkDigit1) % 10; return checkDigit1 === digits[9] && checkDigit2 === digits[10]; } } module.exports = TCKimlikDogrulama;