TLS and OIDC integration
Local fixture: Node.js 22.16.0, OpenSSL 3.5.5 CLI, loopback only, disposable self-signed certificate. Run node main.mjs in a trusted disposable directory. No external identity provider.
Run a local TLS trust and hostname regression
import tls from 'node:tls';import {execFileSync} from 'node:child_process';import {mkdtempSync,readFileSync,rmSync} from 'node:fs';import {tmpdir} from 'node:os';import {join} from 'node:path';import assert from 'node:assert/strict';const dir = mkdtempSync(join(tmpdir(),'bm-tls-'));let server;try { execFileSync('openssl',['req','-x509','-newkey','rsa:2048','-nodes', '-keyout',join(dir,'key.pem'),'-out',join(dir,'cert.pem'),'-days','1', '-subj','/CN=localhost','-addext','subjectAltName=DNS:localhost'], {stdio:'ignore',timeout:3000}); const cert = readFileSync(join(dir,'cert.pem')); server = tls.createServer({key:readFileSync(join(dir,'key.pem')),cert},s=>s.end('ok')); server.on('tlsClientError',()=>{}); await new Promise((resolve,reject)=>{server.once('error',reject);server.listen(0,'127.0.0.1',resolve);}); function probe(servername,ca) { return new Promise(resolve=>{ const s=tls.connect({host:'127.0.0.1',port:server.address().port,servername,ca,rejectUnauthorized:true}); let text=''; s.setTimeout(1000,()=>s.destroy(new Error('timeout'))); s.on('data',chunk=>{text+=chunk;}); s.once('end',()=>resolve(text==='ok'?'ok':'unexpected')); s.once('error',e=>resolve(e.code??'error')); }); } assert.equal(await probe('localhost',cert),'ok'); assert.equal(await probe('wrong.example',cert),'ERR_TLS_CERT_ALTNAME_INVALID'); assert.equal(await probe('localhost',undefined),'DEPTH_ZERO_SELF_SIGNED_CERT'); console.log('trusted peer, wrong hostname, untrusted peer: checks passed');} finally { if(server?.listening) await new Promise(resolve=>server.close(resolve)); rmSync(dir,{recursive:true,force:true});}Expected output:
trusted peer, wrong hostname, untrusted peer: checks passedThe three real loopback connections distinguish success, hostname failure and trust failure. Requires local OpenSSL; generated keys are deleted.
How it works
For HTTPS clients, verify the certificate chain against trusted roots and check that the certificate identifies the intended server. SNI conveys the intended server name during negotiation; it is not itself proof of identity. Mutual TLS adds client certificate authentication where configured. The local TLS example generates a disposable self-signed certificate with OpenSSL, explicitly trusts that certificate for this isolated lab only, and checks both trust and hostname rejection. A real production deployment needs managed trust, rotation and expiry monitoring. For OIDC, use a maintained library, trusted issuer discovery and registered redirect URIs, state and nonce validation where applicable, and authorization code flow with PKCE where appropriate. Validate signature, issuer, audience, expiry and other flow-specific claims. Access tokens and ID tokens have different consumers. The second example accepts a trusted verifier’s hypothetical result; it is not JWT decoding, signature verification or an OIDC client.
Gate application login on a trusted verification result
// A model of the result boundary, not a token validator.function login(result) { if (result?.verified !== true || typeof result.subject !== 'string' || !result.subject) return null; return {id:result.subject};}console.log(JSON.stringify(login({verified:false,subject:'u1'})));console.log(JSON.stringify(login({verified:true,subject:'u1'})));console.log(JSON.stringify(login({verified:true,subject:''})));Expected output:
null{"id":"u1"}nullThe application accepts only a successful result from the assumed trusted verifier. The fixture does not establish whether any real token is valid.
Common mistakes
Never disable certificate verification to make a connection succeed. Never treat a decoded token or an arbitrary issuer URL as trusted. Do not send real tokens to a tutorial or log them.
Practice and next steps
Explain the difference between an untrusted certificate and a trusted certificate for a different hostname. For a fictional login flow, identify which component validates the ID token and which component enforces resource permission.
Further reading: Primary documentation. Related staged reference: TLS client certificate verification.
Further reading: OpenID Connect Core 1.0.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
TLS and OIDC integration — knowledge check
Apply the security boundary and distinguish the example from a production control.
A certificate is trusted but identifies a different hostname. What should the client do?
What does an ID token primarily communicate to the OIDC client?
What is the scope of the second example?