Skip to content

Instantly share code, notes, and snippets.

@thameera
Last active June 9, 2021 00:45
Show Gist options
  • Save thameera/877d3b9f080d1113f373aa46d32a4fbc to your computer and use it in GitHub Desktop.
Save thameera/877d3b9f080d1113f373aa46d32a4fbc to your computer and use it in GitHub Desktop.
Calculate thumbprints from an ADFS metadata file

How to use this code

  1. Download the ADFS metadata file and save it as FederationMetadata.xml.
  2. Install dependencies with: npm install [email protected] [email protected]
  3. Run the file: node calcFSThumbprint.js
const xpath = require('xpath')
const dom = require('xmldom').DOMParser
const crypto = require('crypto')
const fs = require('fs')
const calcThumbprint = function (cert) {
const shasum = crypto.createHash('sha1')
const der = new Buffer(cert, 'base64').toString('binary')
shasum.update(der, 'binary')
return shasum.digest('hex')
}
const xml = fs.readFileSync('./FederationMetadata.xml', 'utf8')
let doc
try {
doc = new dom().parseFromString(xml)
} catch (e) {
console.log('error parsing dom')
process.exit(1)
}
const select = xpath.useNamespaces({ 'xsi': 'http://www.w3.org/2001/XMLSchema-instance' })
const certs = select("//*[local-name(.)='RoleDescriptor' and @xsi:type='fed:SecurityTokenServiceType']/*[local-name(.)='KeyDescriptor' and @use='signing']/*[local-name(.)='KeyInfo' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']/*[local-name(.)='X509Data']/*[local-name(.)='X509Certificate']/text()", doc)
const thumbprints = certs.map(function (cert) {
return calcThumbprint(cert.toString())
});
console.log(thumbprints)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment