tls#DetailedPeerCertificate TypeScript Examples

The following examples show how to use tls#DetailedPeerCertificate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: certs.ts    From harness-cli with Apache License 2.0 6 votes vote down vote up
async function getCerts(host: string) {
    process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
    const options = {
        host: host,
        port: 443,
        method: 'GET',
    }
    return new Promise<string[]>(resolve => {
        const request = https.request(options, res => {
            let cert = (res.connection as TLSSocket).getPeerCertificate(true)
            const list = new Set<DetailedPeerCertificate>()
            do {
                list.add(cert)
                cert = cert.issuerCertificate
            } while (cert && typeof cert === 'object' && !list.has(cert))

            const certs = [...list].map(c => {
                const prefix = '-----BEGIN CERTIFICATE-----\n'
                const postfix = '-----END CERTIFICATE-----'
                const pemText = prefix + c.raw.toString('base64').match(/.{0,64}/g)?.join('\n') + postfix
                return pemText
            })
            process.env.NODE_TLS_REJECT_UNAUTHORIZED = ''            
            resolve(certs)
        })

        request.end()
    })
}